Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:55:12+00:00 2026-05-23T13:55:12+00:00

We have several projects in development sharing the same codebase. Certain pieces of code

  • 0

We have several projects in development sharing the same codebase. Certain pieces of code are only relevant to one or other of those projects.

We have a couple of requirements:

The first requirement is that we want our final releases not to compile in code from the other projects.

One solution (the one we like) is to use the preprocessor to remove that code: (this is what we do in some places).

#if defined PROJECT1
{
    // some code
}
#endif

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:

if (project == 1)
{
    // some code
}

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

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”

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?

#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

If anyone can help out, I’d be much obliged.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:55:12+00:00Added an answer on May 23, 2026 at 1:55 pm

    If the condition in the test is a compile-time constant, any half-decent optimizing compiler will remove the dead code completely from the object file.

    So something as simple as this should work fine:

    #ifdef REMOVECODE
    
    #ifdef PROJECT1
    #define CONDITION1 1
    #else
    #define CONDITION1 0
    #endif
    
    #else
    #define CONDITION1 project == 1
    #endif
    
    ...
    
    if (CONDITION1)
    {
        ...
    }
    

    Run your compiler with -S (or equivalent) and look at the generated assembly to confirm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several web application projects in one solution. When I start debugging one
I have a solution with several projects. One of them is a setup project.
I have several projects on my development system - some projects are open source,
I have several projects with a very large over-lapping code-base. We've just recently started
I have several projects where I need to append strings to a BSTR/CComBSTR/_bstr_t object
I have several projects and a website in a large asp.net solution. In some
Where I work I have several projects in separate repositories. Each project shares a
I have several C# projects along with some internal library components that I'm trying
I have several deployment projects. In order to deploy an application, I need to
I have several related CVS projects, and I'd like to move them into subdirectories

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.