22

I am using Ubuntu and gcc and g++ were working fine but today it showed:

cannot find -lm
cannot find -lc

I searched and found it has something to do with /usr/bin/ld. Which is a symlink (I hope) to lbd.bdf. I pasted that file in the directory from Ubuntu of some friends PC. It didn't work.

I found that -lc means include static library libc.a.
similarly for -lm

I found them in my i386-linux-folders (name was something different).

I tried code blocks but same errors.

RMPR
  • 3,368
  • 4
  • 19
  • 31
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95

3 Answers3

33

The compiler cannot find static glibc, you might have installed only the shared libraries, try:

yum install glibc-static

RMPR
  • 3,368
  • 4
  • 19
  • 31
user1772382
  • 536
  • 6
  • 5
7

make sure that your libpath (in g++) points to the directory(ies) that libm.a and libc.a are located in (use the -L option)

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • How to view current libpath of my g++? – osgx Sep 02 '11 at 13:25
  • @KevinDTimm I never did that before and it worked fine... Now what is the need ? always did g++ -o output_fileName cpp_fileName. Worked fine. – Ashish Negi Sep 02 '11 at 13:55
  • Your LIBRARY_PATH is fouled up (see http://linux.die.net/man/1/gcc, search for LIBRARY_PATH) – KevinDTimm Sep 02 '11 at 14:11
  • If liba and libm are not in the default location then there is a bigger problem. Setting -L is unlikely to help. – Martin York Sep 02 '11 at 15:51
  • I m sorry if i sounded like that.. but i think you are right about LIBRARY_PATH . echo ${LIBRARY_PATH} showed nothing. and libc.a is at /usr/lib/i386-linux-gnu/libc.a /usr/lib/i386-linux-gnu/xen/libc.a and libm.a is at /usr/lib/i386-linux-gnu/libm.a /usr/lib/i386-linux-gnu/xen/libm.a – Ashish Negi Sep 02 '11 at 16:49
  • So, if you update LIBRARY_PATH to /usr/lib/i386-linux-gnu, does your compilation work? – KevinDTimm Sep 02 '11 at 16:53
  • My LIBRARY_PATH was distorted. export LIBRARY_PATH=/usr/lib/i386-linux-gnu well the value of path would depend upon your libc.a and libm.a file location. Thanks to KevinDTimm – Ashish Negi Sep 02 '11 at 17:16
0

ld is the GNU linker.

man ld ld combines a number of object and archive files, relocates their data and ties up symbol references. Usually the last step in compiling a program is to run ld.

It is uses to link your program with the C library and the C math library. You need to make sure that libc6-dev is installed:

foo@bar: $ dpkg -s libc6-dev

Or more generic, ensure build-essential, which depends on a handful of essential C packages.

foo@bar: $ dpkg -s build-essential
Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • note that the OP has no concept of `ld`. When you talk only about `ld` the onus is on you to explain to him how these (`g++` and `ld`) are related. – KevinDTimm Sep 02 '11 at 13:33
  • @KevinDTimm: Same for libpath, eh? Well, I don't know how much of programming and *nix the OP understands already, but if you get a basic book on C++, it usually exaplains the concepts behind compilation (basically preprocessing, compiling, linking). I think if you read such book, then reading `ld is the linker`, than it will already be of help. – Sebastian Mach Sep 03 '11 at 06:33