Libraries that are linked by default
My code is in test.c:
int main(){
return 0;
}
The dynamically shared libraries the executable compiled from it depends on are:
$ gcc -o test test.c
$ ldd test
linux-gate.so.1 => (0x00783000)
libc.so.6 => /lib/libc.so.6 (0x00935000)
/lib/ld-linux.so.2 (0x00ea5000)
- I was wondering what roles the three libraries are playing?
- Which library does the function
main
belong to? /lib/libc.so.6? - Which library does开发者_StackOverflow社区
return
belong to? /lib/libc.so.6? - Are the three libraries all that are dynamically linked by default by gcc?
- How can I find out static libraries that gcc links to by default?
Thanks!
linux-gate.so
isn't really a shared lib, but a part of the kernel that acts like one and makes fast system calls possible.ld-linux.so
is a piece of code that makes loading other shared libraries possible.libc.so
is the C library, containing standard functions likeprintf
andstrcpy
.main
doesn't belong to any library. It belongs to your program, in the sense that its assembled version is stored entirely in thetest
binary file.return
is not a function but a C language construct.- No, it also links in
libgcc
, which is apparently not a shared library on your system (or it would show up) and some startup code.g++
would additionally link inlibstdc++.so
(the C++ standard library) andlibm.so
(the math part of the C standard library).
linux-gate
is a virtual shared object that acts as a connection to system calls within the kernel.libc
is glibc, which provides functions such asprintf()
and so on.ld-linux
is the glibc loader, which allows loading of other shared objects.main()
belongs to your code. It is called bycrt1.o
which is linked into the executable by gcc (well, ld specifically).return
is not a function but rather a language construct, so gcc turns it directly into code contained within the object (and eventually executable) file. As an aside, the value returned frommain()
is caught bycrt1.o
and turned into a program result code.
Exelent description about how does linux execute my main()? There you will find the answer and probably a lot more!
linux-gate
is a virtual library that provides access to system functions. Its full name is the Linux Virtual Dynamic Shared Object. It's used by libc
.
libc
is the C run time. It's what actually calls main() for you. (It's possible to bypass this if you don't use any C functions.)
ld-linux
is the dynamic linker, which actually knows how to load and call the C run time for you.
main
lives in test.o, not in a library.
return
is a keyword, not a function. It directs the compiler to emit code to cause the function to return control to its caller.
ld-linux.so
provides the magic that helps ldd
work.
libc.so
is part of the C runtime library. Among other things, the runtime library contains the actual entry point that calls main
.
main
is provided by your code.
return
is not a function, it's a keyword in the C language.
精彩评论