0

I have an empty c++ project containing one file which says

#ifndef FOO
#error
#endif
void main(){}

I want to compile this using msbuild by passing the constant FOO to msbuild. According to this post it can be done with the command

$ msbuild /property:DefineConstants=FOO /t:Rebuild

in some shell. However, this gives me the error

Project "C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1.sln" on node 1 (Rebuild target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|x64".
The target "BeforeGenerateProjectPriFile" listed in a BeforeTargets attribute at "C:\Program Files (x86)\MSBuild\Microsoft\NuGet\Microsoft.NuGet.targets (186,61)" does not exist in the project, and will be ignored.
Project "C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1.sln" (1) is building "C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1\Project1.vcxproj" (2) on node 1 (Rebuild target(s)).
_PrepareForClean:
  Deleting file "x64\Debug\Project1.tlog\Project1.lastbuildstate".
InitializeBuildStatus:
  Touching "x64\Debug\Project1.tlog\unsuccessfulbuild".
ClCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"x64\Debug\\" /Fd"x64\Debug\vc140.pdb" /Gd /TP /errorReport:queue Test.cpp
  Test.cpp
c:\users\chiel.tenbrinke\documents\visual studio 2015\projects\project1\project1\test.h(3): fatal error C1189: #error: [C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1\Project1.vcxproj]
Done Building Project "C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1\Project1.vcxproj" (Rebuild target(s)) -- FAILED.
Done Building Project "C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1.sln" (Rebuild target(s)) -- FAILED.

Build FAILED.

"C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1.sln" (Rebuild target) (1) ->
"C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1\Project1.vcxproj" (Rebuild target) (2) ->
(ClCompile target) ->
  c:\users\chiel.tenbrinke\documents\visual studio 2015\projects\project1\project1\test.h(3): fatal error C1189: #error: [C:\Users\Chiel.tenBrinke\Documents\Visual Studio 2015\Projects\Project1\Project1\Project1.vcxproj]

    0 Warning(s)
    1 Error(s)

I tried all kind of combinations like

$ msbuild /property:DefineConstants="FOO=1" /t:Rebuild

but that didn't help.

Using the command

$ msbuild /property:PreprocessorDefinitions="FOO" /t:Rebuild

gives the same output.

Community
  • 1
  • 1
chtenb
  • 14,924
  • 14
  • 78
  • 116
  • "Compilation constants" are a VB.NET and C# concept, languages that quite intentionally avoided macros. In C++ you define macros with the `` element. Generates the `/D` command line argument to the compiler. Easy to see if you use the IDE to set them and look what happens with the .vcxproj file. – Hans Passant Mar 08 '16 at 10:54
  • Thanks for pointing out the difference. However, `msbuild /property:PreprocessorDefinitions="FOO" /t:Rebuild` doesn't work out of the box... – chtenb Mar 08 '16 at 11:11
  • Well, all we know is what happens when you tried to do it the wrong way, we can't see what happened when you tried to do it correctly. Some notes about why you are trying to punish yourself like this would be wise as well. – Hans Passant Mar 08 '16 at 11:15
  • Answer updated. I want to see if a solution compiles when a certain preprocessor directive is defined. Passing it on the commandline as described in the linked post seemed to be an easy way to do this. Your wording about me punishing myself suggests that you think this is the wrong way though xD. Is it? – chtenb Mar 08 '16 at 11:27
  • Ok, so in conclusion, DefineConstants is not supported for C++ projects, and the PreprocessorDefinitions alternative can not be passed from the commandline.. (At least that's what that linked post says). Will mark this as duplicate then. – chtenb Mar 09 '16 at 09:07

0 Answers0