Original article address:[Cuda learning-original] engineering implementation that includes C and. Cu!
Author:Black tie again
There is a pure C project in which you want to call the function of the. Cu file. There is a cppintegeration example in the project, but this is called in CPP. the functions in the Cu, and the project is directly referenced by many header files and libraries in the SDK. Therefore, portability is very poor. After a few days of exploration, I finally solved the problem of calling the Cu file in the C Project. Share it with you ~~~
Create two files, A. Cu AND a_kernel.cu. A. Cu are called interfaces and functions implemented in a_kernel.cu.
Then, right-click a. cu-> properties-> Custom generation step-> General-> command line, and add the following command:
"Certificate (cuda_bin_path)nvcc.exe"-ccbin "$ (vcinstalldir) bin"-I "$ (nvsdkcuda_root) commoninc"-I "$ (cuda_inc_path)"-I. -- Host-compilation C-g-C-M 32-o "$ (inputname ). OBJ "" $ (inputdir) $ (inputname ). CU".
Note: -- Host-compilation must be added. Otherwise, the system prompts link: Fatal error lnk1561: the entry point must be defined. In addition, you must add the SDK and INC paths to include the header files "cutil. H" in A. cu.
Add include <a_kernel.cu> to the header of A. cu.
Note: The generated. OBJ path must be "$ (inputname). OBJ". Otherwise, the system will prompt that F. OBJ cannot be found during compilation.
The function f in A. Cu must be defined as follows:
Extern "C" Void F (){}// This must be defined during definition. Otherwise, the system prompts "cannot parse _ f" when linking.
However, when referencing it in file C, you must declare the following statement:
Extern void F (){};// Note: If "C" is added, an error is returned.
In addition, in the C Project linker-input-add dependencies, add
Cudart. Lib cutil32.lib// These two libraries must be added; otherwise, the system prompts: external symbols that cannot be parsed_ Cudaconfigurecall @ 32Errors.
In this way, you can call f () in the C file.
The problem has been solved. You can proceed to the next step !!!
PS: I found that many people use blogs to record technical problems. It is a good habit. Good memory is not as good as that of blog...