cuda单独编译模式的链接错误和cudart的静态链接

Linking error for cuda separate compilation mode and static linking of cudart

基本上,我的目标如下:我想使用 CUDA SDK Toolkit 创建一个程序,我想 运行 在安装了 NVIDIA GPU 和 NVIDIA 驱动程序但不需要 CUDA Toolkit 的系统上运行这个程序。我的想法是静态 link cuda 库。好吧,即使是一个简单的案例,我也没能做到。 让我给你看。 我有三个文件:main.cpp、kernel.cu 和 header.h。这些文件的内容如下。

main.cpp

#include <stdio.h>
#include "header.h"
int main()
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };

    // Add vectors in parallel.
    addWithCuda(c, a, b, arraySize);
    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);


    return 0;
}

header.h

#pragma once
void addWithCuda(int *c, const int *a, const int *b, unsigned int size);

kernel.cu

#include "header.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>

__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}

void addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output)    .
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }
    
    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);
}

虽然我真的不认为代码的上下文很重要,但我还是决定展示它。

然后我有两个文件:运行 和 run_static。

运行

g++ -c main.cpp;
nvcc -c kernel.cu;
g++ main.o kernel.o -o test -L/usr/local/cuda-10.2/lib64 -lcudart -lcudadevrt

run_static

g++ -c main.cpp;
nvcc -c kernel.cu;
g++ main.o kernel.o -o test_static -L/usr/local/cuda-10.2/lib64 -lcudart_static -lcudadevrt

我可以使用 make 但我决定简化一切。

好吧,./运行 创建了 test 可执行文件,它又会产生正确的输出。这是动态 linking 的情况。 但是,./run_static 不会创建 test_static。相反,它会抛出一个错误:

