库已链接,但引用未定义 [英] library is linked but reference is undefined

查看:179
本文介绍了库已链接,但引用未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Ubuntu上编译一个openCL程序,使用之前工作过的NVIDIA卡,

  #include< ; CL / cl.h> 
#include< iostream>
#include< vector>

using namespace std;

int main(){
cl_platform_id platform;
cl_device_id device;
cl_context context;
cl_command_queue command_queue;
cl_int error;

if(clGetPlatformIDs(1,& platform,NULL)!= CL_SUCCESS){
cout< 平台错误< endl;
}

if(clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,1,& device,NULL)!= CL_SUCCESS){
cout< 设备错误< endl;
}

context = clCreateContext(NULL,1,& device,NULL,NULL,& error);
if(error!= CL_SUCCESS){
cout<< 上下文错误< endl;
}

command_queue = clCreateCommandQueue(context,device,0,& error);
if(error!= CL_SUCCESS){
cout<< 命令队列错误< endl;
}

return 0;
}

我这样编译,

  G ++ -I在/ usr /本地/ CUDA /包括-L / usr / lib目录/ NVIDIA电流-lOpenCL opencl.cpp 



我得到这个结果

  / tmp /ccAdS9ig.o:在函数'主':
opencl.cpp :(文字+ 0X1A):未定义的参考`clGetPlatformIDs'
opencl.cpp :(文字+ 0x3D之间):未定义参考`clGetDeviceIDs'。
opencl.cpp :(文字+ 0x65)中:未定义引用`clCreateContext'
opencl.cpp :(文字+ 0x85未):未定义的参考`clCreateCommandQueue'
collect2 :ld返回1退出状态

nm -D / usr / lib / NVIDIA电流/ libOpenCL.so 告诉我,libOpenCL.so至少包含clGetPlatformIDs

  0000000000002400Ť clGetKernelWorkGroupInfo 
0000000000002140牛逼clGetMemObjectInfo
0000000000002e80牛逼clGetPlatformIDs
0000000000002de0牛逼clGetPlatformInfo
0000000000002310牛逼clGetProgramBuildInfo
00000000000022f0牛逼clGetProgramInfo
00000000000021f0牛逼clGetSamplerInfo

我失去了一些东西。


解决方案

gcc 手册页:

  -llibrary 
-l库
链接时搜索名为库的库。 (与库作为一个单独的参数的第二个选择是仅适用于符合POSIX标准,不建议使用。)

这使得其中的命令,你写这个选项是有所不同;链接器搜索和处理库和目标文件中它们被指定的顺序。因此,foo.o
-lz bar.o在文件foo.o之后但在bar.o之前搜索库z。如果bar.o引用z中的函数,那些函数可能无法加载。

链接器搜索库的标准目录列表,实际上是一个名为liblibrary.a的文件。链接器然后使用此文件,就好像它已经通过名称指定了

所以尝试指定 -lOpenCL 您的编译命令中的文件参数。



您还在libOpenCL.so中搜索符号,这是一个共享库文件。用你的命令你的程序再次链接一个静态库,格式 libOpenCL.a


I'm trying to compile an openCL program on Ubuntu with an NVIDIA card that worked once before,

#include <CL/cl.h>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  cl_platform_id platform;
  cl_device_id device;
  cl_context context;
  cl_command_queue command_queue;
  cl_int error;

  if(clGetPlatformIDs(1, &platform, NULL) != CL_SUCCESS) {
    cout << "platform error" << endl;
  }

  if(clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL) != CL_SUCCESS) {
    cout << "device error" << endl;
  }

  context = clCreateContext(NULL, 1, &device, NULL, NULL, &error);
  if(error != CL_SUCCESS) {
    cout << "context error" << endl;
  }

  command_queue = clCreateCommandQueue(context, device, 0, &error);
  if(error != CL_SUCCESS) {
    cout << "command queue error" << endl;
  }

  return 0;
}

I compile it like so,

g++ -I/usr/local/cuda/include -L/usr/lib/nvidia-current -lOpenCL opencl.cpp

and I get this result

/tmp/ccAdS9ig.o: In function `main':
opencl.cpp:(.text+0x1a): undefined reference to `clGetPlatformIDs'
opencl.cpp:(.text+0x3d): undefined reference to `clGetDeviceIDs'
opencl.cpp:(.text+0x65): undefined reference to `clCreateContext'
opencl.cpp:(.text+0x85): undefined reference to `clCreateCommandQueue'
collect2: ld returned 1 exit status

but nm -D /usr/lib/nvidia-current/libOpenCL.so tells me that libOpenCL.so at least contains clGetPlatformIDs

0000000000002400 T clGetKernelWorkGroupInfo
0000000000002140 T clGetMemObjectInfo
0000000000002e80 T clGetPlatformIDs
0000000000002de0 T clGetPlatformInfo
0000000000002310 T clGetProgramBuildInfo
00000000000022f0 T clGetProgramInfo
00000000000021f0 T clGetSamplerInfo

Am I missing something.

解决方案

From the gcc man page:

   -llibrary
   -l library
       Search the library named library when linking.  (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

       It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified.  Thus, foo.o
       -lz bar.o searches library z after file foo.o but before bar.o.  If bar.o refers to functions in z, those functions may not be loaded.

       The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a.  The linker then uses this file as if it had been specified
       precisely by name.

So try to specify the -lOpenCL after the file argument in your compile command.

You also search for symbols in libOpenCL.so, which is a shared library file. With your command you link your program agains a static library, in the format libOpenCL.a.

这篇关于库已链接,但引用未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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