Re: How to set `ld` option to prevent to "Segmentation fault (core dumped)" when run program using cpp, gcc, as and ld to build

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 7 Jun 2023, 23:27 zhonguncle via Gcc-help, <gcc-help@xxxxxxxxxxx>
wrote:

> Hi,&nbsp;
> 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 &nbsp;to build a program successfully.&nbsp;
>
>
> 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.&nbsp;
>
>
> 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).



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux