News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Conditional code in Code::Blocks project files

Started by Ben Key, November 13, 2013, 07:38:30 PM

Previous topic - Next topic

Ben Key

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.

stahta01

#1
Found in a CB wxWidgets Project in the other compiler option section.

[[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.
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

Ben Key

+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.

stahta01

C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

Ben Key

Can anyone give me more information on how to do this?

BlueHazzard

in c::b you can use scripts in many pleases
for ex. in the compiler flags...

greetings

Ben Key

+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.

Jenna

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=Variable_expansion
especially
http://wiki.codeblocks.org/index.php?title=Variable_expansion#Script_expansion

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

Ben Key

+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 ;.