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

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.

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.

specify project file of a solution using msbuild

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.

Add a #define in C++ using cmd

I'm trying to automatically build different flavors of my project.
I'm using the command line to compile the project.
All is working fine but now I want to define a macro in the cml (example : TEST_PROJECT).
I tried using the CL environment variable but it doesn't seem to work
There is an other way to add options to my project
I'm using devenv.exe to compile
devenv.exe project.sln /build
thank you
There's /D for cl.exe in Visual C++. However since you already build .sln file you'll be much better off setting the necessary preprocessor symbol in the project properties. You might want to add a separate project configuration for that.

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.