I have encountered a problem in Visual Studio while using the CUDA toolkit. I want to write my code in multiple .cu files. However, It always gives me a building failure. I did a lot of search on it but I cannot find a solution. Here are what I tried:
- Make sure the Item type is CUDA C/C++ not C++
- Set general relocatable code rcd+=true(Which is the solution for most posts)
- Change the host runtime library to multi-threaded debug(/Mtd)
But none of them works, I got the same error.
My endpoint is to create a matrix class that can be used on both host and device. To make the problem simple, I only keep the construct and print function in the class(But has the same error). Here is the header of the matrix:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#ifndef __alldev__
#define __alldev__ __host__ __device__
#endif
template <class T>
class Matrix {
T* m = NULL;
T* dev_m = NULL;
public:
int rowNum = 0;
int colNum = 0;
Matrix(T* data, int rowNum, int colNum, int location = 0);
void print();
__alldev__ int ind(int i, int j) {
return (i - 1) + (j - 1)*colNum;
}
};
Here is the .cu file of the matrix:
#include "Matrix.cuh"
template <class T>
Matrix<T>::Matrix(T* data, int rowNum, int colNum, int location = 0) {
this->rowNum = rowNum;
this->colNum = colNum;
m = new T[rowNum*colNum];
for (int i = 1; i <= rowNum; i++) {
for (int j = 1; j <= colNum; j++) {
m[ind(i, j)] = data[ind(i, j)];
}
}
}
template <class T>
void Matrix<T>::print() {
for (int i = 1; i <= rowNum; i++) {
for (int j = 1; j <= colNum; j++) {
std::cout << m[ind(i, j)] << " ";
}
std::cout << std::endl;
}
}
The main file:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include "Matrix.cuh"
int main()
{
int N = 10;
float * data = new float[N];
for (int i = 0; i < N; i++) {
data[i] = i;
}
Matrix<float> test(data,N,1);
test.print();
}
The above code works fine when they are in the same file, but does not work when in different files. The most strange thing is that if I comment out the kernel function call, the error message will be changed. I totally have no clue of it.
Here are the normal level output from VS:
1>------ Build started: Project: test1, Configuration: Debug x64 ------
1>Build started 8/31/2018 7:27:07 PM.
1>Target InitializeBuildStatus:
1> Creating "x64\Debug\test1.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
1>Target AddCudaCompileDeps:
1> Skipping target "AddCudaCompileDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompileDeps:
1> Skipping target "AddCudaCompileDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompilePropsDeps:
1> Skipping target "AddCudaCompilePropsDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompilePropsDeps:
1> Skipping target "AddCudaCompilePropsDeps" because all output files are up-to-date with respect to the input files.
1>Target AddCudaCompilePropsDeps:
1> Skipping target "AddCudaCompilePropsDeps" because all output files are up-to-date with respect to the input files.
1>Target CudaBuild:
1> Target CudaBuildCore:
1> Compiling CUDA source file kernel.cu...
1> Target CudaBuildCore:
1> Skipping target "CudaBuildCore" because all output files are up-to-date with respect to the input files.
1> Target CudaBuildCore:
1> Skipping target "CudaBuildCore" because all output files are up-to-date with respect to the input files.
1>
1> C:\Users\wangj\documents\visual studio 2017\Projects\test1\test1>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin\nvcc.exe" -gencode=arch=compute_30,code=\"sm_30,compute_30\" --use-local-env --cl-version 2017 -ccbin "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x64" -x cu -rdc=true -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\include" -G --keep-dir x64\Debug -maxrregcount=0 --machine 64 --compile -cudart static -g -DWIN32 -DWIN64 -D_DEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /Od /FS /Zi /RTC1 /MDd " -o x64\Debug\kernel.cu.obj "C:\Users\wangj\documents\visual studio 2017\Projects\test1\test1\kernel.cu"
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(3477): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(4417): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(5319): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7104): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(776): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(1636): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\device_double_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h(925): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(3477): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(4417): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(5319): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7104): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(776): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(1636): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\device_double_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h(925): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> kernel.cu
1> C:/Users/wangj/documents/visual studio 2017/Projects/test1/test1/kernel.cu(16): warning C4244: '=': conversion from 'int' to 'float', possible loss of data
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h : warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(3477): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(4417): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(5319): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7104): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
1> Done building target "CudaBuildCore" in project "test1.vcxproj".
1>
1> Done building project "test1.vcxproj".
1>Target CudaLink:
1> C:\Users\wangj\documents\visual studio 2017\Projects\test1\test1>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin\nvcc.exe" -dlink -o x64\Debug\test1.device-link.obj -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd " -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64" cudart.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib -gencode=arch=compute_30,code=sm_30 -G --machine 64 x64\Debug\kernel.cu.obj x64\Debug\Matrix.cu.obj x64\Debug\Matrix.cuh.obj
1> cudart.lib
1> kernel32.lib
1> user32.lib
1> gdi32.lib
1> winspool.lib
1> comdlg32.lib
1> advapi32.lib
1> shell32.lib
1> ole32.lib
1> oleaut32.lib
1> uuid.lib
1> odbc32.lib
1> odbccp32.lib
1> kernel.cu.obj
1> Matrix.cu.obj
1> Matrix.cuh.obj
1>Target Link:
1> kernel.cu.obj : error LNK2019: unresolved external symbol "public: __cdecl Matrix<float>::Matrix<float>(float *,int,int,int)" (??0?$Matrix@M@@QEAA@PEAMHHH@Z) referenced in function main
1> kernel.cu.obj : error LNK2019: unresolved external symbol "public: void __cdecl Matrix<float>::print(void)" (?print@?$Matrix@M@@QEAAXXZ) referenced in function main
1> C:\Users\wangj\documents\visual studio 2017\Projects\test1\x64\Debug\test1.exe : fatal error LNK1120: 2 unresolved externals
1>Done building target "Link" in project "test1.vcxproj" -- FAILED.
1>
1>Done building project "test1.vcxproj" -- FAILED.
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:06.70
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I will really appreciate it if any one can me any suggestion.