180

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?

maxpenguin
  • 5,039
  • 6
  • 28
  • 22

9 Answers9

182

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

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
grepsedawk
  • 5,959
  • 5
  • 24
  • 22
  • 3
    that works, I am kind of perplexed that it would name the file in a completely useless way by default - can you provide any insight why it would do this by default? – maxpenguin Dec 03 '08 at 01:09
  • Most likely it is a misconfiguration of the install script – grepsedawk Dec 03 '08 at 01:15
  • 7
    The foo.so.1 is a symlink to foo.so.1.0.0 too. This way, you can have several versions of a library in your system, and if an application needs a specific one, it can link to it, while in general, the newest one is chosen by symlink. I do not know why this symlink was missing. – Svante Dec 03 '08 at 01:19
  • 52
    libmagic.so.1 is the soname, used by the dynamic linker; libmagic.so is used by the linker, and is usually together with the headers in the -dev package. The symlink might be missing because the -dev package was not installed. – CesarB Dec 03 '08 at 10:49
  • 14
    I had the same problem... instead of doing the "hack" I installed the "*-devel" version and it fixed the compilation. – Trevor Boyd Smith Apr 27 '12 at 19:21
  • 1
    Same problem here and got crazy for a day... Actually it is better to let libraries hanle everything and, as Trevor stated, install devel packages... – Andry Oct 17 '13 at 07:32
  • 4
    How to symlink libmagic.so.1 to libmagic.so? And where to find those "devel" packages – Black Aug 26 '15 at 11:56
  • @Black `ln -s path-to-libmagic.so.1/libmagic.so.1 path-to-libmagic.so.1/libmagic.so`. Example: `ln -s /usr/lib/x86_64-linux-gnu/libzstd.so.1 /usr/lib/x86_64-linux-gnu/libzstd.so` – MuhsinFatih Jun 22 '21 at 02:06
70

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
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Piotr Lesnicki
  • 9,442
  • 2
  • 28
  • 26
  • Or in summary, remove the `lib` prefix when linking to it when using `-l`. `-llibmagic` should be `-lmagic`. – phyatt Dec 20 '16 at 00:05
35

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.

ephemient
  • 198,619
  • 38
  • 280
  • 391
10

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.

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
Mr Ed
  • 5,068
  • 1
  • 19
  • 12
  • it did not solve the error **cannot fine -LGL** . could you please give more information on what libtool does and how it solves library issues? – Shahryar Saljoughi May 09 '17 at 10:06
8

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
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39
4

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"
Yamaneko
  • 3,433
  • 2
  • 38
  • 57
Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
1

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

kirenpillay
  • 339
  • 1
  • 5
  • 7
1

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.

jwvh
  • 50,871
  • 7
  • 38
  • 64
nft3000
  • 11
  • 5
0

Another way to solve this problem is to install the -devel package.

If the compiler is looking for libabc.so while you have libabc.so.1, you need to install the -devel package like libabc-devel as libabc.so.1 is a runtime lib but libabc.so is a development lib.

thd
  • 2,380
  • 1
  • 25
  • 33