4

I tried to compile this code:-

#include <vector>

using namespace std;

int main() {
    vector<int> v(5);
    iota(v.begin(), v.end(), 0);
}

And I compiled it with this command:-

D:\workspace\test>nvcc main.cpp --std=c++11

(Because without specifying the std I was getting the "identifier iota() not found" error)

And I get this error:-

nvcc warning : The -std=c++11 flag is not supported with the configured host compiler. Flag will be ignored.
main.cpp
main.cpp(7): error C3861: 'iota': identifier not found

How do I specify the C++ standard I want nvcc to use?

Also, compiling host code separately with g++ and device code with nvcc and then linking the objects with nvcc doesn't work. I get this.

dtn34-
  • 321
  • 3
  • 11
  • 1
    According to [documentation](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#options-for-altering-compiler-linker-behavior) C++ is supported up to 14. Is your compiler up to date? Have you tried the long form (--std=, note the double minus)? – Aconcagua Jul 02 '18 at 09:02
  • See the command I used to compile the code again. I have used the double minus. The command line output is of the same command. And I am using the MSVS 2015. So I'm assuming the cl compiler should be new. – dtn34- Jul 02 '18 at 09:03
  • Joining object code from different compilers into one single executable or library is almost always bound to fail. Each compiler might compile even already the standard libraries in its own way and it is just too likely that these different implementations are incompatible with each other. You might, though, compile one of both into a DLL with C-only interface and load this one from the other project... – Aconcagua Jul 02 '18 at 09:04
  • *"On all platforms, the default host compiler executable (gcc and g++ on Linux, clang and clang++ on Mac OS X, and cl.exe on Windows) found in the current execution search path will be used, unless[...]"* - have you tried passing in [MSVC style](https://blogs.msdn.microsoft.com/vcblog/2016/06/07/standards-version-switches-in-the-compiler/)? – Aconcagua Jul 02 '18 at 09:14
  • @Aconcagua Yes. I get _nvcc fatal : Don't know what to do with 'D:/std:c++11'_ I think those switches are to be used while using Visual Studio. And I don't want to use Visual Studio... for my own reasons. – dtn34- Jul 02 '18 at 09:20
  • Can understand not wanting to use MSVC... Avoid it whenever possible myself as well... Found a [--compiler-bindir](https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#file-and-path-specifications); option, but no way do indicate using gcc explicitly; only found some hints that you likely won't get around MSVC (but all quite old). Perhaps its possble to fool nvcc by setting formentioned option and copy gcc.exe to cl.exe in gcc's bin directory? – Aconcagua Jul 02 '18 at 09:33
  • @Aconcagua I'm sorry, I should've mentioned this in the question: I have tried --compiler-bindir. Check the latest edit in the question. – dtn34- Jul 02 '18 at 11:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174163/discussion-between-dtn34-and-aconcagua). – dtn34- Jul 02 '18 at 11:34

2 Answers2

4

I think you need to add #include <numeric>. enter image description here

Mayfly
  • 51
  • 1
0

There is no need. By default the command line tool nvcc uses Microsoft's cl.exe. And if your cl.exe is updated, the std option is not available. cl.exe automatically supports all the latest C++ standard's features.

However, in cl.exe some of the functions like iota() aren't defined in the std namespace. Instead, iota() is defined in the numeric.h header file. So to run that code you would need to include the said header file. The final code should look like this:-

#include <vector>
#include <numeric.h>

using namespace std;

int main() {
    vector<int> v(5);
    iota(v.begin(), v.end(), 0);
}

The code can be compiled by the command:-

nvcc main.cpp
dtn34-
  • 321
  • 3
  • 11
  • several things wrong with this answer. iota is defined in the std namespace, and you should be including numeric ut the .c – xdavidliu Sep 10 '22 at 20:59