Code :: Blocks 8.02
Ubuntu 10.04 LTS (x86_64)

I write Sishny code. The compiler swears that there is no g ++ and asks to install. Installed. It worked. Now the question is, what is the difference between compiled C code in gcc and C code in g ++?

    2 answers 2

    Why does IDE require a C ++ compiler for C code?

    Everything is very simple. G ++ is used as a linker (linker):

    gcc -Wall -O2 -c /home/gaal/TEST/TEST/main.c -o obj/Release/main.o g++ -o bin/Release/TEST obj/Release/main.o -s 

    Now the question is, what is the difference between compiled C code in gcc and C code in g ++?

    See comments to the previous answer . If the compiler is gcc and the linker is g ++, then dependencies on the C ++ libraries will be added. If the compiler and linker are g ++ with keys for C code, then again there will be certain libraries. But in theory, the code will be "Sishny."

    • one
      I put the gcc linker in the settings, deleted g ++ from the computer and started compiling C code without the need for g ++. Thank. - Jakeroid

    The C ++ runtime will be stitched into the binary, perhaps some additional sections will be added.

    There will be other library dependencies:

    C :

     linux-vdso.so.1 => (0x00007fff6ab46000) libc.so.6 => /lib/libc.so.6 (0x00007f0eb1149000) /lib64/ld-linux-x86-64.so.2 (0x00007f0eb14f4000) 

    C++ :

     linux-vdso.so.1 => (0x00007fff12fff000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007fe1cab00000) libm.so.6 => /lib/libm.so.6 (0x00007fe1ca87d000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007fe1ca666000) libc.so.6 => /lib/libc.so.6 (0x00007fe1ca2e3000) /lib64/ld-linux-x86-64.so.2 (0x00007fe1cae2e000) 

    Here is the code:

     int main(){} 

    compiled ( gcc , g++ ) and disassembled differed only in strings:

     // в начале main .cfi_personality 0x3,__gxx_personality_v0 ... // при выходе из main movl $0, %eax 

    In the C ++ version both were added. The first line - apparently, some metadata, but I can not explain the second, but in general, nothing substantial. For some reason, the eax register is reset.

    • In general, G ++ can build C code as C code: <pre> gaal @ linux-lybs: ~> g ++ -xc 1.c gaal @ linux-lybs: ~> ldd ./a.out linux-gate.so .1 => (0xffffe000) libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7874000) libc.so.6 => /lib/libc.so.6 (0xb7707000) / lib / ld-linux. so.2 (0xb78ac000) </ pre> - gecube
    • And this is gcc native: <pre> gaal @ linux-lybs: ~> gcc 1.c gaal @ linux-lybs: ~> ldd ./a.out linux-gate.so.1 => (0xffffe000) libc.so. 6 => /lib/libc.so.6 (0xb757b000) /lib/ld-linux.so.2 (0xb7702000) </ pre> - gecube
    • one
      <pre> movl $ 0,% eax </ pre> And this is very simple - return 0. Because the return value MUST be. Default is 0. - gecube
    • Yeah, but in the C version of this movl $0 not. - Vladimir Gordeev
    • four
      This is normal. The fact is that C is a much less strict language. If you do not specifically include anything, then you can pass in the function the devil knows what parameters, you can not return anything from the function with an explicitly specified return type .. in general, weak type control. - cy6erGn0m