I have problems integrating CUDA code into an existing C++ system.
The structure is like this
./CudaTest.cpp
./MCSimulation.h
./GPU/MCSimulation.cuh
./GPU/MCSimulation.cu
When I try to compile the project in VS with a call from MCSimulation.h to a function in MCSimulation.cu I’m getting the following linkin error
error LNK2001: unresolved external symbol "void __cdecl initEngine(int)" (?initEngine@@YAXH@Z)
Apparently VS2012 is not compiling the .cu file and therefore not linking it. I don’t know how to solve this problem of having CUDA files in different directories because having all the files in the same dir everything compiles with no problem.
CudaTests.cpp
#include "MCSimulation.h"
int main()
{
MCSimulation<Gpu::On> A;
A.run();
MCSimulation<Gpu::Off> B;
B.run();
}
MCSimulation.h
#ifndef __MCSIMULATION_H__
#define __MCSIMULATION_H__
#include <iostream>
#include "GpuSwitch.h"
#include "GPU\MCSimulation.cuh"
template<Gpu::Switch _switch>
class MCSimulation
{
private:
int init;
public:
MCSimulation()
{
if (_switch == Gpu::On)
{
init = 100;
}
else
{
init = 000;
}
}
void run();
};
template<Gpu::Switch _switch>
void MCSimulation<_switch>::run()
{
if (_switch == Gpu::On)
{
std::cout << "Hola On - " << init << std::endl;
initEngine(init);
}
else
{
std::cout << "Hola Off - " << init << std::endl;
initEngine(init);
}
}
#endif
MCSimulation.cuh
#ifndef __MCSIMULATION_CUH__
#define __MCSIMULATION_CUH__
void initEngine(int v);
#endif __MCSIMULATION_CUH__
MCSimulation.cu
#include <cuda_runtime.h>
#include <iostream>
#include "MCSimulation.cuh"
__global__
void kernel(int v)
{
printf("Value: %d\n", v);
}
void initEngine(int v)
{
cudaStream_t s;
cudaStreamCreate(&s);
kernel<<<1,1>>>(v);
cudaDeviceSynchronize();
cudaStreamDestroy(s);
}
I can tell you that compiling seperately with the command line and then linking the obj files with nvcc produces a good executable file but I cannot figure out to make it work in VS2012 and this is only a test example because the actual project has many files in several different directories.
CudaTests.zip (5.48 KB)