question

BOUGNOUXSylvain-3703 avatar image
0 Votes"
BOUGNOUXSylvain-3703 asked BOUGNOUXSylvain-3703 answered

Command line compilation while augmenting preprocessor defintion

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

vs-msbuild
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

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



foo-preproc.png (14.4 KiB)
nomacros.png (25.7 KiB)
onemacro.png (26.8 KiB)
twomacros.png (27.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

One additional thought -- using a property sheet to set up the custom properties is quicker and easier than editing project properties.

0 Votes 0 ·
BOUGNOUXSylvain-3703 avatar image
0 Votes"
BOUGNOUXSylvain-3703 answered

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"'

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.