Skip to content

u/amauk

In this archive from Oct 2012 to Nov 2020

0
posts
253
comments
0
media
1k
score

Top comments

I've tried several cars (Ford, BMW, Volkswagon, Honda, ..), but never really understood the difference between them.

I understand that many cars just use one car as a base (i.e. Seat -> Volkswagon, Lexus -> Toyota) and offer a slightly adjusted styling (i.e. 2 door sport edition).

Apart from the body styling, gearbox and engine type shipped with the car and how they are serviced (every x thousand miles or every x months) I haven't noticed many differences. That doesn't even apply if one were to setup a bare-bones kit car.

55 points

These things are separate because turning a set of C or C++ source files into an executable is a multi-step process.

gcc is the GNU Compiler Collection.
It deals with turning source files into architecture-dependant assembly code.
It's the first step in compilation, and is typically the only user-invoked bit of the process (gcc typically deals with calling the assembler and linker automatically)

glibc is the GNU implementation of the Standard C library.
The Standard C library is a set of C header files that provide core definitions and low-level functionality needed for non-trivial programs to operate.
See here http://en.wikipedia.org/wiki/C_standard_library

libstdc++ is the C++ standard library.
It provides the same core definitions and low-level functionality as the Standard C library, plus a whole heap of other stuff too.
(stuff that, if you'd done the program in C, would need additional external libraries linked in) See here http://en.wikipedia.org/wiki/C%2B%2B_standard_library

binutils are programs for translating assembly language into object code, and various manipulation techniques for linking object files together.
http://en.wikipedia.org/wiki/Binutils

An example might be helpful here, so here goes.

main.c

int main()
{
  hello();
  bye();
}

hello.c

#include <stdio.h>
void hello()
{
  printf ("Hello World!\n");
}

bye.c

#include <stdio.h>
void bye()
{
  printf ("Goodbye World!\n");
}

As you can see from the source files, we're including the input/output functions from the Standard C library.

Calling gcc in the conventional way will just compile the thing in one go, without showing you any of the intermediate steps.

gcc -o foo main.c hello.c bye.c

You get an executable foo

However, if you tell gcc not to do anything other than compile to assembly, you can see the intermediate steps.

gcc -S -o main.S main.c
gcc -S -o hello.S hello.c
gcc -S -o bye.S bye.c

Now you have 3 assembly files, translated from the C source code.
View these in a text editor to see the assembly

hello.S

        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "Hello World!"
        .text
        .globl  hello
        .type   hello, @function
hello:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        movl    $.LC0, %edi
        call    puts
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   hello, .-hello
        .ident  "GCC: (Gentoo 4.8.2 p1.0, pie-0.5.8) 4.8.2"
        .section        .note.GNU-stack,"",@progbits

Then you can run the assembly through the assembler to get the 3 object files

as -o main.o main.S
as -o hello.o hello.S
as -o bye.o bye.S

These object files are binary, so not viewable by a text editor.

Then you can link the 3 object files together into a single executable
(ld is a pain to use directly, so I'm using it via gcc)

gcc -o foo main.o hello.o bye.o

Bingo, your foo executable.

26 points

Subreddits

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.