Linking a C program directly with ld fails with undefined reference to `__libc_csu_fini`

后端 未结 8 1496
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 08:54

I\'m trying to compile a C program under Linux. However, out of curiosity, I\'m trying to execute some steps by hand: I use:

  • the gcc frontend to produce assemb
8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 09:36

    In Ubuntu 14.04 (GCC 4.8), the minimal linking command is:

    ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 \
      /usr/lib/x86_64-linux-gnu/crt1.o \
      /usr/lib/x86_64-linux-gnu/crti.o \
      -L/usr/lib/gcc/x86_64-linux-gnu/4.8/ \
      -lc -lgcc -lgcc_s \
      hello.o \
      /usr/lib/x86_64-linux-gnu/crtn.o
    

    Although they may not be necessary, you should also link to -lgcc and -lgcc_s, since GCC may emit calls to functions present in those libraries for operations which your hardware does not implement natively, e.g. long long int operations on 32-bit. See also: Do I really need libgcc?

    I had to add:

      -L/usr/lib/gcc/x86_64-linux-gnu/4.8/ \
    

    because the default linker script does not include that directory, and that is where libgcc.a was located.

    As mentioned by Michael Burr, you can find the paths with gcc -v. More precisely, you need:

    gcc -v hello_world.c |& grep 'collect2' | tr ' ' '\n'
    

提交回复
热议问题