How to call cuda function from c++ file?

Hi,
I have a c++ QT project in which I would like to perform some calculations with CUDA, but I am having trouble adding CUDA to my already existing project. I’ve successfully installed CUDA and am able to run the samples and create CUDA runtime projects and compile/run them.

I’ve tried to add CUDA by right clicking on my QT project and selecting “Build Dependencies > Build Customization” and checking the box for “CUDA 9.2(.targets, .props)”. This seems to allow me to import CUDA dependencies properly but I’m still getting errors when trying to compile the project:

Test.cu

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

__global__ void test_kernel(void) {
}

void wrapper(void) {
	test_kernel <<<1, 1 >>> ();
}

main.cpp

#include "Test.cu"
#include <cuda.h>

int main(void) {
	wrapper();
	return 0;
}

When compiling with visual studio I get error code E0029, “expected an expression” at line 10 in Test.cu. I guessed that this was because of visual studio not compiling with nvcc and thus the “<<<X,Y>>>” syntax was invalid. I tried to handle this by compiling manually from terminal with “nvcc main.cpp” but then I received the error “error C2059: syntax error: ‘<’”. Finally, I moved the main function into Test.cu and just compiled that file (just a sanity check) and it compiled and ran successfully. I tried to rename the main.cpp file to main.cu and calling the function this way works when manually compiling “nvcc main.cu” but not from visual studio.

As I’d like to call the CUDA functions from my c++ code, I now wonder how would I go about doing this in visual studio? Alternatively, how do I set Visual Studio to compile using nvcc, and will this work with QT?

1 Like

You don’t want to include Test.cu in main.cpp

Instead you should create a header file with your wrapper function prototype in it, and include that header file in both Test.cu and main.cpp

This is pretty much exactly what you would do if you had 2 .cpp files and wanted to call functions from one file in the other file. You wouldn’t put #include “Otherfile.cpp” in main.cpp. You would use a header file.

1 Like

Changing the code to

main.cpp

#include "Test.cuh"

int main(int argc, char *argv[])
{
	Wrapper::wrapper();
}

Test.cuh

#pragma once
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

namespace Wrapper {
	void wrapper(void);
}

Test.cu

#include "Test.cuh"

__global__ void test_kernel(void) {
}

namespace Wrapper {
	void wrapper(void)
	{
		test_kernel <<<1, 1>>> ();
		printf("Hello, world!");
	}
}

Allows me to compile and run the code. Thanks for your help. I also had to add “$(CudaToolkitLibDir)” to Project “Properties > Linker > General > Additional Library Directories” and add “cudart.lib” to Project “Properties > Linker > Input > Additional dependencies” before it would start working.

Edit: One thing I was wondering about though, the “Hello, World!” doesn’t seem to get printed and no terminal opens.

4 Likes

Hi
I wanted also to run CUDA kernels in .CPP file but U get this error:

|Error|LNK2019|unresolved external symbol void __cdecl Wrapper::wrapper(void) (?wrapper@Wrapper@@YAXXZ) referenced in function main|CudaWrapping|

I even added cudart.lib to my project

2 Likes

I also have this error, it make me confuse , how to fix it?