0

I worte a most simple C program main.c under linux, just like this:

int
main(void)
{ 
  return 0;
}

And compiled :

gcc main.c -o main 

...happy, and got main exe

But when I issued the command

ldd main

It showed me like this:

linux-vdso.so.1 (0x00007ffd78151000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3f53a92000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3f53c9c000)

Why would this program depend on the glibc without invoking any functions of it ?

Li-Guangda
  • 341
  • 1
  • 4
  • 14
  • 2
    The Operating System does not call your `main()`. It calls glibc's `_start()` and that code calls your `main()` – pmg Jun 07 '21 at 10:04
  • Related? https://stackoverflow.com/questions/29694564/what-is-the-use-of-start-in-c – pmg Jun 07 '21 at 10:07
  • A Timely answer.It help me,thank you. – Li-Guangda Jun 07 '21 at 10:26
  • And the specific mechanism that creates the dependency in your code is that when you link a C program, the name of the start-up routine is set by default to `_start`. (The loader, `ld`, has a switch for this, `-e name`.) That causes the linker to look for a definition for that symbol while it is building the executable file. It finds that symbol in the glibc library, resulting in a dependency on that library. – Eric Postpischil Jun 07 '21 at 10:35
  • 1
    @pmg operating system does not call _start only jumps to the program entry point stored in the executable file. From there execution starts. Not related at all to `_start` – 0___________ Jun 07 '21 at 10:36
  • 1
    If you do not want to use glibc you need to write your own startup code. – 0___________ Jun 07 '21 at 10:38

0 Answers0