none
How to add/remove file from compilation in a C++ project depending on the current configuration RRS feed

  • Question

  • Hello! I'm trying to setup a project with two configurations:

    • One for tests (Tests)
    • Another one for debug (Debug)

    In the Tests configuration I want the file tests.cpp that contains the main function that runs the tests from Google Test to be compiled, but I want this file to be excluded from compilation in the Debug config.

    In the Debug configuration, I want the file main.cpp that contains the main function of the application to be compiled, but I want this file to be excluded in the Tests config.

    If this works, I wont have any problems of "redefinition of main", but I'm having this problem right now because I cant exclude/include these files (by switching to the appropriate Configuration and right clicking on the file in the Solution Explorer window and then clicking in "Include in Project" or "Exclude from Project") for a single configuration. Everytime I try to include/exclude these files, this setting seems to be applied to all the configurations of the project.

    Am I missing something here?

    Thanks in advance for your help

    PS: I'm using Visual Studio 2019 with VisualGDB



    Thursday, December 5, 2019 9:09 PM

All replies

  • Yes, that does apply to the entire project, hence why it states Exclude from Project.

    What you want to do is right click on the individual item, select Properties and bring up the property window for that single file.

    You can see the Exclude From Build property that is highlighted. If you select Yes then it will be excluded from build for the configuration that the properties window is showing.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Thursday, December 5, 2019 10:07 PM
  • Thanks for the answer!

    I did this and the .vcxproj file was updated to this:

        <ClCompile Include="main.cpp">
          <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Tests|x64'">true</ExcludedFromBuild>
          <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">false</ExcludedFromBuild>
        </ClCompile>

        <ClCompile Include="tests\tests.cpp">
          <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
          <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Tests|x64'">false</ExcludedFromBuild>
        </ClCompile>

    This looks good, but its still compiling both of the files in both configurations.

    Can this be a problem with VisualGDB overriding some of these settings?

    Thursday, December 5, 2019 11:49 PM
  • Well, the obvious dumb question is what platform is Visual Studio itself set to? The selected configuration/platform pair can be different to the properties window.

    The snipped of the project file shows that you have this set up for x64 (you can see this in the |x64 in the condition). So if you have Visual Studio set as:

    then this should work. Notice the x64? However, if you have Visual Studio set as:

    Notice the x86? Then the configuration will default to including both of them.

    Please make sure that you have the excluded files set properly for each configuration/platform pair that you want to build for. Also note that in the properties, x86 is denoted by Win32.

    Also, Solution Explorer will show whether a file is excluded for that configuration/platform:

    Notice the red circle with the horizontal line it it? This is saying that in this configuration (which was Debug/x86) the file asm64.asm is not going to be included in the build.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Friday, December 6, 2019 7:03 AM