Command line compilation while augmenting preprocessor defintion

BOUGNOUX Sylvain 1 Reputation point

Sorry if it has been asked many times, but I still cannot make my way.

I've a C++ foo.sln generating foo.exe. I would like to script the generation passing different preprocessor options (like /DFOO1 /DFOO2 ...). There are msbuild, devenv, nmake, cmake, vcbuild ... This question being asked for more than a decade now!

For unknown reason 'msbuild foo.sln /p:DefineConstants=FOO1 /t:Rebuild', does not work (and '/D FOO1' does not even appear in the cl.exe generated command)! (Even it is not the right solution as I just want to add new defines not overwriting them all).

I am sure cmake will work, but I would like to avoid wasting time generating and maintaining both makefile & foo.sln)

Making several configurations (within foo.sln) for each combination is a nightmare as their number explode.

devenv seems too limited; nmake I don't know; vcbuild is obsolete?

It is quite strange for me to run into this issue in 2022, for this trivial workflow.

Thanks for your help

Using VS2017pro, here with vcvars64.bat

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,618 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RLWA32 43,046 Reputation points

    It's possible with msbuild if you are willing to do a bit of editing to the preprocessor definitions for various projects of interest.

    For example -


    Code for proof of concept to show what macros have been defined -

    #include <stdio.h>  
    #include <tchar.h>  
    int main()  
    #ifdef FOO1  
        puts("FOO1 defined");  
    #ifdef FOO2  
        puts("FOO2 defined");  
    #ifdef FOO3  
        puts("FOO3 defined");  
        puts("All done.");  
        return 0;  

    msbuild command - Dont define any macros.


    msbuild command - Define FOO1


    msbuild command - Define FOO1 & FOO2


  2. BOUGNOUX Sylvain 1 Reputation point

    Thanks for your prompt answer, I knew this trick from ( last answer). I confirm it works now, I should have messed up something during my 1st trial. Thanks again.

    PS: you don't need several $(Custom_i), eg I have $(MyConstants), and use it as '/p:MyConstants="FOO1;FOO2"'

    0 comments No comments