Include CUDA in C++ project

Hi,

I want include CUDA in existing project.
In examples - existing project “cppIntegration”.

But I don now how doing it :(

This is something that confused me when I first started with CUDA.

Here’s how I do it:

myProject.cu:

extern "C" {

__host__ int myFunction( ... ) {

    (some code)

    myKernel<<< ... >>>( ... );

     }

}

__global__ void myKernel( ... ) {

   ...

    }

Compile the .cu file into an object like so:

nvcc -c myProject.cu

main.cpp:

extern "C" {

    void myFunction( ... );

     }

int main( int argc, char* args[] ) {

int myResult;

// some code 

myResult = myFunction( ... );

// some more code

}

Finally, compile the project with:

g++ -o myProject myProject.o myProject.cpp -L/usr/local/cuda/lib -lcuda -lcudart
nvcc -c myProject.cu

main.cpp:

[code]

Not to beat a dead horse, but when I try this approach to compile a .cu kernel with CUDA .9, I get the following error:

‘nvopencc’ is not recognized as an internal or external command,

operable program or batch file.

Could anyone help out? The format of the file being compiled is exactly the same as YetAnotherNoob posted.

If I happen to get a .o file when this error is fixed, how would I integrate that .o file into a DLL that I can call from another program?

Thanks,

Austin

this error indicates that nvcc isn’t in the path. In linux you’d check if /usr/local/cuda/bin is in your path.

The problem with this solution is that it works for C function but not C++ functions.
Is there a way to link g++ compiled code and nvcc compiled code in C++ (using template for example).

The following snippet from a document that I am writing will help your cause:
"
6. How do I plug-in NVCC into VC++: To compile code for CUDA under VC++, you must first create and add a new “.cu” file to a new VC++ project. Now, in the solution explorer right-click the file and go to “properties”. In the custom-build section specify “nvcc –I”$CUDA_INC_PATH” –c –keep –o ${ConfigurationName}\xxx.obj xxx.cu”

a. The “-keep” option will make sure that the intermediate files are preserved. The most important among them are “.cubin” file and the “.ptx” file. From the cubin file, you can figure out the number of registers that your kernel uses, the amount of shared memory it uses and hence the cuda-occupancy. The “ptx” file corresponds to the PTX (Parallel Thread eXecution) assembly language generated for your device code.
Also, enter the custom-build output name as “${ConfigurationName}\xxx.obj”
Also, in the Project Properties, you must add “cudart.lib cuda.lib” as “Additional Dependencies” in the “Linker section”. Also, you need to specify “$(CUDA_LIB_PATH) as the “Additional linker search path”. Otherwise, your project won’t link properly.

  1. NVCC: NVCC can also be invoked from the “Command Prompt”. This compiler defines “CUDACC” so that you can select portions of your source file that needs to be compiled with NVCC. But NVCC already has keywords that can isolate Device code and Host code. So, most likely you won’t find use for CUDACC. You can use it only to make sure that NVCC is the only compiler used to compile your source. That’s all.
    a. On windows platform, NVCC invokes the VC++ compiler “CL.EXE” to compile host code. On Linux platform, it is “gcc”
    i. Note that on Windows platform, “CL.EXE” resides at “C:\Program Files\Microsoft Visual Studio\VC98\bin” (or in some other directory depending on VC version and where you had installed it).

  2. So, make sure that the %PATH% variable has the VC++ bin directory in it. (if NOT, set the PATH environment variable from “my computer” -> properties). You need to re-launch your command prompt after changing the PATH variable via “windows”.

  3. “CL.EXE” expects certain environment variables to be SET before its invocation. When you invoke NVCC from command prompt, you need to set them all yourselves. The user has to set these variables by running “VSVARS32.BAT”. This one resides in the same directory as mentioned above.
    a. Otherwise, you can launch the VC++ command prompt that is available from “Start Menu -> Programs……”

  4. Using “verbose” option (-v) of NVCC can give you more details about the compilation progress and also about the errors that are generated.

  5. .cu extension: If you want to compile code for CUDA using NVCC, you need to name your source file with a “.cu” extension. The “NVCC” invokes the host compiler for all source files that start with “.c”.

"