使用多个 .h 和 .cu 文件编译静态库时,我得到一个未解析的外部函数。这是一个复制错误的简短示例。
看来我无法让 Nsight Eclipse 版先编译 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 ****
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:
#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);
}
额外功能.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:
#include <cuda.h>
#include <cuda_runtime.h>
#include "extrafunctions.h"
int __device__ increment(int i){
return i+1;
}