无法将内核的原型(prototype)放在 header 中

标签 c cuda makefile

io.cuh:

#ifndef IO_CUH
#define IO_CUH

#include <cuda.h>

typedef struct{
    unsigned width;
    unsigned height;
    unsigned char *image; //RGBA
}rgb_image;

__global__ void transformToGrayKernel(rgb_image *img);
void decodeTwoSteps(const char* filename, rgb_image *img);
void encodeOneStep(const char* filename, rgb_image *img);
void processImage(const char *filename, rgb_image *img);

#endif // IO_CUH

我正在尝试使用以下 makefile 编译简单的程序:

lodepng.o: lodepng.h
        cc -c lodepng.c -o lodepng.o
io.o: io.cuh lodepng.o
        nvcc -c io.cu -o io.o
main: io.o
        nvcc main.c io.o -o main

'main.c' 使用 io.cu 中的一个函数,它依赖于 lodepng.c。

在一些引用代码的小警告之后,我得到了以下错误:

nvcc main.c io.o -o main nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release. In file included from main.c:1:0: io.cuh:12:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘void’ global void transformToGrayKernel(rgb_image *img); ^ makefile:6: recipe for target 'main' failed make: *** [main] Error 1

最佳答案

错误是由关键字__global__引起的,而不是你内核的原型(prototype)。

那是因为编译器nvcc会解释它的输入文件,并选择相应的规则来编译它。虽然您使用 nvcc 来编译“main.c”,但它会被视为 C 源文件,而不是 CUDA 源代码。所以编译器在编译文件“main.c”时无法识别CUDA关键字__global__

您可以将文件类型从“main.c”更改为“main.cu”,它将被视为 CUDA 源代码。然后编译器可以识别关键字__global__

关于无法将内核的原型(prototype)放在 header 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29867956/

相关文章:

performance - 将构建的持续时间添加到 Makefile 中

C - 如何将宽字符日语字符转换为 UTF-8?

c - 用 C 中的交换程序理解指针

cuda - NVCC 失败,错误为 ""_GLIBCXX_MATH_H“未定义”

c++ - 我可以在 CUDA 中对设备类和主机类使用 vector 吗

makefile - 检测未使用的 makefile 命令行参数

c - 当符号等于零时运行时未定义的链接器符号

c - 找不到 lib curl 符号

c - 为什么可以通过额外的比较来检查枚举位掩码?

opencv - 使用 CUDA 在 Visual Studio 2012 中构建 OpenCV