Author Topic: Unnecessary default linking to libstdc++ and libgcc for C code  (Read 11975 times)

Offline Avidanborisov

  • Single posting newcomer
  • *
  • Posts: 2
Unnecessary default linking to libstdc++ and libgcc for C code
« 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?
« Last Edit: October 21, 2012, 03:41:21 pm by Avidanborisov »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7590
    • My Best Post
Re: Unnecessary default linking to libstdc++ and libgcc for C code
« Reply #1 on: October 21, 2012, 03:49:48 pm »
http://wiki.codeblocks.org/index.php?title=FAQ

Read the first 2 General FAQs.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Unnecessary default linking to libstdc++ and libgcc for C code
« Reply #2 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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Avidanborisov

  • Single posting newcomer
  • *
  • Posts: 2
Re: Unnecessary default linking to libstdc++ and libgcc for C code
« Reply #3 on: October 21, 2012, 04:31:43 pm »
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.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Unnecessary default linking to libstdc++ and libgcc for C code
« Reply #4 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.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Unnecessary default linking to libstdc++ and libgcc for C code
« Reply #5 on: October 21, 2012, 06:15:02 pm »
You should be able to create a toolchain for pure C projects/targets.

Offline p2rkw

  • Almost regular
  • **
  • Posts: 142
Re: Unnecessary default linking to libstdc++ and libgcc for C code
« Reply #6 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.