c++ - 库已链接,但引用未定义

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

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

我是这样编译的,

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

我得到了这个结果

/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

但是 nm -D/usr/lib/nvidia-current/libOpenCL.so 告诉我 libOpenCL.so 至少包含 clGetPlatformIDs

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

我是不是错过了什么。

最佳答案

链接时,库和源文件的顺序会有所不同。例如对于您的情况,

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

OpenCL 库中定义的函数可能不会被加载,因为在它们之前没有任何东西要求查找。但是,如果您使用,

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

那么任何对函数的请求都会在 OpenCL 库中找到并被加载。

https://stackoverflow.com/questions/7783345/

相关文章:

c++ - 如何使用 C++ 模板减少编译时间

c++ - 如何在 Windows 上找到 Qt5 CMake 模块

c++ - 析构函数可以是最终的吗?

c++ - C++ 标准是否对 float 的表示做了任何规定?

c++ - 与 win32 CRITICAL_SECTION 相比的 std::mutex 性能

c++ - "&s[0]"是否指向 std::string 中的连续字符?

c++ - 编译lua代码,存储字节码然后加载并执行

c++ - 三元表达式中的逗号混淆

c++ - vector 的 std::remove 和删除之间的区别?

c++ - 什么是 C++ 内联类?