public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How to set `ld` option to prevent to "Segmentation fault (core dumped)" when run program using cpp, gcc, as and ld to build
@ 2023-06-07 22:26 zhonguncle
  2023-06-07 23:31 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: zhonguncle @ 2023-06-07 22:26 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 954 bytes --]

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. 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.


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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to set `ld` option to prevent to "Segmentation fault (core dumped)" when run program using cpp, gcc, as and ld to build
  2023-06-07 22:26 How to set `ld` option to prevent to "Segmentation fault (core dumped)" when run program using cpp, gcc, as and ld to build zhonguncle
@ 2023-06-07 23:31 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-06-07 23:31 UTC (permalink / raw)
  To: zhonguncle; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1671 bytes --]

On Wed, 7 Jun 2023, 23:27 zhonguncle via Gcc-help, <gcc-help@gcc.gnu.org>
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).

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-07 23:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 22:26 How to set `ld` option to prevent to "Segmentation fault (core dumped)" when run program using cpp, gcc, as and ld to build zhonguncle
2023-06-07 23:31 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).