Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to conditionally compile / run code based on a macro OR a variable?
    text
    copied!<p>We have several projects in development sharing the same codebase. Certain pieces of code are only relevant to one or other of those projects.</p> <p>We have a couple of requirements:</p> <p>The first requirement is that we want our final releases not to compile in code from the other projects.</p> <p>One solution (the one we like) is to use the preprocessor to remove that code: (this is what we do in some places).</p> <pre><code>#if defined PROJECT1 { // some code } #endif </code></pre> <p>The second requirement is, that while we are developing, it is helpful to make sure that a change of code still works across all projects, so we would like to compile ALL the project code at once, and be able to switch between projects without a recompile - so in other places in our code, we use a variable to determine the project instead:</p> <pre><code>if (project == 1) { // some code } </code></pre> <p>What I'd like to be able to do is to combine the benefits of both - code which in some situations (let's say determined by a #define REMOVECODE) is not included in the final exe at all, but in other situations (determined by the non-definition of the REMOVECODE define) to include the code in the compiled .exe</p> <p>One more thing - sometimes we have code which exists in a couple of projects, so the solution would need to handle tests like "if project == 1 || project == 2"</p> <p>I'm thinking it would look something like the following (this doesn't work because I don't think you can nest preprocessor directives), but I'm not sure if it is even possible with macros. Maybe there's some kind of template solution?</p> <pre><code>#ifdef REMOVECODE #define BEGINTEST #if #define ENDTEST #endif #define CONDITION1 defined PROJECT1 #else #define BEGINTEST if #define ENDTEST #define CONDITION1 project == 1 #endif BEGINTEST(CONDITION1) { // some code } ENDTEST </code></pre> <p>If anyone can help out, I'd be much obliged.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload