Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: mkaut on March 31, 2014, 09:24:15 am

Title: OS-dependent library linking?
Post by: mkaut on March 31, 2014, 09:24:15 am
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.
Title: Re: OS-dependent library linking?
Post by: 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
Code
[[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
Title: Re: OS-dependent library linking?
Post by: 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
Title: Re: OS-dependent library linking?
Post by: mkaut on March 31, 2014, 01:15:11 pm
You can add a squirrel script in the build process:
Add this in the linker settings, other linker options
Code
[[if(PLATFORM == PLATFORM_MSW) {print("-lws2_32"); };]]

This worked, thanks a lot!  :)
Title: Re: OS-dependent library linking?
Post by: mkaut on March 31, 2014, 01:16:33 pm
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..