红联Linux门户
Linux帮助

gcc with math lib in ubuntu

发布时间:2008-07-10 16:28:02来源:红联作者:zbatecn
It compiles your program without errors, BUT the linker (ld) fails because you do not link it to the math library "libm.so" (where the actual math functions reside).

Use the -l argument and the library name "m" to link your program.

You can do it in 2 steps:

First, compile the code (but do not link to external libraries). This produces pure object code (.o) but's not executable. (study: ELF format).
$ gcc -c yourcode.c -o yourcode.o

Link the program by putting together all object files (*.o) and external libraries (*.so). The result is an executable program. (study: ELF format).
Gcc calls the "ld" linker module to do the job.

$ gcc yourcode.o -lm -o yourcode

Run it
$ ./yourcode
---

Or do it all (compile + link) on one line
$ gcc yourcode.c -lm -o yourcode
---

Notice:
1) The /usr/include/math.h file includes signatures (prototypes) of all math functions, but the actual code resides in the libm.so lib. That's why your code must link to it.
--
2) Dynamic vs. Static lib:
Library names with ending .so (such as libm.so) are called dynamic libraries. These are equivalent to Microsoft Windows' DLL. Dynamic means that you link to library at runtime (when you start a program). The size of executable is therefore small but the required external libraries must exist in the system when you install and run the program.

! Linux creates a cache over dynamic libraries (*.so) by running the "ldconfig" command. This cache makes runtime loading and linking very quick.
See /etc/ld.so.conf file. Run also "sudo ldconfig -vv" and "man ldconfig".

Library names with ending .a (such as libm.a) are called static libraries. Static means that the code from library is baked into the executable at compile time. This makes the size of executable notably bigger but the executable is self-contained, it does not require external libraries at installation or when you start the program. It has everything within.
文章评论

共有 0 条评论