Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang linker does not look into LD_LIBRARY_PATH

Tags:

c++

cmake

clang

ld

I am trying to build and link a C++, cmake-based project with clang (3.0). This project links to several libraries that are installed in a custom directory /my/dir/. This directory is included in the LD_LIBRARY_PATH and LIBRARY_PATH environment variables. Project builds and links fine with g++.

The link command generated and executed by cmake looks like the following:

/usr/bin/clang++ -O3 stuff.cpp.o -o stuff -rdynamic -lmylib

ld then complains with the following message:

/usr/bin/ld: cannot find -lmylib

The link command above runs fine whenever I manually add -L/my/dir/. Is there a way to link without specifying the -L flag?

like image 811
Régis B. Avatar asked Sep 25 '12 12:09

Régis B.


1 Answers

The $LD_LIBRARY_PATH environment variable (and its various alternatives on other UNIX-based platforms) is used at runtime, not link time, to find libraries.

Using -L is the correct approach and cannot be avoided.

Note: A better approach under Linux (you don't specify your platform so I'm guessing) is to correctly configure a file in /etc/ld.so.conf.d/ and avoid using $LD_LIBRARY_PATH altogether.

like image 153
trojanfoe Avatar answered Oct 30 '22 01:10

trojanfoe