Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: MrPrise on June 10, 2008, 02:37:37 pm

Title: Custom value for a variable depending on the current platform
Post by: MrPrise on June 10, 2008, 02:37:37 pm
What is the suggested / ideal method to maintain a cross-platform project?
I have problem with the included libraries. On Windows I need to include the mingw32 lib, but on Linux I don't need that.
I use a custom variable on the linker settings page, but I don't know how can I change the value of that variable depending on the actual platform. How can I do this? Thanks.
Title: Re: Custom value for a variable depending on the current platform
Post by: mariocup on June 10, 2008, 04:02:32 pm
Hi MrPrise,

you could attach a script to your build targets e.g. settings.script. Here an example that could do the job.

Code
function SetBuildOptions(base)
{
        if(PLATFORM == PLATFORM_MSW)
        {
                 base.AddLinkLib(_T("name"));
        }
} // end of SetBuildOptions

Bye,

Mario
Title: Re: Custom value for a variable depending on the current platform
Post by: MrPrise on June 10, 2008, 04:10:56 pm
That is exactly what I need! Thank you very much!

Update: I still have a little issue with that solution. I need to add mingw32 as the first library, else I get "undefined reference to `WinMain@16'"
Title: Re: Custom value for a variable depending on the current platform
Post by: MrPrise on June 10, 2008, 04:55:01 pm
I found the solution to my problem. I post it in case someone else have this problem.
Mariocup's solution was good in general, but for mingw32 it needs a little tunning, since I need to add mingw32 as the first library.
Here is my solution:
Code
local libs_bk = base.GetLinkLibs();
base.SetLinkLibs(_T("mingw32"));
base.AddLinkLib(&libs_bk);
First, we get the current list of libraries (specified on the linker settings page). We replace the list of libraries with mingw32, than we append the original list of the libraries to the list.
Title: Re: Custom value for a variable depending on the current platform
Post by: mariocup on June 10, 2008, 10:12:33 pm
Hi MrPrise,

you could even surround your libraries with the linker options

Code
-Wl,--start-group <list of libs> Wl,--end-group

then the order of libs is not relevant, as the linker will solve cyclic references.

Bye,

Mario
Title: Re: Custom value for a variable depending on the current platform
Post by: MrPrise on June 10, 2008, 11:33:50 pm
Thanks! Can I define custom variables in the build script?
Title: Re: Custom value for a variable depending on the current platform
Post by: mariocup on June 11, 2008, 08:09:55 am
Yes you can. See the wiki page for details (http://wiki.codeblocks.org/index.php?title=Scripting_commands#CompileOptionsBase).
Title: Re: Custom value for a variable depending on the current platform
Post by: MrPrise on June 11, 2008, 12:28:41 pm
Thank you for your help!