gcc - 创建 CUDA 共享库和 libpthread 的问题

标签 gcc linker cuda pthreads ld

我目前正在尝试使用 CUDA 例程创建一个库,但遇到了麻烦。我将使用一个相当小的示例来解释我的问题,我的实际库会更大。

我已经成功编写了 test.cu,这是一个包含 __global__ CUDA 函数及其包装器的源文件(用于分配和复制内存)。我还可以使用以下命令成功地将这个文件编译成共享库:

nvcc -c test.cu -o test.o -lpthread -lrt -lcuda -lcudart -Xcompiler -fPIC
gcc -m64 -shared -fPIC -o libtest.so test.o -lpthread -lrt -lcuda -lcudart -L/opt/cuda/lib64

生成的libtest.so导出我需要的所有符号。

我现在编译我的纯 C main.c 并将其链接到我的库:

gcc -std=c99 main.c -o main -lpthread -ltest -L.

这一步也成功了,但是在执行 ./main 时,所有被调用的 CUDA 函数都会返回错误:

test.cu:17:cError(): cudaGetDeviceCount: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaMalloc: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaMemcpy: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaMemcpy: [38] no CUDA-capable device is detected
test.cu:17:cError(): cudaFree: [38] no CUDA-capable device is detected

(错误消息是通过我自己的调试功能创建的)

在最初的步骤中,我遇到了完全相同的问题,因为我直接从 test.cu 创建可执行文件,因为我忘记链接 libpthread (-lpthread )。但是,正如您在上面看到的,我已将所有源文件链接到 libpthread。根据lddlibtest.somain都依赖于libpthread,这是理所应当的。

我在 ArchLinux 上使用 CUDA 5(是的,我确实意识到它是测试版)以及 gcc 4.6.3 和 nvidia 驱动程序版本 302.06.03。

如果能帮助解决这个问题,我们将不胜感激!

最佳答案

这是一个简单的例子......

// File: test.cu
#include <stdio.h>

__global__ void myk(void)
{
    printf("Hello from thread %d block %d\n", threadIdx.x, blockIdx.x);
}

extern "C"
void entry(void)
{
    myk<<<1,1>>>();
    printf("CUDA status: %d\n", cudaDeviceSynchronize());
}

编译/链接nvcc -m64 -arch=sm_20 -o libtest.so --shared -Xcompiler -fPIC test.cu .

// File: main.c
#include <stdio.h>

void entry(void);

int main(void)
{
    entry();
}

编译/链接gcc -std=c99 -o main -L. -ltest main.c .

关于gcc - 创建 CUDA 共享库和 libpthread 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885565/

相关文章:

mac 上的 python-config ldflags

python - PyCUDA:查询设备状态(特别是内存)

c++ - CUDA 是否提供类似 future 的功能?

c++ - Typedef 适用于结构但不适用于枚举,仅适用于 C++

C++:指向 std::map 的指针的奇怪行为

c++ - 为什么这个使用统一初始化的代码片段使用 g++4.6 而不是 g++4.7 编译?

c++ - 使用 directX 和 Visual Studio 2010 的链接器错误

在 C 中通过 HTTP 连接到网站

c++ - 如何使用交叉链接器而不是 native 链接器

cuda - 如何在 CUDA 中使用 64 位指针编写指针追踪基准测试?