Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: Avidanborisov on October 21, 2012, 03:39:03 pm

Title: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: Avidanborisov on October 21, 2012, 03:39:03 pm
Code::Blocks links to libstdc++ and libgcc by default when compiling C code. That's really unnecessary for a default behavior I think.

Test:

Code
avidan@avidebian:~$ cat hello.c
#include <stdio.h>

int main(void)
{
printf("Hello World!\n");
return 0;
}
avidan@avidebian:~$ codeblocks --build hello.c

Compiling: /home/avidan/hello.c
Linking console executable: /home/avidan/hello
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

avidan@avidebian:~$ ldd hello
linux-vdso.so.1 =>  (0x00007fffe18c6000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f6cc090f000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f6cc068d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f6cc0476000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f6cc00ef000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6cc0c39000)
avidan@avidebian:~$ # Build manually using GCC
avidan@avidebian:~$ gcc hello.c -o hello
avidan@avidebian:~$ ldd hello
linux-vdso.so.1 =>  (0x00007fff9b537000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f98e5ade000)
/lib64/ld-linux-x86-64.so.2 (0x00007f98e5e88000)
avidan@avidebian:~$ rm hello*
avidan@avidebian:~$

It also links to libm, but that's perhaps good to avoid confusion, and because the math library is just used too often. I can't understand why it links to libgcc and especially libstdc++ by default when compiling C code. Is there any way to disable the linking of these libraries?
Title: Re: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: stahta01 on October 21, 2012, 03:49:48 pm
http://wiki.codeblocks.org/index.php?title=FAQ (http://wiki.codeblocks.org/index.php?title=FAQ)

Read the first 2 General FAQs.

Tim S.
Title: Re: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: oBFusCATed on October 21, 2012, 04:11:15 pm
This happens because C::B uses g++ to do linking, if you think that you won't do any c++ programming you can change it to gcc.
Title: Re: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: Avidanborisov on October 21, 2012, 04:31:43 pm
http://wiki.codeblocks.org/index.php?title=FAQ (http://wiki.codeblocks.org/index.php?title=FAQ)

Read the first 2 General FAQs.

Tim S.

That's not new to me. I know that Code::Blocks isn't a compiler or something like that. I did notice that it linked unnecessary libraries as part of it's usual building procedure for C code.

This happens because C::B uses g++ to do linking, if you think that you won't do any c++ programming you can change it to gcc.

Thanks, I now found that I could change the "Linker for dynamic libs:" to gcc instead of g++ in the "Compiler and debugger settings" dialog and it won't link any additional libraries. Shouldn't that be the default behavior? I mean, when compiling C use gcc as the linker and when compiling C++ use g++ as the linker.
Title: Re: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: oBFusCATed on October 21, 2012, 04:47:21 pm
No, because C::B doesn't do natively multi-compiler project. It support mixed C/C++ projects using only and it is always in this mode.
Title: Re: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: Jenna on October 21, 2012, 06:15:02 pm
You should be able to create a toolchain for pure C projects/targets.
Title: Re: Unnecessary default linking to libstdc++ and libgcc for C code
Post by: p2rkw on October 22, 2012, 09:53:48 pm
g++ by default links libstdc++ and libc,. Notice that those libraries are linked dynamically. If you want to prevent linking to this libs use -nostdlib (http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html).