Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: Ben Key on November 13, 2013, 07:38:30 pm

Title: Conditional code in Code::Blocks project files
Post by: Ben Key on November 13, 2013, 07:38:30 pm
Is it possible to add conditional code to a project files? For example I would like to be able to add compiler options that are only used on the Windows platform. I know I can accomplish this by having a Windows specific target, but my project file already has four targets (Debug x64, Debug x86, Release x64, and Release x86) and this approach would require me to add four more targets.

The WiX project, which also uses an XML based file format, supports the following syntax.

    <?ifdef condition ?>
    <?else?>
    <?endif?>

Is a similar syntax supported in Code::Blocks project files?

I found the Conditional Evaluation section of the Variable expansion page on the Code::Blocks Wiki but it fails to provide any details regarding the syntax of the condition or any indication of whether or not it can be used for this purpose.
Title: Re: Conditional code in Code::Blocks project files
Post by: stahta01 on November 13, 2013, 08:04:19 pm
Found in a CB wxWidgets Project in the other compiler option section.
Code
[[if (PLATFORM == PLATFORM_MSW && (GetCompilerFactory().GetCompilerVersionString(_T("gcc")) >= _T("4.0.0"))) print(_T("-Wno-attributes"));]]

The above code adds the Compiler Option of "-Wno-attributes" if using a gcc compiler >= version 4.0 on the Windows platform.

Look under CB scripting for more info.

Tim S.
Title: Re: Conditional code in Code::Blocks project files
Post by: Ben Key on November 13, 2013, 08:43:47 pm
+stahta01
Can you give a more complete example? I am not certain how to incorporate this into a <Compiler> section of a CBP file.

To begin with, the syntax for options seems to be "<Add option={option} />." I do not see anything that tells me where to put the [[{code}]] stuff.

Second, what are PLATFORM and PLATFORM_MSW? Are these global variables that are available everywhere in Code::Blocks project files.

As for the Scripting Code::Blocks page, it seems to describe changes to the Code::Blocks scripts, not project files.

In short, your answer, while helpful, does not provide near enough information.
Title: Re: Conditional code in Code::Blocks project files
Post by: stahta01 on November 13, 2013, 08:47:23 pm
No more info from me.

Tim S.
Title: Re: Conditional code in Code::Blocks project files
Post by: Ben Key on November 13, 2013, 09:21:41 pm
Can anyone give me more information on how to do this?
Title: Re: Conditional code in Code::Blocks project files
Post by: BlueHazzard on November 13, 2013, 09:22:38 pm
in c::b you can use scripts in many pleases
for ex. in the compiler flags...

greetings
Title: Re: Conditional code in Code::Blocks project files
Post by: Ben Key on November 13, 2013, 09:32:34 pm
+BlueHazzard
Once again I must ask for more information. It is not enough for you to simply tell me that scripts can be used in many places. I need specific information on exactly how to use scripts to conditionally add a compiler flag.

As I have already said, the syntax seems to be "<Add option={option} />." What I would like to know is exactly how to modify that so that an Add option line is conditionally evaluated. I know the syntax is [[{code}]] but I have seen nothing about how to combine this with an Add option line.
Title: Re: Conditional code in Code::Blocks project files
Post by: Jenna on November 13, 2013, 09:56:42 pm
You should not touch the projectfile directly (except for very rare cases and if you know exactly what you do), or you might break it.

If you add the snippet posted by Tim in the "Other options" tab of the "Compilr settings" (either on project or target level), you will get the "-Wno-attributes" option added to your compiler options, if you are on windows (MSW) and have gcc >= 4.0 as compiler.

See:
http://wiki.codeblocks.org/index.php?title=Scripting (http://wiki.codeblocks.org/index.php?title=Scripting)

http://wiki.codeblocks.org/index.php?title=Variable_expansion (http://wiki.codeblocks.org/index.php?title=Variable_expansion)
especially
http://wiki.codeblocks.org/index.php?title=Variable_expansion#Script_expansion (http://wiki.codeblocks.org/index.php?title=Variable_expansion#Script_expansion)

http://wiki.codeblocks.org/index.php?title=Scripting_commands (http://wiki.codeblocks.org/index.php?title=Scripting_commands)
for the constants in the snippet:
http://wiki.codeblocks.org/index.php?title=Scripting_commands#Constants (http://wiki.codeblocks.org/index.php?title=Scripting_commands#Constants)
Title: Re: Conditional code in Code::Blocks project files
Post by: Ben Key on November 14, 2013, 02:09:35 am
+jens
Thanks. That was what I needed to know.

I did notice that if I add two [[{code}]] lines in the "Other options" control I get errors related to there being a missing ;.