use*_*457 34 c++ ubuntu linker opencl
我正在尝试用UIDntu编译一个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 << "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;
}
Run Code Online (Sandbox Code Playgroud)
我像这样编译它,
g++ -I/usr/local/cuda/include -L/usr/lib/nvidia-current -lOpenCL opencl.cpp
Run Code Online (Sandbox Code Playgroud)
我得到了这个结果
/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
Run Code Online (Sandbox Code Playgroud)
但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
Run Code Online (Sandbox Code Playgroud)
我错过了什么.
小智 32
当您进行链接时,库和源文件的顺序会有所不同.例如,对于你的情况,
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
Run Code Online (Sandbox Code Playgroud)
然后在OpenCL库中找到任何函数请求,它们将被加载.
Geo*_*roy 27
从gcc
手册页:
-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.
Run Code Online (Sandbox Code Playgroud)
因此,请尝试-lOpenCL
在compile命令中指定file参数之后.
您还可以在libOpenCL.so中搜索符号,这是一个共享库文件.使用您的命令,您可以使用格式将程序再次链接到静态库libOpenCL.a
.
就我而言,我编译了一个 C++ 应用程序,但链接了一个 C 库。该库包含的头文件未将函数原型指定为 extern“C”,因此链接器正在搜索装饰函数名称而不是纯 C 名称。在 C 头周围指定 extern "C" 包括为我解决了这个问题。
extern "C" {
#include "cheader.h"
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46292 次 |
最近记录: |