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

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • 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: 497
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: 13413
    • 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: 497
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: 13413
    • 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: 13413
    • 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: 497
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: 13413
    • 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!]

Offline manuelS

  • Single posting newcomer
  • *
  • Posts: 5
Re: Option to select the linker - auto detect mode
« Reply #8 on: April 20, 2020, 04:51:15 pm »
I just switched my codeblocks version from 13.12 to 20.03 and I found that some of my old projects don't link anymore.

I studied the Build log, and found that the linker command changed from "tricore-g++" to "tricore-gcc". My compiler command in these cases is always "tricore-gcc". Then I found a new option under "Project"->"Build options.."->"Linker Settings" - "Linker executable".

When I change that from "Auto detect" to "Use C++ compiler", the program links fine.

Is there something wrong with the "Auto detect"? It would be cool if I didn't have to change the "Linker executable" option on all my old projects, because I have a couple of them.




Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #9 on: April 20, 2020, 05:06:03 pm »
Why do you need a C++ linker in the first place?
Are you linking to a C++ static library?
What are the linker errors you have?
Have you done a full rebuild of all dependent libraries (they might be C only but still use the C++ linker, which means they are not C++ libs)?
(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 manuelS

  • Single posting newcomer
  • *
  • Posts: 5
Re: Option to select the linker - auto detect mode
« Reply #10 on: April 20, 2020, 06:15:09 pm »
Quote
Why do you need a C++ linker in the first place?
I basically still use the toolchain setup that came from the supplier of the hardware and bios libraries. Never thought about changing it.

Quote
Are you linking to a C++ static library?
My own libraries are all c, but about the bios-libraries, I can't tell.

Quote
What are the linker errors you have?
I just have one with one of the bios-libraries:
Code
tricore-gcc.exe -L..\libs -o ..\result\cflr25.elf ..\temp\cflr25\cflr25main.o ..\temp\cflr25\cflr25param.o ..\temp\cflr25\doha.o  -mcpu=TC1796 -Wl,--relax-bdata -Wl,--relax-24rel -nostartfiles -Wl,-Map=../temp/cflr25.map -T esx3xl_link_extflash.ld -Wl,-Map=..\result\cflr25.map  ..\result\cMultitr.a ..\result\esx3b06.a ..\result\esx3mbIO.a ..\result\LstdLib.a ..\result\paramCan.a ..\result\schmaCan.a ..\result\scomLib.a ..\result\SGlib.a ..\result\sx3_err.a ..\result\emr3X.a ..\result\dirBremse.a ..\libs\libeb06.a ..\libs\libesx3xl.a ..\result\X3_at68.a ..\result\vera.a ..\result\speedControl.a
..\libs\libeb06.a(eb06_pid_control.o): In function `eb06_pid_init':
eb06_pid_control.o(.text+0x12a): undefined reference to `nanf'
Quote
Have you done a full rebuild of all dependent libraries (they might be C only but still use the C++ linker, which means they are not C++ libs)?
I did a rebuild on all of my own libraries.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #11 on: April 20, 2020, 06:30:16 pm »
Hm, interesting.
nanf is not a C++ symbol, so I guess the C++ linker is linking to some library you're missing.
I guess this is a bug in your project - under-specifying the required libraries.
I think this symbol is in libm, so you should be able to fix the build using -lm, but this might be different for your target.
Please inspect the manual of the compiler/linker, how to solve this.

To debug this you could probably execute the g++ and gcc commands in a terminal. Adding -v or -vv (or something similar, check the manual for the exact switch) you can see the difference in the commands executed by the two executions. I think -v make gcc to print all the commands it executes (the preprocessor, compiler, assembler and linker).
(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 manuelS

  • Single posting newcomer
  • *
  • Posts: 5
Re: Option to select the linker - auto detect mode
« Reply #12 on: April 21, 2020, 04:03:32 pm »
I tried a little more testing today. First I reduced the application to only one function call in the main function.

Quote
I think this symbol is in libm, so you should be able to fix the build using -lm, but this might be different for your target
I tried it, "-lm" is the right lib, but the following happens:
- "Auto detect" or "Use C compiler", adding "-lm" to last line in "Project build options"->"Linker Settings"->"Other linker options"-> lib command order for -ld.exe becomes:"...  -lm -leb06 ..." (which is wrong order)
- "Use C++ compiler", with or WITHOUT adding "-lm" to last line in "Project build options"->"Linker Settings"->"Other linker options"-> lib command order for -ld.exe becomes: "-leb06 -lstdc++ -lm" (which is the correct order)

The linking works with the "Auto detect" and "Use C compiler", if I append "libm.a" to the list of "Link libraries" (where libeb06.a is placed) and don't use "Other linker options" for it. But I guess this lib should be included via "Other linker options".

Why doesn't "-lm" go to the end of the lib list for the linker?


Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Option to select the linker - auto detect mode
« Reply #13 on: April 21, 2020, 04:36:01 pm »
You should add "m" to the linker library list. Adding it to the other linker settings is not correct in most if not all cases. Edit: This is true for all libraries to be linked

Tim S.
« Last Edit: April 21, 2020, 04:48:44 pm by stahta01 »
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 manuelS

  • Single posting newcomer
  • *
  • Posts: 5
Re: Option to select the linker - auto detect mode
« Reply #14 on: April 21, 2020, 04:58:34 pm »
Hello Tim, thanks a lot. It's working this way! For my understanding just, "Other linker options" is not intended to ever add any compiler libs?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Option to select the linker - auto detect mode
« Reply #15 on: April 21, 2020, 05:12:54 pm »
Hello Tim, thanks a lot. It's working this way! For my understanding just, "Other linker options" is not intended to ever add any compiler libs?

Correct. Those settings are added right before the libraries are added.

Edit: The only common exception is the shell scripts/commands are added at that location.
And, they often add libraries.

Tim S.
« Last Edit: April 21, 2020, 05:15:42 pm by stahta01 »
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 manuelS

  • Single posting newcomer
  • *
  • Posts: 5
Re: Option to select the linker - auto detect mode
« Reply #16 on: April 21, 2020, 05:27:42 pm »
Thanks once again, it was very helpful to me!

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Option to select the linker - auto detect mode
« Reply #17 on: April 21, 2020, 07:45:01 pm »
Hello Tim, thanks a lot. It's working this way! For my understanding just, "Other linker options" is not intended to ever add any compiler libs?

Correct. Those settings are added right before the libraries are added.
Wrong. This field is intended for people which need to add special linker options. If this is some library so be it.
The problem here is that the order of libraries is important for ld or other unix linkers.
If libA depends on libB, libB should be before libA in the list. But the other linker options are appended to the linker command after the list of libraries, which violates the requirements of libA and you get a linking failure.

Ideally there should be a switch to disable this linker behaviour, but I've not yet implemented this.
(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!]