4

I'm building some projects using cmd devenv.

For some projects I want to #define form the command line.

Is there a way to #define using the cmd and devenv? I don't want to create new configurations.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
alejo
  • 41
  • 1
  • 2
  • A "better" answer can be found at http://stackoverflow.com/questions/1783158/how-do-i-add-preprocessor-define-in-devenv-command-line ... you can set an environment variable CL with whatever extra command line options you want cl.exe to use during the build process, such as this: set CL=/DSOMETHING=1 – CasaDeRobison Jan 09 '14 at 21:38

3 Answers3

6

You could do this:

  • Add $(AdditionalPreprocessorDefinitions) to the list of preprocessor definitions of the project. After that, they might look like WIN32;_DEBUG;_CONSOLE;$(AdditionalPreprocessorDefinitions);%(PreprocessorDefinitions)

  • Use msbuild instead of devenv to start the build process

  • Specify the additional preprocessor definitions by adding /p:AdditionalPreprocessorDefinitions="SOMEDEF;ANOTHERDEF" to the command line.

Please not that I picked the name AdditionalPreprocessorDefinitions just for clarity, you can pick something shorter. This will only work with VC2010, because in earlier versions, VC++ projects were not built with msbuild.

Timbo
  • 27,472
  • 11
  • 50
  • 75
  • I tried using the msbuild using /p:Definition="TEST" but it didn't work it tried /p:DefineConstants="TEST" didn't work neither What i'm doing wrong? I'm compiling a solution with 3 projects – alejo Jun 17 '11 at 20:03
  • Did you follow step 1 of the list above? The DefineConstants thing is C# only, in this solution for C++ you have to edit the project and insert $(VariableName); into the preprocessor definitions string. – Timbo Jun 17 '11 at 20:51
  • Yes I tried adding the $(Flavors) to the preprocessor definitions now it looks:WIN32;NDEBUG;_WINDOWS;%(Flavors);%(PreprocessorDefinitions) and I use the command msbuild project.sln /p:Flavors="Test" /t:rebuild It's still not working – alejo Jun 20 '11 at 14:45
  • @alejo try replacing %(Flavors) with $(Flavors) – Timbo Jun 20 '11 at 16:55
  • now it's working fine, That was a really bad mistake, Thank you for your help – alejo Jun 20 '11 at 17:40
1

Many compilers allow you to set define macros at the command line. The common option is "-D".

Check your compiler documentation for exact syntax.

Edit 1: The Visual Studio compiler has configuration options to specify the macros in the IDE (which get passed to the compiler using the "-D" switch.)

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

You can not do this with devenv.exe, see the Command Line Options. You can do this with the compiler however, cl.exe but then you can't reuse all the settings in your project file.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
jcopenha
  • 3,935
  • 1
  • 17
  • 15
  • I tried using the msbuild using /p:Definition="TEST" but it didn't work it tried /p:DefineConstants="TEST" didn't work neither What i'm doing wrong? I'm compiling a solution with 3 projects – alejo Jun 17 '11 at 20:03