Originally Posted by
rags_to_riches
-lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.
gcc(1): GNU project C/C++ compiler - Linux man page
This does not have so much to do with gcc as it does with the linker.
LinuxSmith, have a look at the ld(1) man page, either on your own system, or here. Do a search for this (read the paragraph that follows it):
-l namespec
--library=namespec
Originally Posted by
LinuxSmith
So where might I find a listing of all common pre-defined library names then?
It is not so simple as that - for any library that is not standard, you will have to use the -l switch to tell the linker to look for something that is not standard. For example, when compiling OpenGL (with SDL) stuff under linux, you would see lots of -l switches:
Code:
-lglut -lXext -lX11 -lXmu -lXi -lGL -lGLU -lm -lSDL -lSDL_mixer
rags_to_riches is correct though - the man pages for the functions you are using ought to tell you when you need to use a switch like that.
As an aside, compile the following program, (using the -lm switch), and then run it with ldd.
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double base = 2.0;
double exp = 3.0;
printf("2^3=%.0f\n", pow(base, exp));
return 0;
}
As an example, this is what I get:
Code:
~/cprogs/board> gcc -Wall -Wextra -o demo -lm demo.c
~/cprogs/board> ldd ./demo
linux-vdso.so.1 => (0x00007fff5bfff000)
libm.so.6 => /lib64/libm.so.6 (0x00007f7d9ca63000)
libc.so.6 => /lib64/libc.so.6 (0x00007f7d9c703000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7d9ccba000)
~/cprogs/board>
As you can see, my program needed libm, which is why I needed to use -lm.
I am not sure why math.h is considered non-standard - it seems to me that this is a carry over from old Unix behaviour that was long since unnecessary. You can read a little more about some of those quirks (though no explanation as to why) in the FAQ (follow the links through as well).
Edit: There is a discussion on stackoverflow about why we still need to manually link libm. Lots of ideas to consider, if nothing else. Read it here.