Currently trying to compile a simple program given in OpenCL from this website. It will give me the required DeviceInfo that I need. After simply invoking a make, I get the error below:

sharan@sharan-X550CC:~/opencl-ex/Ex1$ make 
g++ DeviceInfo.cpp -I ../../Cpp_common  -lOpenCL -o DeviceInfo
/usr/bin/ld: cannot find -lOpenCL
collect2: error: ld returned 1 exit status
Makefile:23: recipe for target 'DeviceInfo' failed
make: *** [DeviceInfo] Error 1

Now I have installed using the instructions from this website. However, I still the get the error above.

How can I solve this error?


You linker can't find the OpenCL library.

You should help the linker to find the OpenCL library.

Similar issue was raised here

The solution there was to make a link for the library to a known lib location:

sudo ln -s /usr/lib/x86_64-linux-gnu/libOpenCL.so.1 /usr/lib/libOpenCL.so

Another option:

Assuming that OpenCL library located in /usr/lib/x86_64-linux-gnu/ you can also add the library folder to the Libraries path:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu/"

You may need to update the "Dynamic Linker":

sudo ldconfig
  • 1
    This worked for me! The very first answer. Thank you! – SDG Feb 21 '18 at 6:14

As already stated by Yaron the linker does not know where to find the OpenCL library, i.e. it is in none of the places it looks for it.

Instead of moving it to one of those places (e.g. /usr/lib) I would suggest to inform the linker where to look for it via the -L flag. The command would then read (note the -L/usr/lib/x86_64-linux-gnu)

g++ DeviceInfo.cpp -I ../../Cpp_common -L/usr/lib/x86_64-linux-gnu -lOpenCL -o DeviceInfo

If you are using a handwritten Makefile you can simply modify the compiler/linker command like this. Otherwise you will have to touch your build system how to include it.

  • or try LDFLAGS e.g. make 'LDFLAGS=-L/usr/lib/x86_64-linux-gnu' – steeldriver Feb 19 '18 at 13:49
  • If your looking for a purely temporary solution this will also do the trick. However I advise to make it LDFLAGS=$LDFLAGS:/usr/lib/x86_64-linux-gnu/ to preserve any previous settings – mbeyss Feb 19 '18 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.