3

I can never remember what to type when linking include files in GCC, in fact the only one I can remember is -lm for math.h. The one I am specifically concerned with right now is sys/time.h.

This page clears things up some, but I would still like a list.

Does anyone know of a good list of linking options?

EDIT:

Maybe my question was not clear. I want to know what I need to type at the command line (like -lm for math or -lpthread for pthread) for the various libraries I might need to link when making C programs.

ubiquibacon
  • 10,451
  • 28
  • 109
  • 179

2 Answers2

1

The functionality provided in <sys/time.h> is implemented in libc.so (C library). You don't need to link anything else in as gcc should automatically link to libc.so by itself. There is no 'linking of include files', rather you are linking against libraries that contain the symbols defined by code.

The -l flag is one of GCC's linker options and is used to specify additional libraries to link against.

edit because my gcc was performing optimizations on my source code at compile time

Also, the information in that link is a little outdated - you should not need an explicit link to libm (which is what -l m or -lm does) in modern GCC.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • Wow, when did that happen? http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c/1033940#1033940 I'm pretty sure that was accurate a year ago… – ephemient Nov 30 '10 at 21:49
  • @ephemient - `libstdc++` still links to `libm.so` on my system (With G++ 4.4.5), but I meant that you shouldn't need the `-lm` flag to pull it in at all with C code. – wkl Nov 30 '10 at 21:54
  • I'm just wondering when that changed. Testing with GCC 4.2.4, 4.3.5, 4.4.3, 4.4.5, and 4.5.1, along with glibc 2.11.1 and 2.12.1, I still need `-lm` for plain `gcc`. Which modern GCC do you mean? – ephemient Nov 30 '10 at 22:09
  • @ephemient - I'm using GCC 4.4.5 on Debian 5- it might just be a quirk with my version. I compiled C code using a bunch of the `math.h` functions, and compiled with just `gcc source.c`. I just tested on an RH5 machine with gcc 4.1.2 and it requires `-lm`. Time to edit my answer. – wkl Nov 30 '10 at 22:21
  • 1
    I wonder if it's architecture dependent. I've tested on 3 x86_64 machines: Gentoo, Debian sid (which comes with glibc 2.11.2, I miscounted in my earlier comment), Ubuntu Lucid. Also, are you sure you're actually linking the math functions? GCC's constant folding will fold the math functions too. – ephemient Nov 30 '10 at 22:30
  • @ephemient - that was it - gcc's floating point constants folding was the problem. If I compile with `gcc -frounding-math source.c` I get undefined reference errors. – wkl Nov 30 '10 at 22:36
0

I'm not sure i understand your question but -lm is not an ld option, -l is an option and -lx links libx.a (or .so, it depends). you might want to look at the ld manual for a full list of options.
I think all other standard libraries other than math are included in libc.so(.a) (-lc)

stnr
  • 435
  • 4
  • 14
  • `libcrypt`, `libdl`, `libpthread`, `libresolv`, and `librt` all contain more standard C/POSIX functions that aren't in `libc` and `libm`. Many people would consider `libz` to be a standard system library too. – ephemient Nov 30 '10 at 22:52