Includes Linux GCC Linker

I do not understand how GCC works under Linux. In the source file, when I do:

#include <math.h>

Does the compiler issue the corresponding binary code and paste it into the compiled executable file OR does the compiler paste the link to the external binary file (a-la Windows DLL?)

I assume the general version of this question is this: is there an equivalent concept for a Windows DLL under * nix?

+5
source share
5 answers

. math.h, , , . , (), , , ( test.c test.o). :

Relocation section '.rel.text' at offset 0x308 contains 1 entries:
 Offset     Info    Type            Sym.Value  Sym. Name
0000001c  00000902 R_386_PC32        00000000   bar

. , ​​ undefined:

9: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND bar

test.o , libm.so. so .dll . , . , test.o, . , ( libm.a then), , . , . ( readelf -d ./test):

Dynamic section at offset 0x498 contains 22 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libm.so.6]
 0x00000001 (NEEDED)                     Shared library: [libc.so.6]
 ... ... ...

, , . Linux , , ld.so. , , ; .

, . .

+25

.

. , , . , GCC - (, , ).

, . - , /. , . , ( , libcurl.a , ):

gcc codefile.c -lcurl
gcc codefile.c /path/to/libcurl.a

( "linker" ), libcurl.a ( gcc , , , ). . , .dll Windows ( .lib Windows). Linux .so.

- GCC, ld, binutils, / ( , ).

+8

Windows DLL * nix?

, "Shared Objects" .so . . linux "ldd" , , . ListDLLs sysinternals, .

+2

, , , , , . ( , , GCC, math.h.)

, . libm.so, " ", Windows.DLL. , libc.so, C runtime.

Edit: -lm , - libm.so.

+1

There is. Inclusion includes text inclusion of the header file (which is standard C / C ++ behavior). What you are looking for is a linker . The -l argument to gcc / g ++ tells the linker which library (s) to add. For math (libm.so) you should use -lm. General pattern:

  • source file: #include <foo.h>
  • Gcc / g ++ command line: -lfoo
  • shared library: libfoo.so

math.h is a small variation of this theme.

+1
source

All Articles