Author Topic: platform-specific linking in linux and windows  (Read 256 times)

Offline alle_meije

  • Multiple posting newcomer
  • *
  • Posts: 29
platform-specific linking in linux and windows
« on: November 11, 2019, 10:33:26 am »
My program writes out .bmp files using the header-only CImg library, which includes (for functionality that I don't use) GUI libraries: in Windows it needs to link against gdi32 and in Linux against X11. Other than that the project does not need to change between the two.

I was trying to find a way to have a 'platform-aware' project that uses '-lgdi32' when building in Windows and '-lX11' when building in Linux. Is that possible via the project's build options (or somewhere else)?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2553
Re: platform-specific linking in linux and windows
« Reply #1 on: November 11, 2019, 10:46:35 am »
There are multiple ways to do this...
1) Create two targets: one for windows and one for linux:
1.1) Project->Properties->Build targets
1.2) Copy one target, rename it to for ex. windows and select windows in the platforms dialog
1.3) Copy one target, rename it to for ex. linux and select unix in the platforms dialog
1.4) Select the target on the left->Build options. Now you can add the libraries how you like
1.5) Now if you build on windows, select the windows target, or the virtual target "all" and codeblocks will build the windows target. If you are on unix, select the unix target or "all" and codeblocks will build the unix version....

2) Via scripting
2.1) Project->Build options->Select the project name on the left
2.2) Linker Settings->Other linker options
2.3) Add the squirrel script:
Code: [Select]
[[ if(PLATFORM == PLATFORM_MSW  ) print(_("-lgdi32")); else if(PLATFORM == PLATFORM_X11 ) printf(_("-lX11")); ]]
be aware, this has to be in one line of the other liner options...

Option 2) is probably not the right way, because on unix and windows the binary files have different extensions, so you probably have to make different build targets, with different ouptut names anyway, so way 1) is probably the more secure way to go...

Offline alle_meije

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: platform-specific linking in linux and windows
« Reply #2 on: November 14, 2019, 04:38:49 pm »
Well I'm delighted to disagree :)

The 2nd option looks a lot simpler and works brilliantly for what I need to do -- compile a number of example projects, whose dependencies are in the same workspace. So it's easy to set all the search directories and link using the lowercase L notation (avoiding the extensions controversy).

Thanks to your script I can do 'Rebuild Workspace" with exactly the same project tree in Linux and Windows. Much obliged!
« Last Edit: November 14, 2019, 04:40:28 pm by alle_meije »

Offline cacb

  • Regular
  • ***
  • Posts: 398
Re: platform-specific linking in linux and windows
« Reply #3 on: November 15, 2019, 09:04:55 am »
Well I'm delighted to disagree :)

The 2nd option looks a lot simpler and works brilliantly for what I need to do -- compile a number of example projects, whose dependencies are in the same workspace. So it's easy to set all the search directories and link using the lowercase L notation (avoiding the extensions controversy).

Thanks to your script I can do 'Rebuild Workspace" with exactly the same project tree in Linux and Windows. Much obliged!

I use the first option (build targets) on Windows and Linux and it works fine. Dependencies are between projects and not build targets so "Rebuild Workspace" works in this scenario as well.

Offline alle_meije

  • Multiple posting newcomer
  • *
  • Posts: 29
Re: platform-specific linking in linux and windows
« Reply #4 on: November 15, 2019, 09:31:58 am »
Excellent, good to hear -- so both of these work!