How can I generate an ELF file with GCC?

前端 未结 1 1315
情深已故
情深已故 2021-02-14 23:46

I am writing C and C++ code on Linux OS and I am using GCC. After finishing my code, I would like to generate an ELF file. I just can generate \"a.out\" file and I don\'t need i

相关标签:
1条回答
  • 2021-02-15 00:22

    The compiler (i.e. gcc or g++) will invoke the linker (ld) which produces an ELF executable.

    In practice, you will use a builder program (like make) to drive gcc commands. See this answer.

    The default output file for gcc is still named a.out (for historical reasons) but is an ELF file. And you really want to ask gcc to output an executable with a more fancy name.

    Simple example, you code a single-file hello-world.c program. You can compile it with e.g.

     gcc -Wall -g hello-world.c -o hello-world-bin
    

    (order of arguments to gcc matters a lot!)

    and the produced hello-world-bin is an ELF executable. Check with

     file hello-world-bin
    

    then run it with

     ./hello-world-bin your arguments to it
    

    Later, learn how to use the gdb debugger on it.

    See also this and that answers.

    0 讨论(0)
提交回复
热议问题