ld cannot find an existing library

LinuxGccLinkerShared LibrariesShared Objects

Linux Problem Overview


I am attempting to link an application with g++ on this Debian lenny system. ld is complaining it cannot find specified libraries. The specific example here is ImageMagick, but I am having similar problems with a few other libraries too.

I am calling the linker with:

g++ -w (..lots of .o files/include directories/etc..) \
-L/usr/lib -lmagic

ld complains:

/usr/bin/ld: cannot find -lmagic

However, libmagic exists:

$ locate libmagic.so
/usr/lib/libmagic.so.1
/usr/lib/libmagic.so.1.0.0
$ ls -all /usr/lib/libmagic.so.1*
lrwxrwxrwx 1 root root    17 2008-12-01 03:52 /usr/lib/libmagic.so.1 -> libmagic.so.1.0.0
-rwxrwxrwx 1 root root 84664 2008-09-09 00:05 /usr/lib/libmagic.so.1.0.0
$ ldd /usr/lib/libmagic.so.1.0.0 
	linux-gate.so.1 =>  (0xb7f85000)
	libz.so.1 => /usr/lib/libz.so.1 (0xb7f51000)
	libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb7df6000)
	/lib/ld-linux.so.2 (0xb7f86000)
$ sudo ldconfig -v | grep "libmagic"
	libmagic.so.1 -> libmagic.so.1.0.0

How do I diagnose this problem further, and what could be wrong? Am I doing something completely stupid?

Linux Solutions


Solution 1 - Linux

The problem is the linker is looking for libmagic.so but you only have libmagic.so.1

A quick hack is to symlink libmagic.so.1 to libmagic.so

Solution 2 - Linux

As just formulated by grepsedawk, the answer lies in the -l option of g++, calling ld. If you look at the man page of this command, you can either do:

  • g++ -l:libmagic.so.1 [...]
  • or: g++ -lmagic [...] , if you have a symlink named libmagic.so in your libs path

Solution 3 - Linux

It is Debian convention to separate shared libraries into their runtime components (libmagic1: /usr/lib/libmagic.so.1 → libmagic.so.1.0.0) and their development components (libmagic-dev: /usr/lib/libmagic.so → …).

Because the library's soname is libmagic.so.1, that's the string that gets embedded into the executable so that's the file that is loaded when the executable is run.

However, because the library is specified as -lmagic to the linker, it looks for libmagic.so, which is why it is needed for development.

See Diego E. Pettenò: Linkers and names for details on how this all works on Linux.


In short, you should apt-get install libmagic-dev. This will not only give you libmagic.so but also other files necessary for compiling like /usr/include/magic.h.

Solution 4 - Linux

In Ubuntu, you can install libtool which resolves the libraries automatically.

$ sudo apt-get install libtool

This resolved a problem with ltdl for me, which had been installed as libltdl.so.7 and wasn't found as simply -lltdl in the make.

Solution 5 - Linux

As mentioned above the linker is looking for libmagic.so, but you only have libmagic.so.1.

To solve this problem just perform an update cache.

ldconfig -v 

To verify you can run:

$ ldconfig -p | grep libmagic

Solution 6 - Linux

Unless I'm badly mistaken libmagic or -lmagic is not the same library as ImageMagick. You state that you want ImageMagick.

ImageMagick comes with a utility to supply all appropriate options to the compiler.

Ex:

g++ program.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs` -o "prog"

Solution 7 - Linux

Installing libgl1-mesa-dev from the Ubuntu repo resolved this problem for me.

Solution 8 - Linux

I tried all solutions mentioned above but none of them solved my issue but finally I solved it with the following command.

sudo apt-get install libgmp3-dev

This will do the magic.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmaxpenguinView Question on Stackoverflow
Solution 1 - LinuxgrepsedawkView Answer on Stackoverflow
Solution 2 - LinuxPiotr LesnickiView Answer on Stackoverflow
Solution 3 - LinuxephemientView Answer on Stackoverflow
Solution 4 - LinuxMr EdView Answer on Stackoverflow
Solution 5 - LinuxMiguel Ignacio IglesiasView Answer on Stackoverflow
Solution 6 - LinuxBrian GianforcaroView Answer on Stackoverflow
Solution 7 - LinuxkirenpillayView Answer on Stackoverflow
Solution 8 - Linuxnft3000View Answer on Stackoverflow