specify project file of a solution using msbuild - build

I want the commandline for building a particular project of a solution using msbuild like we do with devenv.com.In devenv.com we can specify a project of a solution using following commandline
devenv.com /Build Release|x86 test.sln /project "testproject"
Using the above commandline i can build the testproject in the test.sln using devenv.com.What is the commandline for msbuild for the same solution.
Thanks

msbuild test.sln /t:project /p:Configuration="Release" /p:Platform="x86" /p:BuildProjectReferences=false
Notice that what is assigned to /t is the project name in the solution, it can be different from the project file name.
Also, as stated in How to: Build specific targets in solutions by using MSBuild.exe:
If the project name contains any of the characters %, $, #, ;, ., (, ), or ', replace them with an _ in the specified target name.
You can also build multiple projects at once:
msbuild test.sln /t:project;project2 /p:Configuration="Release" /p:Platform="x86" /p:BuildProjectReferences=false
To rebuild or clean, change /t:project to /t:project:clean or /t:project:rebuild

MSBuild actually works through the use of projects not the solution. The solution is only used to parse it into a temporary project file in MSBuild internally. You should be able to just build the project of interest directly through MSBuild by executing the following command.
"msbuild testproject /p:Configuration=Release /p:Platform=x86"
There is one major issue I know you could run into using the project directly instead of the solution: if you use the solution to express dependencies between the projects, instead of adding the references to the project and letting the build system work out the dependencies automatically.
If you are enforcing a build order using the sln file, I recommend working those dependencies directly into the proj files and removing them from the sln. This will allow you to invoke any proj file from MSBuild directly and the projects will all build independently without any additional work. You really should treat the sln file as a group of projects to make working in Visual Studio easier and not as a build input.

Posting as information to future seekers
Add the following to the build script and run it once. This will generate the exact targets and other information that msbuild will actually use.
Ex: If you have . in the project name or folders msbuild will expect _ in place of the ..
set MSBuildEmitSolution=1
After getting the information update the build script with the required details.

In order to do this, you need to know what the project's target-name is, not necessarily the project name.
One way to find this out is to use MSBuild against your SLN with intended parameters after setting a special environment variable called MSBuildEmitSolution to the value of 1.
set MSBuildEmitSolution=1
msbuild my_stuff.sln /t:rebuild /p:Configuration=Release /p:Platform=x64
I recently had to do this due to a very specific name for a target in nested directories. So from my generated file, my_stuff.sln.metaproj I found this line:
<Target Name="Utils\Firewall\FirewallUtils:Rebuild">
That means the command-line to use ends up being,
msbuild my_stuff.sln /t:Utils\Firewall\FirewallUtils:Rebuild /p:Configuration=Release /p:Platform=x64

Just to add additional information, executing msbuild in the project folder will by default build the project file since its the only one there.
>msbuild
There are many variations of using msbuild this way. You can specify the proj file directly.
>msbuild helloworld.csproj -t:Build.
Review the msbuild documentation for usage, proj file requirements, as well and the benefits of building the project instead of the solution.
MS MSBuild Documentation
There are benefits to building this way as mentioned by mark-smith above.

Related

How build a project with platform configuration via devenv.exe\com?

