1

複数の.hファイルと.cuファイルを使用して静的ライブラリをコンパイルすると、未解決のextern関数が発生します。エラーを再現する短い例を次に示します。

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

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:

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


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

1 に答える 1

5

これを機能させるには、個別のコンパイルを明示的に有効にする必要があります。プロジェクトを右クリックし、[プロパティ]、[ビルド]、[CUDA]の順にクリックして、[個別のコンパイル]リンカーモードを選択します。

個別のコンパイルはSM2.0以降のGPUでのみ機能し、SASSのみを放出できることに注意してください(たとえば、将来のCUDAデバイスと互換性のあるPTXを放出することはできません)。詳細については、NVCCマニュアルの「CUDAでの個別コンパイルの使用」をお読みください。

更新 デバイスコードをリンクするにはNVCCリンカーを使用する必要があります。そのため、GCCリンカーは失敗します。Nsightでは、NVCCを使用してアプリケーション全体をリンクするか、すべてのCUDAコードを含み、NVCCトールチェーンで構築された静的ライブラリプロジェクトと、GCCを使用して最初のプロジェクトから生成された静的ライブラリとリンクする通常のC /C++プロジェクトをセットアップできます。

于 2012-11-29T21:53:47.763 に答える