views:

2756

answers:

7

In a C++ file, I have a code like this:

#if ACTIVATE
#   pragma message( "Activated" )
#else
#   pragma message( "Not Activated")
#endif

I want to set this ACTIVE define to 1 with the msbuild command line.

It tried this but it doesn't work:

msbuild /p:DefineConstants="ACTIVATE=1"

Any idea?

+3  A: 

I think you want:

/p:DefineConstants=ACTIVATE
Matt Howells
@Matt, I suggested the same answer and was told by two commenters that this doesn't work. So I deleted my answer so that others with more experience with MSBuild might offer ideas.
Onorio Catenacci
Oh and acemtp told me that he already tried this and it didn't work.
Onorio Catenacci
Well it works fine in C# anyway...
Matt Howells
This works for me as well. I did put quotes around the list of constants i defined but that probably doesn't matter.
blak3r
+1  A: 

It should probably be:

#ifdef ACTIVATE
#   pragma message( "Activated" )
#else
#   pragma message( "Not Activated")
#endif
Robert
@rdeml--good idea.
Onorio Catenacci
No it's not #ifdef, it's a #ifAnd anyway, it doesn't work with #ifdef too
acemtp
+1  A: 

C++ projects (and solutions) are not (yet ?) integrated in the MSBuild environment. As part of the build process, the VCBuild task is called, which is just a wrapper around vcbuild.exe.

You could :

  • create a specific configuration for your solution where ACTIVATE=1 would be defined, and compile it with devenv.exe (with the /ProjectConfig switch).
  • create your own target file to wrap your own call to the VCBuild task (see the Override parameter)...
  • use vcbuild.exe instead of msbuild.exe. (vcbuild.exe does not seem to have the equivalent of an Override parameter).

Note that your solution would not work for C# projects either unless you tweaked your project files a bit. For reference, here is how I would do this :

  • Add the following code before the call to <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> :
<PropertyGroup Condition=" '$(MyConstants)' != '' ">
  <DefineConstants>$(DefineConstants);$(MyConstants)</DefineConstants>
</PropertyGroup>
  • Call MSBuild like this :
msbuild /p:MyConstants="ACTIVATE=1"
Mac
I can use vcbuild.exe instead of msbuild.exe but the question is the same. How to set a specific preprocessor in the vcbuild command line?
acemtp
You're right : I wrongly assumed that vcbuild.exe had the same set of parameters as the VCBuild task. Answer updated.
Mac
A: 

The answer is : YOU CANNOT

acemtp
Shrug, perhaps it doesn't in C++ but Matt Howells answer did work for me with a C# project. Can't think of any reason it wouldn't work in C++.
blak3r
A: 

I needed to do this too - needed to be able to build two different versions of my app and wanted to be able to script the build using VCBUILD. VCBUILD does have the /override command line switch, but I am not sure it can be used to modify #define symbols that can then be tested using #if conditional compilation.

The solution I cam up with was to write a simple utility to create a header file that #defined the symbol based on the state of an environment variable and run the utility from a pre-build step. Prior to each execution of the VCBUILD step the script sets the environment variable and "touches" a file in the app to ensure that the prebuild step is executed.

Yes, it is an ugly hack, but it was the best I could come up with!

Bruce Ikin
A: 

Is there specific documentation of this somewhere? I know there are several references that can be used to "connect the dots"(MSBuild Wiki & VS10 announcments), but I need something very clear and concise.

Randolph
A: 

This is a duplicate of this or this.

in fact, the 2 others are duplicated from this one (oldest)
acemtp