How to “get rid of” $(SolutionDir) when building Visual Studio project from outside Visual Studio

你。 提交于 2019-12-20 10:39:47

问题


I want to build a .vcxproj via MSBuild from outside Visual Studio. The problem is that there are many occurrences of $(SolutionDir) inside the .vcxproj file which apparently only get set correctly when the solution is loaded.

When I replace $(SolutionDir) with the actual absolute path, it works. But other people on other machines are working with the same project file, so this is not a solution.

Is there a solution or hack to solve this problem?


回答1:


You can set the variable by passing parameter arguments:

/p:SolutionDir=path

So, rather than editing the solution file, you can create a build script that sets up the environment and executes MSBuild accordingly, leaving the Visual Studio file as is for development work.




回答2:


You can use a relative path.

I am not sure about Visual Studio 2010 as in the question, but it guaranteed works since Visual Studio 2012.

If the project folder is right under the solution folder then the relative path is ..\\.

This, by the way, can be combined with the parameter argument from Grant Thomas's answer.

<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>



回答3:


The solution to explicitly pass SolutionDir as an argument is insufficient for me.

Namely, if the project I want to build has dependencies with different build configurations, the build fails with a This project doesn't contain the Configuration and Platform combination of <my target configuration> error.

This makes sense, since I didn't specify the target configuration for the dependencies and thus it tries to target same configuration as the project I'm building, which they don't have (in my case).

Since a solution file contains the build configurations for all projects in the solution (given some solution-configuration), we want to make use of this solution to solve this problem. Luckily, MSBuild allows us to build a solution and pass a specific target project as argument, which will only cause that project to be build (together with its dependencies).

msbuild MySolution.sln /t:MyProject


来源:https://stackoverflow.com/questions/15053915/how-to-get-rid-of-solutiondir-when-building-visual-studio-project-from-outs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!