/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::initializeDriverEntrypoints()':
(.text+0x10990): undefined reference to `dlsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::initializeDriverEntrypoints()':
(.text+0x109ba): undefined reference to `dlsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::initializeDriverEntrypoints()':
(.text+0x109e6): undefined reference to `dlsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::initializeDriverEntrypoints()':
(.text+0x10a12): undefined reference to `dlsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::initializeDriverEntrypoints()':
(.text+0x10a3e): undefined reference to `dlsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o):(.text+0x10a6a): more undefined references to `dlsym' follow
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::loadDriverInternal()':
(.text+0x13c52): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::loadDriverInternal()':
(.text+0x13c84): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::globalState::initializeDriverInternal()':
(.text+0x15f55): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosSemaphoreSignal(sem_t*)':
(.text+0x4e1e5): undefined reference to `sem_post'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosSemaphoreWait(sem_t*, unsigned int)':
(.text+0x4e215): undefined reference to `sem_trywait'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosSemaphoreWait(sem_t*, unsigned int)':
(.text+0x4e22a): undefined reference to `sem_wait'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosSemaphoreWait(sem_t*, unsigned int)':
(.text+0x4e2ec): undefined reference to `sem_timedwait'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosSemaphoreDestroy(sem_t*)':
(.text+0x4e325): undefined reference to `sem_destroy'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosSemaphoreCreate(sem_t*, int)':
(.text+0x4e349): undefined reference to `sem_init'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `__tcf_4':
cuosLinux.cpp:(.text+0x4f4b1): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `__tcf_0':
cuosLinux.cpp:(.text+0x4f4e1): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `__tcf_1':
cuosLinux.cpp:(.text+0x4f511): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `__tcf_2':
cuosLinux.cpp:(.text+0x4f541): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `__tcf_3':
cuosLinux.cpp:(.text+0x4f571): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4fedc): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4fee8): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ff0a): undefined reference to `dlvsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ff16): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ff2c): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ff7c): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ff88): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ffaa): undefined reference to `dlvsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ffb6): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x4ffcc): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x5001c): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x50028): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x5004a): undefined reference to `dlvsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x50056): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x5006c): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x500bc): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x500c8): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x500ea): undefined reference to `dlvsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x500f6): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x5010c): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x50153): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x5015f): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x50181): undefined reference to `dlvsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x5018d): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInit()':
(.text+0x501a3): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosHasThreadExited(cudart::CUOSthread_st*)':
(.text+0x51ddb): undefined reference to `pthread_kill'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosThreadDetach(cudart::CUOSthread_st*)':
(.text+0x51df9): undefined reference to `pthread_detach'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosThreadCreateWithName(cudart::CUOSthread_st**, int (*)(void*), void*, char const*)':
(.text+0x51eb6): undefined reference to `pthread_create'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosCondCreateWithSharedFlag(pthread_cond_t*, int)':
(.text+0x52135): undefined reference to `pthread_condattr_setpshared'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosOnce(int*, void (*)())':
(.text+0x52155): undefined reference to `pthread_once'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTryAcquireWriterLock(void**)':
(.text+0x52198): undefined reference to `pthread_rwlock_trywrlock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTryAcquireReaderLock(void**)':
(.text+0x521c8): undefined reference to `pthread_rwlock_tryrdlock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitRWLockEx(void**, void*, unsigned long)':
(.text+0x52249): undefined reference to `pthread_rwlockattr_init'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitRWLockEx(void**, void*, unsigned long)':
(.text+0x5225a): undefined reference to `pthread_rwlockattr_setpshared'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitRWLockEx(void**, void*, unsigned long)':
(.text+0x52269): undefined reference to `pthread_rwlock_init'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTryEnterCriticalSection(pthread_mutex_t*)':
(.text+0x52295): undefined reference to `pthread_mutex_trylock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitializeCriticalSectionWithSharedFlag(pthread_mutex_t*, int)':
(.text+0x522fc): undefined reference to `pthread_mutexattr_init'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitializeCriticalSectionWithSharedFlag(pthread_mutex_t*, int)':
(.text+0x52321): undefined reference to `pthread_mutexattr_settype'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitializeCriticalSectionWithSharedFlag(pthread_mutex_t*, int)':
(.text+0x5232f): undefined reference to `pthread_mutexattr_setpshared'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitializeCriticalSectionWithSharedFlag(pthread_mutex_t*, int)':
(.text+0x5234a): undefined reference to `pthread_mutexattr_destroy'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTlsSetValue(unsigned int, void*)':
(.text+0x52418): undefined reference to `pthread_setspecific'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTlsAlloc(void (*)(void*))':
(.text+0x52460): undefined reference to `pthread_key_create'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosFreeLibrary(void*)':
(.text+0x52585): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosLoadLibrary(char const*)':
(.text+0x525a5): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosPosixInit()':
(.text+0x527ac): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosPosixInit()':
(.text+0x527b8): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosPosixInit()':
(.text+0x527da): undefined reference to `dlvsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosPosixInit()':
(.text+0x527e6): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosPosixInit()':
(.text+0x527fc): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `__tcf_0':
cuos_common_posix.cpp:(.text+0x52ba1): undefined reference to `dlclose'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosDestroyRWLock(void**)':
(.text+0x52bc8): undefined reference to `pthread_rwlock_destroy'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosLoadLibraryUnsafe(char const*)':
(.text+0x52c05): undefined reference to `dlerror'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitRWLock(void**)':
(.text+0x52c51): undefined reference to `pthread_rwlockattr_init'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitRWLock(void**)':
(.text+0x52c89): undefined reference to `pthread_rwlockattr_setpshared'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosInitRWLock(void**)':
(.text+0x52c98): undefined reference to `pthread_rwlock_init'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosThreadJoin(cudart::CUOSthread_st*, int*)':
(.text+0x52ccf): undefined reference to `pthread_join'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosCondCreateShared(pthread_cond_t*)':
(.text+0x533dc): undefined reference to `pthread_condattr_setpshared'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosCondCreate(pthread_cond_t*)':
(.text+0x53439): undefined reference to `pthread_condattr_setpshared'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosShmCloseEx(cudart::cuosShmInfoEx_st*, unsigned int, unsigned int)':
(.text+0x534e6): undefined reference to `shm_unlink'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosShmOpenNamedEx(void*, char const*, unsigned long, cudart::cuosShmInfoEx_st**)':
(.text+0x535b1): undefined reference to `shm_open'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosShmCreateNamedEx(void*, char const*, unsigned long, cudart::cuosShmInfoEx_st**)':
(.text+0x537cf): undefined reference to `shm_open'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosShmCreateNamedEx(void*, char const*, unsigned long, cudart::cuosShmInfoEx_st**)':
(.text+0x537e9): undefined reference to `shm_unlink'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosVirtualReserveInRange(unsigned long, void*, void*, unsigned long)':
(.text+0x53a31): undefined reference to `pthread_once'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosDestroyRWLockEx(void**)':
(.text+0x52174): undefined reference to `pthread_rwlock_destroy'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosReleaseReaderLock(void**)':
(.text+0x52184): undefined reference to `pthread_rwlock_unlock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosAcquireWriterLock(void**)':
(.text+0x521f4): undefined reference to `pthread_rwlock_wrlock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosAcquireReaderLock(void**)':
(.text+0x52204): undefined reference to `pthread_rwlock_rdlock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTlsGetValue(unsigned int)':
(.text+0x52434): undefined reference to `pthread_getspecific'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosTlsFree(unsigned int)':
(.text+0x52444): undefined reference to `pthread_key_delete'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosGetProcAddress(void*, char const*)':
(.text+0x52571): undefined reference to `dlsym'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosLoadLibrary(char const*)':
(.text+0x525b3): undefined reference to `dlopen'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosReleaseWriterLock(void**)':
(.text+0x52a44): undefined reference to `pthread_rwlock_unlock'
/usr/local/cuda-10.2/lib64/libcudart_static.a(libcudart_static.a.o): In function `cudart::cuosLoadLibraryUnsafe(char const*)':
(.text+0x52c13): undefined reference to `dlopen'
collect2: error: ld returned 1 exit status

显然,我遗漏了一些简单的东西。

更新。我试过了

g++ -c main.cpp;
nvcc -c kernel.cu;
g++ main.o kernel.o -o test -L/usr/local/cuda-10.2/lib64  -static -lcudadevrt -ldl -pthread -lrt -lcudart_static

g++ -c main.cpp;
nvcc -c kernel.cu;
g++ main.o kernel.o -o test -L/usr/local/cuda-10.2/lib64 -static -lcudart_static -lcudadevrt -ldl -lrt -pthread 

在这两种情况下,我都收到以下错误:

/usr/bin/ld: cannot find -lrt
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

更新 2.

什么对我有用

-lcudadevrt -lcudart_static -lrt -pthread -ldl -lc  -lpthread -Bstatic -lgcc -lstdc++ -ldf 

您需要使用 -L$(YOUR_PATH_TO_gclib)

link

您缺少 libdllibpthreadlibrt,将以下内容添加到您的 link 行:

-ldl -pthread -lrt

它们是 libcudart 的依赖项,用于检查其他 运行 ldd 在 cuda 共享对象文件上。


根据 OP 评论编辑。

此外,请注意使用 GCC linker,如果您不知道顺序依赖性(什么取决于什么),您将需要使用以下内容包装库依赖项:-Wl,--start-group -lmylib -lmyotherlib -Wl,--end-group .