News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

OS-dependent library linking?

Started by mkaut, March 31, 2014, 09:24:15 AM

Previous topic - Next topic

mkaut

Hello,
I have a project that I build on Windows and Linux. Most of the targets work for both platforms, but one of them needs winsocks when compiled on Windows.
I want to avoid having two targets, so I wanted to ask what is the best/recommended way of dealing with this situation?

I tried adding the winsocks to "Other linker options" using the $if(){}{} command:
$if("OS == Windows"){-lws2_32}{}, but did not find any way to write the OS == Windowscondition.

At the end, I just created a custom compiler variable WINSOCKS=ws2_32 in my Windows installation of C::B and use the following test:
$if($WINSOCKS){-l$WINSOCKS}{}. This works for me, but it will fail for anyone else who will try to build the project on Windows, without the variable defined..

So, my question is, is there a better way?


As a side note, the target file prepends three libraries to the project settings:
PocoDataMySQL, mysqlclient, and ws2_32 (in this order). If I have the first two as "Link libraries" and winsocks in "Other linker options", the linker gets
-lws2_32 -lPocoDataMySQL -lmysqlclient, i.e., winsocks goes first ... which does not work. As a result, I have to move the other two libraries to "Other linker options" - not really an issue, just that I would expect "other options" to go at the end.

BlueHazzard

You can add a squirrel script in the build process:
Add this in the linker settings, other linker options

[[if(PLATFORM == PLATFORM_MSW) {print("-lws2_32"); };]]

more information about build scripts and squirrel:
http://wiki.codeblocks.org/index.php?title=Scripting_commands
http://wiki.codeblocks.org/index.php?title=Variable_expansion#Script_expansion

greetings

BlueHazzard


mkaut

Quote from: BlueHazzard on March 31, 2014, 11:10:26 AM
You can add a squirrel script in the build process:
Add this in the linker settings, other linker options

[[if(PLATFORM == PLATFORM_MSW) {print("-lws2_32"); };]]


This worked, thanks a lot!  :)

mkaut

Quote from: BlueHazzard on March 31, 2014, 11:13:47 AM
or you use build scripts http://wiki.codeblocks.org/index.php?title=Build_scripts but for this you need a additional file

Thanks.
I think I will go with the first/easier solution, but good to know about this approach..