unresolved external symbol的解决方法

Posted Clotho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unresolved external symbol的解决方法相关的知识,希望对你有一定的参考价值。

有多种原因导致,可选方法如下:

1.右键项目 -> Properties -> C/C++ -> Code Generation -> Runtime Library

有4个选项:MT、MTd、MD、MDd。

第2个字母,T表示静态运行库(.lib)、D表示动态运行库(.dll)

第3个字母,d表示debug模式,不写表示release模式

如果报错"unresolved external symbol",可以选择MTd或MDd试试。

 

2.右键项目 -> Properties -> Linker -> System -> SubSystem

把"Console (/SUBSYSTEM:CONSOLE)"改成"Windows (/SUBSYSTEM:WINDOWS)"试试

 

3.右键项目 -> Properties -> General -> Configuration Type

把"Application (.exe)"改成"Static library (.lib)"或"Dynamic Library (.dll)"试试

 

ptxas fatal : Unresolved extern function Error 255

This question already has an answer here:

I am trying to understand how to decouple CUDA __device__ codes in separate header files.

I have three files.

File: 1: int2.cuh

#ifndef INT2_H_
#define INT2_H_

#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

__global__ void kernel();
__device__ int k2(int k);

int launchKernel(int dim);

#endif /* INT2_H_ */
File 2: int2.cu

#include "int2.cuh"
#include "cstdio"

__global__ void kernel() {
    int tid = threadIdx.x;
    printf("%d\n", k2(tid));
}

__device__ int k2(int i) {
    return i * i;
}

int launchKernel(int dim) {
    kernel<<<1, dim>>>();
    cudaDeviceReset();
    return 0;
}
File 3: CUDASample.cu

include <stdio.h>
#include <stdlib.h>
#include "int2.cuh"
#include "iostream"

using namespace std;

static const int WORK_SIZE = 256;

__global__ void sampleCuda() {
    int tid = threadIdx.x;
//    printf("%d\n", k2(tid)); //Can not call k2
    printf("%d\n", tid * tid);
}

int main(void) {

    int var;
    var = launchKernel(16);

    kernel<<<1, 16>>>();
    cudaDeviceReset();

    sampleCuda<<<1, 16>>>();
    cudaDeviceReset();

    return 0;
}

 

The code works file. I can call the sampleCuda() kernel (in same file), call the C function launchKernel() (in other file), and call kernel() directly (in other file).

The problem I am facing is calling the __device__ function from sampleCuda() kernel. then it shows the following error. However, the same function is callable in kernel().

10:58:11 **** Incremental Build of configuration Debug for project CUDASample ****
make all 
Building file: ../src/CUDASample.cu
Invoking: NVCC Compiler
/Developer/NVIDIA/CUDA-6.5/bin/nvcc -G -g -O0 -gencode arch=compute_20,code=sm_20  -odir "src" -M -o "src/CUDASample.d" "../src/CUDASample.cu"
/Developer/NVIDIA/CUDA-6.5/bin/nvcc -G -g -O0 --compile --relocatable-device-code=false -gencode arch=compute_20,code=compute_20 -gencode arch=compute_20,code=sm_20  -x cu -o  "src/CUDASample.o" "../src/CUDASample.cu"
../src/CUDASample.cu(18): warning: variable "var" was set but never used

../src/CUDASample.cu(8): warning: variable "WORK_SIZE" was declared but never referenced

../src/CUDASample.cu(18): warning: variable "var" was set but never used

../src/CUDASample.cu(8): warning: variable "WORK_SIZE" was declared but never referenced

ptxas fatal   : Unresolved extern function _Z2k2i
make: *** [src/CUDASample.o] Error 255

10:58:14 Build Finished (took 2s.388ms)

 




上面这个错误的解决方法都是:需要分开编译文件,对应的编译参数为--relocatable-device-code={true,false}将false 改为 true
nsight 的设置的位置为:
In Nsight Eclipse, the option is available as the radio button "Separate compilation" under Project > Properties > Build > Settings > CUDA > switch to advanced mode 里面勾选就可以了。
VS的设置位置为:刚才突然想写。。找不到了。。具体google 搜索吧,best wishes....

以上是关于unresolved external symbol的解决方法的主要内容,如果未能解决你的问题,请参考以下文章

unresolved external symbol的解决方法

unresolved external symbol 问题

[ilink32 Error] Error: Unresolved external 'SendARP'

C语言unresolved external symbol ?

MFC 编译链接错误:unresolved external symbol

出现fatal error LNK1120: 1 unresolved externals