I am trying to build a solution via devenv.com and I get errors. The command I use:
set devenvCom=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.com
"%devenvCom%" "C:\...\mySolution.sln" /Build "Release|AnyCPU"
I get an error: "The operation could not be completed"
But if I run it without a platform configuration:
set devenvCom=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.com
"%devenvCom%" "C:\...\mySolution.sln" /Build "Release"
This works fine.
But in their documentation they say that you can use the format: /Build "Release|AnyCPU"
And it doesn't matter if Irun the command using devenv.exe or devenv.com they both fail.
How can I run the command successfuly?
The syntax you have in both of your commands - with and without platform - is correct for devenv.com in Visual Studio 2017, according to Microsoft documentation at https://learn.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe?view=vs-2017.
I run similar commands with devenv.com in VS2019 without any difficulty, via a batch file run as part of a Jenkins job.
The following notes may assist in resolving this issue:
When defining environment variables for file or folder paths, I suggest using double quotes as follows:
set "devenvCom=C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.com"
These double quotes make it easier to ensure you have not added any trailing whitespace to the environment variable value by mistake. The double quotes also ensure that any parentheses or ampersands in the path will not be misinterpreted by the cmd.exe shell.
It is fine to double quote the parameter which follows /Build, e.g. /Build "Release". These double quotes are REQUIRED when specifying the platform such as /Build "Release|AnyCPU", to ensure the vertical bar is not misinterpreted by the cmd.exe shell, or when the build configuration name contains a space.
I recommend /Rebuild rather than /Build when performing automated builds. Only the former is guaranteed to perform a complete build from scratch.
Microsoft documentation states "Insert one space character between switches and arguments on the same line", i.e. put ONE SPACE between the /Build parameter and the value which follows it, e.g. "Release|AnyCPU".
The name of the build configuration and platform, e.g. "Release|AnyCPU", is expected to be found in the specified solution file. These solution build configurations names and platforms do not have to correspond to the project build configuration names and platforms (though they should whenever possible).
If you wish to build a single project only, I have been successful with the following syntax in VS2019:
"%devenvCom%" "C:\...\myProject.vcxproj" /Build "Release|AnyCPU" /Project "myProject"
Note that the first parameter in the above example is a project file, rather than a solution file. I believe that the specified build configuration and platform refer to the project rather than the unspecified solution (note: Visual Studio may create a temporary solution when performing single project builds if it cannot find a solution that corresponds to the specified project).
For single project builds, I previously had success specifying the solution file first, and the project file after /Project, but this previous syntax does not consistently succeed in VS2019 build 16.11.2 - it can lead to devenv.exe remaining running indefinitely after the project builds successfully (devenv.exe gets launched by devenv.com).
Your VS2017 installation may be corrupt, or you may have an extension that is interfering with VS2017.

How do I build all configurations of a Visual Studio 2008 C++ project on the command line?

I'd like to build all the configurations of a VS 2008 C++ project on the command line. Something like:
devenv TheProject.vcproj /build /nologo
But this doesn't work because the /build command insists on having a configuration following it like this:
devenv TheProject.vcproj /build "Release|Win32" /nologo
Is there a way to get a command line build of all configurations within the vcproj file?
I was thinking you can do what you want with MSBUILD, but it appears that it isn't much better for this than DEVENV.
You still have to specify each configuration on the command line, although it would be easy to write a batch file to accomplish this with either MSBUILD or DEVENV.
It looks like previous versions of the development environment may have supported an "ALL" option, but VS 2008 does not.
http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/8701b3d0-d5c9-45fb-8dd4-e7700c8caca6/
Not directly, but you can have projects depend on other projects - so you could have an 'all' or 'install' project with a dependacy of everything else.
Haven't used VS in a long time. But the project properties panel used to show the command line generated for linking and compiling a project for a particular configuration. It used to be under the Advanced tab. Will using that directly from the command line serve your purpose? This method will not use the VS IDE at all.
Alternatively,
Steps:
Create a project which has a dependency on all other projects.
Write a script which builds this project with different configurations sequentially. You cannot create a single configuration which encapsulates all other configurations.

More with eclipse cdt

