Author Topic: Option to select the linker - auto detect mode  (Read 647 times)

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12069
    • Travis build status
Option to select the linker - auto detect mode
« on: September 30, 2019, 08:29:48 pm »
There are numerous reports that building C-only projects uses g++ to link them.
This is one nuisance of our build system I want to solve.
The problem happens because we have only one option for linker executable, but with GCC/LLVM compiler we need two.

Here you could see my current implementation: https://github.com/obfuscated/codeblocks_sf/tree/experiments/auto_linker

The idea is to add new option to the Linker options of every target. This option make will allow the user to choose which executable to use for linking: C compiler, C++ compiler, Linker or Auto-detect. Default is auto-detect. In auto-detect mode the idea is that C::B will detect if the target is building a C or C++ binary. When C::B detects a C++ binary it will the C++ compiler, else it will use the C compiler.

This should work fine for GCC/LLVM compilers. But it will fail for VStudio, so I do an additional check - the C or C++ compiler is used for linking only if one of them is set as the linker executable. If not the linker executable is used instead.

I've currently tested this with GCC on simple projects on Linux.
Would it be possible to test this with various compilers and tell me if it works correctly?
(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 sodev

  • Regular
  • ***
  • Posts: 329
Re: Option to select the linker - auto detect mode
« Reply #1 on: September 30, 2019, 09:19:41 pm »
There is a setting for a C compiler and a setting for a C++ compiler, wouldn't it be more simple and logical to add a setting for a C linker and a setting for a C++ linker? You still need to figure out if the project is plain C but this is less auto-magic? Btw. that would be a good addition, to actually be able to set in what mode to compile the whole project, afaik this is not possible, you have to specify this for each file.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12069
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #2 on: September 30, 2019, 11:07:46 pm »
Is there a case where you have two different linker executables (one for C and one for C++) and they are not the same as the compiler executables?

p.s. I plan to add a setting for asm compiler. Should we add a linker for asm after that?
p.p.s. Do you have a valid case where you really want to force a project to compile as C?
(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 sodev

  • Regular
  • ***
  • Posts: 329
Re: Option to select the linker - auto detect mode
« Reply #3 on: October 01, 2019, 10:44:38 pm »
Is there a case where you have two different linker executables (one for C and one for C++) and they are not the same as the compiler executables?
In my small world i only know GCC and MSVC but in CodeBlocks there is a long list of other compilers and i don't know how they work. I don't know how often these other compilers are used and how much people will complain if something doesn't work anymore, your changes are about solving issues, don't introduce new ones by using wrong magic ;)

Quote
p.s. I plan to add a setting for asm compiler. Should we add a linker for asm after that?
I never had to deal with assembler code in my projects so i can't even offer dangerous superficial knowledge here. That needs an own linker to link with what, a regular C/C++ project? Or are you talking about standalone support for a plain ASM compiler like for Fortran, which i also never used ;D? Beside C++-sources i only have to deal with .wxrc and .proto 8)

Quote
p.p.s. Do you have a valid case where you really want to force a project to compile as C?
I am a C++ developer and so far i only had problems with some C sources i needed to include into a project that just don't compile in C++ mode. Shame on me i didn't even know you need a different linker on GCC to produce a C-only executable ::). In MSVC you don't and that one offers global switches to turn on C mode or C++ mode, but still this doesn't have any influence on the linker stage. On second thought maybe that use-different-linker-if-everything-is-c is kind of a GCC internal and automagic could solve this invisible to the user :).

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12069
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #4 on: October 01, 2019, 11:49:19 pm »
OK, so you have no real world examples why this won't work. Please test it especially if you're using C::B + msvc. My magic is override-able, so people having problems would have a way to disable it. Also if it turns problematic we could change the default to Linker and this would behave exactly the same as now.

About the linker: You can use g++ to link C only projects, but this forces them to depend on the C++ runtime. People don't want this, so they want to use gcc. MSVC is not a C compiler, it is C++ compiler, also there you have a separate linker (link.exe), you cannot use cl.exe for multi-file projects as far as I know (you can prove me wrong with a link to example commands).

About asm: It will make it possible to put .asm files into the project and just compile without the fiddling needed at the moment.
(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 oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12069
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #5 on: October 21, 2019, 11:14:23 pm »
This is soon going in trunk/master. Any final comments or thoughts?  8)
(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 sodev

  • Regular
  • ***
  • Posts: 329
Re: Option to select the linker - auto detect mode
« Reply #6 on: October 22, 2019, 09:58:30 pm »
Will complain if it breaks my projects after it went into trunk :). Im currently just too busy/lazy to checkout a second copy for testing, im just waiting for an updated svn ;).

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 12069
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #7 on: October 26, 2019, 07:40:09 pm »
In svn, lets see who is going to complain, because I've broken something.  8)
(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!]