2

I am trying to build a visual studio solution from the windows command line as follows:

msbuild solution.sln /t:Build /p:Configuration=Release 

But I need to exclude a certain directory from one of the projects, and include another one. How do I do that?

baruchiro
  • 5,088
  • 5
  • 44
  • 66
RandomName
  • 163
  • 1
  • 10
  • Do you want another folder to be in the project? Or just when you compile with `msbuild` from command line? – baruchiro Jan 23 '19 at 15:36
  • For every build configuration, there is a folder containing cpp files. When building, I would like to include the folder of the current build configuration, and exclude the folders of the other configuration. Right now I have to do that manually when building from visual studio (right click on folders then press on include/exclude from project). But if your solution would automate that for me too, then great. – RandomName Jan 24 '19 at 08:35
  • Good, try my solution and update us – baruchiro Jan 24 '19 at 11:22
  • @RandomName As Baruch said, whether the solution helps? If it does help, you could accept it as the answer, so it could help other community members who get the same issues and we could close this thread. And if you need some further support to solve the issue, please feel free to contact us. – LoLance Jan 29 '19 at 03:02
  • @LanceLi-MSFT I have not been able to check this yet, but I will be able to by the end of the week and will update the thread. Sorry for the delay. – RandomName Jan 29 '19 at 12:38
  • @RandomName Long time haven't heard from you.Does it work? And I add an answer with more details below. Hope it helps. – LoLance Feb 07 '19 at 02:14

2 Answers2

3

Use this in your project csproj file:

<Compile Include="Folder1/*" Condition=" '$(Configuration)' == 'Release' " />
<Compile Exclude="Folder2/*" Condition=" '$(Configuration)' == 'Release' " />

Of course, you already set up the Configuration property, but you can replace it with your own property and set him from command line same as Configuration.

baruchiro
  • 5,088
  • 5
  • 44
  • 66
1

In the project dir, add two folders "CppFilesForDebug" and "CppFilesForRelease".

The "CppFilesForDebug" folder contains "Debug1.cpp,Debug2.cpp,Debug3.cpp" while "CppFilesForRelease" folder contains "Release1.cpp,Release2.cpp,Release3.cpp ".

Then add statement in the bottom of the vcxproj file like red rectangle below: enter image description here

The red rectangle means we build and compile cpp files in "CppFileForDebug" folder when using Debug mode. And in release mode, we only compile cpp files in "CppFilesForRelease" folder insteat of cpp files in "CppFilesForDebug" folder.

I test it with C++ projects in VS2015 and VS2017 and it works. I think it can go well with your command "msbuild solution.sln /t:Build /p:Configuration=Release". Please it a try and any feedback would be great.

Update: IF your issue results from QT conflicts, In vs, go QT menu ->Qt Project Settings->MocDirectory-> change it to

.\GeneratedFiles

Note: Don forget the "." before \GenerateFiles. Hope it helps. Any update you can share here.

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Hey again. I finally got around to testing it, but it doesn't work. When switching between configurations and back (e.g. Build Debug -> Build Release -> Build Debug), I get the following warnings: **warning MSB8027: Two or more files with the name of moc_file1.cpp will produce outputs to the same location. This can lead to an incorrect build result. The files involved are GeneratedFiles\Debug\moc_file1.cpp, GeneratedFiles\Release\moc_file1.cpp.** – RandomName Feb 08 '19 at 14:40
  • This occurs even after adding the following in the project file: **true**. It appears that in the project file, when building in Debug for example, the Include Debug\*.cpp line I add is automatically replaced with the actual files includes. And those includes remain there when switching to another configuration. – RandomName Feb 08 '19 at 14:42
  • I've just tried (e.g. Build Debug -> Build Release -> Build Debug) and my solution still works well. But my project type is Windows Console Application(C++). And after i search the error message on the Internet, your issue seems to be more related to [QT path conficts](https://stackoverflow.com/questions/24072041/why-is-my-moc-dir-ignored-when-generating-visual-studio-projects-with-qmake/24072042#24072042). – LoLance Feb 11 '19 at 09:42
  • You can build a simple C++ project to test with the solution to check if it works. And if your issue results from QT conflicts, In vs, go QT menu ->Qt Project Settings->MocDirectory-> change it to .\GeneratedFiles Note: Don forget the "." before \GenerateFiles. Hope it helps. Any update you can share here. – LoLance Feb 11 '19 at 09:49
  • But won't changing it to .\GeneratedFiles instead of .\GeneratedFiles\$(ConfigurationName) cause it to compile both release and debug mocs into the same folder, and so overwriting everything? Then I won't have the separate folders to include/exclude based on the build configuration – RandomName Feb 14 '19 at 08:08