On Wed, 7 Jun 2023, 23:27 zhonguncle via Gcc-help, <gcc-help@xxxxxxxxxxx> wrote: > Hi, > Recently I want to use gcc to learning the compiler. And I know the > program `gcc` is a collection of preprocessed, compiler, assembler, linker > and other tools. It's actually only the preprocessor and compiler. It uses the assembler and linker but they are not part of gcc and must be provided separately (on many operating systems they will be provided by the binutils project). So I try to use `gcc -E`,`gcc -S`,`gcc -c` and `gcc -o` to preprocess, > compile, assemble and link to build a program successfully. > > > But when I try to use `cpp`, `cpp -S`, `as` and `ld` to build a program, > the output of program is wrong. > I hope this is just for educational purposes, because there is typically no reason to do it all in separate stages. > > The source code of program is "helloword" in C. > > > The command is: > > > $ cpp ../main.c -o main.i > $ gcc -S main.i > $ as main.s -o main.o > $ ld main.o -o main -I/lib64/ld-linux-x86-64.so.2 -lc -e main > > > The output of program is: > > > $ ./main > Hello World! > Segmentation fault (core dumped) > > > I think reason is `ld`. Because when I use `gcc -o` replace `as`, the > program will work fine. > > > So I want to know how to set `ld` option can make it work same as `gcc -o`. > > > Looking forward to your reply, Thanks. Adding -v to any gcc command will show the subprocesses it executes, with all the options. Running simply 'gcc -v main.c -o main' will show you all the options passed to the assembler and all the options passed to the linker (via the 'collect2' helper program).