But after I changed the name of “main.cpp” to “main.c”, errors was reported when linking: In function main': In function main’: undefined reference to `cudatest’
what’s the problem?
And I found that in CUDA SDK sample projects, all the C/C++ files are .cpp files, what’s the difference between .c files and .cpp files when compiled by nvcc?
nvcc compiles files with C++ linkage. If you want to link and call functions from a C file, cudatest must be declared extern “C”. Or, you could try nvcc --host-compilation=c, but I’m not sure if that works.
Unlike most Linux apps, gcc looks at the file extension to decide how to process a file. You can call it .c, but tell gcc to compile it as c++ (or use its g++ alias).
… what’s the difference between .c files and .cpp files when compiled by nvcc?
If you are referring to the CCFILES variable in Makefile, that is all magic performed by …/…/common/common.mk
Whatever is in CCFILES will be compiled by what CXX is set to (which is g++) and whatever is in CFILES will be compiled by what CC is set to (which is gcc.)
You can change those after including common.mk to what you like say CC :=gcc4.3
It seems that the problem is due to the difference between compiling with gcc and g++
when I use g++, .c file can be compiled to the same .o file as .cpp and it works when linked to executable file
but I tried nvcc to compile .c and .cpp file and find that the two .o file were not the same, and the .o file that was compiled from .c file could not be linked with cudatest.cu by nvcc.
How conld I compile .c file to get the same result as .cpp file by nvcc?
Is nvcc a super set of gcc and g++? If it is, how can I use nvcc like g++ to compile C/C++ files?
Check out /TP option of “VC++” compiler. While compiling in NVCC, pass this option to this host compiler.
MSDN:
"
The /Tc option specifies that filename is a C source file, even if it does not have a .c extension. The /Tp option specifies that filename is a C++ source file, even if it doesn’t have a .cpp or .cxx extension. A space between the option and filename is optional. Each option specifies one file; to specify additional files, repeat the option.
"
Not sure how to do that with GCC. BUt there must be a GCC option for it.