What is done when we import an existing project(maybe a visual studio project)?
Which files are used for configuration?
Try this one, you may get some information.
Migrate Visual Studio C and C++ projects to Eclipse CDT
Eclipse manages files completely differently than Visual Studio, files are managed by Eclipse and placed into the project workspace. Adding existing files has the aggravating effect of copying the files from their location into the workspace. There are workarounds for this (adding a link to existing file, makefile-only projects) but the default is to copy files around.
This is great if your project is managed by Eclipse alone, not great if you want a VS project AND an Eclipse project for the same codebase.
I'm don't think you can import a VS project into Eclipse CDT, at least not the way you're thinking.
The files used for configuration are stored in (path to workspace)/.metadata, there are a LOT of files that change constantly and can contain absolute paths. Caveat emptor.
There is no explicit wizard for importing visual studio project files. What you import is a directory tree full of source code files. Basic information about this is worked out and stored in a file called .project, which contains the settings from Project/properties.
If, when you create or import the project, 'use default location' is specified, the tree is copied into a workspace directory. If not, it is left where it is.
If the project type is 'makefile project', the only real assumption is that there is some external command to be run to build the software. Project properties/C++ build can be used to specify this command - by default, it is 'make'.
DevStudio can export a makefile for one of it's projects - from the Project menu, select 'Export Makefile'. Or you can just write one by hand, or use some other build tool such as ant.
If all else fails, set the build command to 'cat' (e.g. from cygwin) and the build argument to the name of a file that contains the output log from however the software was built.
Eclipse itself has two kinds of projects - those with makefiles, and those that it manages itself.
Makefile projects have a separate make file that you generally write on your own.
Eclipse managed projects have a .project file that is used by the IDE to create make files on the fly, when you build your project.
Are you asking specifically for visual studio projects, or is that just an example?

How to create Visual studio solution from make files?

I have a source code for a project with their make files. I want to create a Visual Studio (2005) solution from it. Is there any direct way to do this? can anyone help me please. I spent hours for searching, but couldn't find a way to do this.
Thanks.
Unfortunately, Microsoft removed this capability after VC++ 6.
If all you're looking to do is to build a Visual Studio project from a command line or script, you can use the devenv command to build using the settings in a project.
Something like:
devenv /build debug /project myproj myapp.sln
Ans starting with VS2010, C++ projects will use the MSBuild system, so you can drive builds using that technology.
If you really want a makefile, you'll need to write it up by hand (or maybe there's some 3rd party tool out there that I'm unaware of).
I'm not sure whether this solution can help you. Which I tried and it worked well in my previous projects. It need manually add the files.
Create a blank VS solution/project. Add the source files into that project.
Mark all source files as "Excluded from building". You can right click the files in project explorer and find the setting. So now nothing will happen when you build your project.
In project setting, find something like "Custom build step". Add the commands that invoke your original build command. (You may write different build command for debug/release ). You can also set the post-build actions such to copy your result to some folder....
Now you can edit and build source files.
For my experience, I can even debug it after setting the executable.
Hope this can help you.
If this is a one-off then it is easier to just create the VS project manually in visual studio.
If you are going to need to do this often look at ceating the project in something like cmake or Qt's .pro whcihc an generate makefiles and VS build files from the same defintion.
Do you want to use the makefile to build? You can create a project from existing source in VS 2005 and setup the project to use make to build (and the wizard will take you through all of this).
I am using VS2010.In order to build you can create a project from existing code. In VS2010 you can create project from existing code File->New->Project from Existing code. You can specify the other parameters and then ready with the solution. I did not go with make file but followed this approach which is working great.

How can I build all with MSBuild from the command line?

Is this valid?
MSBuild /t=all /configuration=all
I want to build ALL configurations of all projects in a sln file, etc from the command line using MSBuild in Visual Studio 2008.
I do not want to have to specify them when I call MSBuild, the sln/proj files all have that information. I don't want to change my build script if I add configurations to project files.
So for the target I can use BuildAll. If I leave the configuration empty will it build all or is "BuildALL" valid for configuration as well?
EDIT
essentially what I am asking is given an SLN or VCProj file, I want msbuild to iterate all configurations and build it itself, or alternatively some mechanism that will discover them so I don't have to specifically list them on the command line or in a script.
i.e. I don't want to update my build script when I add or remove a configuration. That seems like a pretty reasonable thing to want to do.
You can't by default build all configurations using MSBuild command line options. In order to do this you need to create a new target (VS Project).
The way I do it is:
msbuild /t:BuildAll /Configuration:"Debug;Release;ContinuousIntegration"
I make a standard Target, and call it BuildAll, and for every project I wanted to automate, I'd just create that Target and make it depend on all the targets you want to build automatically.