比如一下代码是正常的:
kernel.h
extern __global__ void kernel();
#include <stdio.h> #include "kernel.h" __global__ void kernel() { printf("hello world!"); }
#include "kernel.h" int main(void) { kernel<<<1,1>>>(); cudaDeviceSynchronize(); return 0; }
但如果把test.cu的名字改为test.cpp,就出错了。
make all Building file: ../src/test2.cpp Invoking: NVCC Compiler /usr/local/cuda-5.5/bin/nvcc -G -g -O0 -gencode arch=compute_35,code=sm_35 -odir "src" -M -o "src/test2.d" "../src/test2.cpp" /usr/local/cuda-5.5/bin/nvcc -G -g -O0 --compile -x c++ -o "src/test2.o" "../src/test2.cpp" In file included from ../src/test2.cpp:3:0: ../src/kernel.h:5:8: 错误: ‘__global__’不是一个类型名 ../src/test2.cpp: 在函数‘int main()’中: ../src/test2.cpp:10:2: 错误: ‘kernel’在此作用域中尚未声明 ../src/test2.cpp:10:10: 错误: expected primary-expression before ‘<’ token ../src/test2.cpp:10:16: 错误: expected primary-expression before ‘>’ token ../src/test2.cpp:10:18: 错误: expected primary-expression before ‘)’ token ../src/test2.cpp:11:24: 错误: ‘cudaDeviceSynchronize’在此作用域中尚未声明 make: *** [src/test2.o] 错误 1
在这里找到答案
http://stackoverflow.com/questions/16550031/cuda-device-and-global-error-expected-constructor-destructor-or-type
The errors appeared because you included a header file with device code decorators (__device__
and__global__
) in a .cpp file.
The .cpp file by default gets handled directly by the host compiler, which does not know what those decorators mean.
因为cpp默认是用主机编译器即c++的编译器来处理的。
而在test.cpp中包含了kernel.h,kernel.h中声明了__global__函数。
cuda编程:不要在一个cpp文件中声明__device__和__global__函数,布布扣,bubuko.com
cuda编程:不要在一个cpp文件中声明__device__和__global__函数
原文:http://blog.csdn.net/lingerlanlan/article/details/25063331