Command line compilation while augmenting preprocessor defintion

BOUGNOUX Sylvain 1 Reputation point
2022-05-05T15:37:29.643+00:00

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
    2022-05-05T18:02:13.077+00:00

    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 -

    199343-foo-preproc.png

    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");  
    #endif  
    #ifdef FOO2  
        puts("FOO2 defined");  
    #endif  
    #ifdef FOO3  
        puts("FOO3 defined");  
    #endif  
        puts("All done.");  
        return 0;  
    }  
      
    

    msbuild command - Dont define any macros.

    199290-nomacros.png

    msbuild command - Define FOO1

    199323-onemacro.png

    msbuild command - Define FOO1 & FOO2

    199299-twomacros.png


  2. BOUGNOUX Sylvain 1 Reputation point
    2022-05-06T08:06:02.51+00:00

    Thanks for your prompt answer, I knew this trick from (https://stackoverflow.com/questions/485237/define-a-preprocessor-value-from-command-line-using-msbuild 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