Unable to resolve link error

Hi!

I’ve just started learning to program in OpenCL, but I’m getting a link error that I’m unable to resolve. I believe I’ve installed everything properly, it’s probably just an error in the way I’ve coded the gcc command. I’m running under Ubuntu 1910 (64-bit), with an Nvidia graphics card. The program just calls clGetPlatformIDs() and prints the results. Here’s the console output:

david$ ls -l /usr/lib/x86_64-linux-gnu/libOpenCL.*
lrwxrwxrwx 1 root root    18 Apr  5  2017 /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 -> libOpenCL.so.1.0.0
-rw-r--r-- 1 root root 43072 Apr  5  2017 /usr/lib/x86_64-linux-gnu/libOpenCL.so.1.0.0
david$ nm -D /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 | grep -i clgetplatform
0000000000005010 T clGetPlatformIDs
00000000000060b0 T clGetPlatformInfo
david$ gcc -DCL_TARGET_OPENCL_VERSION=120 detect.c -L/usr/lib/x86_64-linux-gnu/libOpenCL.so.1
/usr/bin/ld: /tmp/ccCIjSXP.o: in function `main':
detect.c:(.text+0x2d): undefined reference to `clGetPlatformIDs'
collect2: error: ld returned 1 exit status

Here’s the source code:

#include <stdio.h>
#include <CL/cl.h>

int main(void)
{
    cl_int err;
    cl_uint numPlatforms;
    err = clGetPlatformIDs(0, NULL, &numPlatforms);
    if (CL_SUCCESS == err)
         printf("\nDetected OpenCL platforms: %d", numPlatforms);
    else
         printf("\nError calling clGetPlatformIDs. Error code: %d", err);
    return 0;
}

As you can see, the library is installed, but the linker is unable to resolve the reference to clGetPlatformIDs(). Any suggestions?

Thanks.

with the “-L” flag you set the search path for libraries. to use a specific library you have to use the “-l” flag
and usually you dont use the full path with “-l”. try “-lOpenCL”

1 Like

Yes, I tried that too, but I got a different error (library not found). I tried specifying the file name in full, as well as the abbreviation (“OpenCL”). I also tried both the symbolic link (‘libOpenCL.so.1’ and the actual file (‘libOpenCL.so.1.0.0’). Could it be an issue with dynamic vs. static link libraries?

david$ ls -l /usr/lib/x86_64-linux-gnu/libOpenCL*
lrwxrwxrwx 1 root root    18 Apr  5  2017 /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 -> libOpenCL.so.1.0.0
-rw-r--r-- 1 root root 43072 Apr  5  2017 /usr/lib/x86_64-linux-gnu/libOpenCL.so.1.0.0
david$ gcc -DCL_TARGET_OPENCL_VERSION=120 detect.c -L/usr/lib/x86_64-linux-gnu -lOpenCL
/usr/bin/ld: cannot find -lOpenCL
collect2: error: ld returned 1 exit status
david$ gcc -DCL_TARGET_OPENCL_VERSION=120 detect.c -L/usr/lib/x86_64-linux-gnu -llibOpenCL.so.1.0.0
/usr/bin/ld: cannot find -llibOpenCL.so.1.0.0
collect2: error: ld returned 1 exit status
david$ gcc -DCL_TARGET_OPENCL_VERSION=120 detect.c -L/usr/lib/x86_64-linux-gnu -llibOpenCL.so.1
/usr/bin/ld: cannot find -llibOpenCL.so.1
collect2: error: ld returned 1 exit status
david$ 

Thanks.

I found the problem. The symbolic link to the library had a suffix of ‘.1’, which prevented the linker from finding the library. After renaming it from ‘libOpenCL.so.1’ to ‘libOpenCL.so’, I was able to compile and link with the following command (as you suggested):

$ gcc -DCL_TARGET_OPENCL_VERSION=120 detect.c -L/usr/lib/x86_64-linux-gnu -lOpenCL

I’m not sure why it had that suffix (it was installed with that name), but anyway…

Thanks for your help!