具有多个.h和.cu文件的静态库无法解析函数 [英] Static library with multiple .h and .cu files can't resolve functions

查看:301
本文介绍了具有多个.h和.cu文件的静态库无法解析函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When compiling a static library with multiple.h and .cu files I get an unresolved extern function. Here is a short example that replicates the error.

似乎我无法获得Nsight Eclipse版本编译extrafunctions.cu。在我的完整项目中,首先编译具有额外函数的文件,但仍然会导致无法解析外部函数错误。

It appears that I can't get Nsight Eclipse Edition to compile extrafunctions.cu first. In my full project the file with extra functions is compiled first but it still throws the unable to resolve external function error.

此示例:

**** 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 ****

cudatest.h:

cudatest.h:

#ifndef CUDAPATH_H_
#define CUDAPATH_H_

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

void test();


#endif /* CUDAPATH_H_ */

cudatest。 cu:

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);
}

extrafunctions.h:

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_ */

extrafunctions.cu:

extrafunctions.cu:

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


int __device__ increment(int i){
    return i+1;
}


推荐答案

编译为此工作。右键单击您的项目属性,Build-> CUDA并选择单独编译链接器模式。

You need to explicitly enable separate compilation for this to work. Right-click your project, "Properties", Build->CUDA and select "Separate compilation" linker mode.

请注意,单独的编译只适用于SM 2.0+并且只能发出SASS(例如,不可能发射将与未来的CUDA设备兼容的PTX)。有关详细信息,请参阅 NVCC手册

Please note that separate compilation only works on SM 2.0+ GPUs and can only emit SASS (e.g. it is not possible to emit PTX that will be compatible with future CUDA devices). For more information please read "Using Separate Compilation in CUDA" in NVCC manual.

更新
您需要使用NVCC链接器链接设备代码,这就是为什么GCC链接器失败。在Nsight中,您可以使用NVCC链接整个应用程序,或者设置一个包含所有CUDA代码的静态库项目,并且使用NVCC托管链建立一个常规C / C ++项目,并使用GCC并与第一个项目生成的静态库链接。

Update You need to use NVCC linker to link device code, that is why GCC linker fails. In Nsight you can either link the whole application using NVCC or setup a static library project that contains all CUDA code and is built with NVCC tollchain and a regular C/C++ project that uses GCC and links with the static library produced from the first project.

这篇关于具有多个.h和.cu文件的静态库无法解析函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