Author Topic: Conditional code in Code::Blocks project files  (Read 6473 times)

Offline Ben Key

  • Single posting newcomer
  • *
  • Posts: 5
  • Software Engineer and Aspiring Author
Conditional code in Code::Blocks project files
« 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.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Conditional code in Code::Blocks project files
« Reply #1 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.
« Last Edit: November 13, 2013, 08:05:56 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Ben Key

  • Single posting newcomer
  • *
  • Posts: 5
  • Software Engineer and Aspiring Author
Re: Conditional code in Code::Blocks project files
« Reply #2 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.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7678
    • My Best Post
Re: Conditional code in Code::Blocks project files
« Reply #3 on: November 13, 2013, 08:47:23 pm »
No more info from me.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Ben Key

  • Single posting newcomer
  • *
  • Posts: 5
  • Software Engineer and Aspiring Author
Re: Conditional code in Code::Blocks project files
« Reply #4 on: November 13, 2013, 09:21:41 pm »
Can anyone give me more information on how to do this?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Conditional code in Code::Blocks project files
« Reply #5 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

Offline Ben Key

  • Single posting newcomer
  • *
  • Posts: 5
  • Software Engineer and Aspiring Author
Re: Conditional code in Code::Blocks project files
« Reply #6 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.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7256
Re: Conditional code in Code::Blocks project files
« Reply #7 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=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

Offline Ben Key

  • Single posting newcomer
  • *
  • Posts: 5
  • Software Engineer and Aspiring Author
Re: Conditional code in Code::Blocks project files
« Reply #8 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 ;.