一个简单的c ++ HelloWorld和cuda

时间:2015-12-30 10:14:34

标签: c++ cuda

我试图用cuda制作我的第一个节目。 所以我用各种页面的指南制作了这个简单的HelloWorld。

#include <cstdlib>
#include <cstdio>
#include <cuda.h>

using namespace std;

__global__ void mykernel(void) {
}

int main(void) {
mykernel<<<1,1>>>();
printf("CPU Hello World!\n");
return 0;
} 

但我得到了这个输出:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/hellowordcuda
make[2]: Entering directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
mkdir -p build/Debug/GNU-Linux-x86
rm -f "build/Debug/GNU-Linux-x86/hellowordcuda.o.d"
g++    -c -g -I/usr/local/cuda-7.5/include -MMD -MP -MF "build/Debug/GNU-Linux-x86/hellowordcuda.o.d" -o build/Debug/GNU-Linux-x86/hellowordcuda.o hellowordcuda.cpp
hellowordcuda.cpp:19:1: error: ‘__global__’ does not name a type
 __global__ void mykernel(void) {
 ^
hellowordcuda.cpp: In function ‘int main()’:
hellowordcuda.cpp:24:1: error: ‘mykernel’ was not declared in this scope
 mykernel<<<1,1>>>();
 ^
hellowordcuda.cpp:24:11: error: expected primary-expression before ‘<’ token
 mykernel<<<1,1>>>();
           ^
hellowordcuda.cpp:24:17: error: expected primary-expression before ‘>’ token
 mykernel<<<1,1>>>();
                 ^
hellowordcuda.cpp:24:19: error: expected primary-expression before ‘)’ token
 mykernel<<<1,1>>>();
                   ^
make[2]: *** [build/Debug/GNU-Linux-x86/hellowordcuda.o] Error 1
make[2]: Leaving directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/sebastian/Dropbox/Universidad/Trabajo_de_Grado_pregrado/Codigos/HelloWordCuda'
 make: *** [.build-impl] Error 2

我很抱歉提出这么简单的问题,但我找不到任何答案。

非常感谢,非常感谢任何帮助!

3 个答案:

答案 0 :(得分:8)

要完成这项工作,您需要做两件事:

  1. 使用CUDA编译器驱动程序nvcc来引导代码编译
  2. 将代码传递给hellowordcuda.cpp 时,
  3. hellowordcuda.cu重命名为nvcc

    第二点是必要的,因为nvcc使用文件扩展名来引导编译,如果你的代码有.cc.cpp文件扩展名,它只会将代码传递给将导致主编译器和相同的编译错误

答案 1 :(得分:4)

您似乎正在使用g ++直接构建。您需要使用NVidia的编译器(nvcc)来使用CUDA,并确保它知道将文件作为CUDA C处理。这可以通过将扩展名更改为.cu或通过玩游戏来实现指定文件的编译选项&amp;处理类型。我推荐前者。

答案 2 :(得分:3)

在指南中也提到了以下几行,

$ nvcc hello.cu
$ a.out
Hello World!

请在Makefile中将g++更改为nvcc