Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ld: library not found

Tags:

build

linker

ld

I'm trying to build a project that depends on SDL2 library. I've installed and linked it using homebrew:

> ls /usr/local/lib | grep SDL2
libSDL2-2.0.0.dylib
libSDL2.a
libSDL2.dylib
libSDL2_test.a
libSDL2main.a

I also added /usr/local/lib to my /etc/paths and ~/.bash_profile as well:

> cat /etc/paths
/usr/local/lib
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

However, when I try to build the project, I still get this error:

error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' (...) '-lSDL2'
ld: library not found for -lSDL2
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why does it happen and how can I fix it?

like image 524
Max Yankov Avatar asked Oct 17 '14 17:10

Max Yankov


People also ask

What is LD command in Mac?

Description. The ld command, also called the linkage editor or binder, combines object files, archives, and import files into one output object file, resolving external references. It produces an executable object file that can be run.


2 Answers

I fixed the issue by adding /usr/local/lib to my $LIBRARY_PATH:

For bash, in ~/.bash_profile:

export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/lib"

And for fish shell, in ~/.config/fish/config.fish:

set -g -x LIBRARY_PATH $LIBRARY_PATH /usr/local/lib
like image 176
Max Yankov Avatar answered Oct 25 '22 03:10

Max Yankov


/etc/paths is for executable files, not shared libraries. Same with the $PATH environmental variable set in .bash_profile. These are the paths searched for programs when you type a command in the terminal.

What you need to do is change the linkers link path. See the answer to this question for details on how to set up the link path.

like image 38
mp_ Avatar answered Oct 25 '22 04:10

mp_