具有多个.h和.cu文件的静态库无法解析函数

Sea*_*ean 1 c++ cuda header-files static-libraries unresolved-external

使用multiple.h和.cu文件编译静态库时,我得到一个未解析的extern函数.这是一个复制错误的简短示例.

看来我不能让Nsight Eclipse Edition先编译extrafunctions.cu.在我的整个项目有额外功能的文件编译第一,但它仍然抛出无法解决外部函数错误.

以下是此示例的输出:

**** Build of configuration Debug for project linkerror ****

make all 
Building file: ../cudatest.cu
Invoking: NVCC Compiler
nvcc -I/usr/local/cuda/include -G -g -O0 -gencode arch=compute_30,code=sm_30 -odir "" -M -o "cudatest.d" "../cudatest.cu"
nvcc --compile -G -I/usr/local/cuda/include -O0 -g -gencode arch=compute_30,code=compute_30 -gencode arch=compute_30,code=sm_30  -x cu -o  "cudatest.o" "../cudatest.cu"
../cudatest.cu(19): warning: variable "devInts" is used before its value is set

../cudatest.cu(19): warning: variable "devInts" is used before its value is set

ptxas fatal   : Unresolved extern function '_Z9incrementi'
make: *** [cudatest.o] Error 255

**** Build Finished ****
Run Code Online (Sandbox Code Playgroud)

cudatest.h:

#ifndef CUDAPATH_H_
#define CUDAPATH_H_

#include <cuda.h>
#include <cuda_runtime.h>
#include "extrafunctions.h"

void test();


#endif /* CUDAPATH_H_ */
Run Code Online (Sandbox Code Playgroud)

cudatest.cu:

#include <cuda.h>
#include <cuda_runtime.h>
#include "extrafunctions.h"

__global__ void kernel(int* devInts){
    int tid = threadIdx.x + (blockDim.x*blockIdx.x);

    if (tid == 0){
        for(int i = 0; i < NUMINTS; i++){
            devInts[i] = increment(devInts[i]);
        }
    }
}

void test(){

    int* myInts = (int*)malloc(NUMINTS * sizeof(int));
    int* devInts;
    cudaMemcpy((void**)devInts, myInts, NUMINTS*sizeof(int), cudaMemcpyHostToDevice);
    kernel<<<1,1>>>(devInts);
    int* outInts = (int*)malloc(NUMINTS * sizeof(int));
    cudaFree(devInts);
    free(myInts);
    free(outInts);
}
Run Code Online (Sandbox Code Playgroud)

extrafunctions.h:

#ifndef EXTRAFUNCTIONS_H_
#define EXTRAFUNCTIONS_H_

#include <cuda.h>
#include <cuda_runtime.h>

#define NUMINTS 4

int __device__ increment(int i);

#endif /* EXTRAFUNCTIONS_H_ */
Run Code Online (Sandbox Code Playgroud)

extrafunctions.cu:

#include <cuda.h>
#include <cuda_runtime.h>
#include "extrafunctions.h"


int __device__ increment(int i){
    return i+1;
}
Run Code Online (Sandbox Code Playgroud)

Eug*_*ene 5

您需要显式启用单独的编译才能生效.右键单击项目"Properties",Build-> CUDA并选择"Separate compilation"链接器模式.

请注意,单独的编译仅适用于SM 2.0+ GPU,并且只能发出SASS(例如,无法发出与未来CUDA设备兼容的PTX).有关更多信息,请阅读NVCC手册中的 "在CUDA中使用单独编译" .

更新 您需要使用NVCC链接器链接设备代码,这就是GCC链接器失败的原因.在Nsight中,您可以使用NVCC链接整个应用程序,也可以设置包含所有CUDA代码的静态库项目,并使用NVCC Tollchain和使用GCC的常规C/C++项目以及与第一个项目生成的静态库链接.