How to use makefiles in Visual Studio? - c++

I heard a lot about makefiles and how they simplify the compilation process. I'm using VS2008. Can somebody please suggest some online references or books where I can find out more about how to deal with them?

The Microsoft Program Maintenance Utility (NMAKE.EXE) is a tool that builds projects based on commands contained in a description file.
NMAKE Reference

A UNIX guy probably told you that. :)
You can use makefiles in VS, but when you do it bypasses all the built-in functionality in MSVC's IDE. Makefiles are basically the reinterpret_cast of the builder. IMO the simplest thing is just to use Solutions.

To answer the specific questions...
I'm using VS2008. Can somebody please suggest some online references
or books where I can find out more about how to deal with them?
This link will give you a good introduction into Makefiles by mapping it with Visual Studio.
Introduction to Makefiles for Visual Studio developers
I heard a lot about makefiles and how they simplify the compilation
process.
Makefiles are powerful and flexible but may not be the best solution to simplify the process. Consider CMake which abstracts the build process well which is explained in this link.
CMake for Visual Studio Developers

I actually use a makefile to build any dependencies needed before invoking devenv to build a particular project as in the following:
debug: coratools_debug
devenv coralib.vcproj /build debug
coratools_debug: nothing
cd ../coratools
nmake debug
cd $(MAKEDIR)
You can also use the msbuild tool to do the same thing:
debug: coratools_debug
msbuild coralib.vcxproj /p:Configuration=debug
coratools_debug: nothing
cd ../coratools
nmake debug
cd $(MAKEDIR)
In my opinion, this is much easier than trying to figure out the overly complicated visual studio project management scheme.

The VS equivalent of a makefile is a "Solution" (over-simplified, I know).

To summarize with a complete solution...
There are 2 options:
Use NMAKE from the Developer Command Prompt for Visual Studio
The shortcut exists in your Start Menu. Look inside the makefile to see if there are any 'setup' actions. Actions appear as the first word before a colon. Typically, all good makefiles have an "all" action so you can type: NMAKE all
Create a Solution and Project Files for each binary.
Most well designed open source solutions provide a makefile with a setup action to generate Visual Studio Project Files for you so look for those first in your Makefile.
Otherwise you need to drag and drop each file or group of files and folders into each New Project you create within Visual Studio.
Hope this helps.

Makefiles and build files are about automating your build. If you use a script like MSBuild or NAnt, you can build your project or solution directly from command line. This in turn makes it possible to automate the build, have it run by a build server.
Besides building your solution it is typical that a build script includes task to run unit tests, report code coverage and complexity and more.

If you are asking about actual command line makefiles then you can export a makefile, or you can call MSBuild on a solution file from the command line. What exactly do you want to do with the makefile?
You can do a search on SO for MSBuild for more details.

Related

Is there any way to generate nmake from Visual Studio?

I want to build many c++ projects in Windows 7,
however building them one by one seems fantastic.
Then, I thought to write NMAKE file manually and run some script,
but it seems also troublesome because I have to write many NMAKE files.
So I want to know is it possible to generate NMAKE file automatically from Visual Studio 2010.
Starting with Visual Studio 2002, there is no longer a way to export your VC++ project files to a Makefile. (That option existed up until VC++ 6.0 but was removed.)
As of VS 2010, the build process for VC++ projects is the same as any other projects: there is a solution file and a set of project files, both of which are recognized by MSBuild as inputs. MSBuild take care of the dependency resolution, build ordering, etc.
The only significant feature that you lose building inside the IDE is parallel builds. If that's your concern, you can run the msbuild command on your solution file and pass in the /m parameter to specify how many simultaneous builds it should attempt.
As far as I know, no one has built any kind of tool or plug-in to parse the .vcxproj files and spit out a makefile, though the file format is pretty well documented XML, so you could always write one yourself. Otherwise, if you really need makefiles for some reason, you'll have to craft them by hand.
You can use MSBuild to build a Visual Studio solution or project from the command line.
Convert it to CMake https://stackoverflow.com/a/9387350/525578
Generate using NMake or Jom generator of the CMake

How to deal with commits from different development environments?

I decided to convert my Visual Studio project to CMake to develop on multiple platforms. But I wonder how to effectively apply versioning to a CMake project.
Say, from the CMake project I generated a Visual Studio project on Windows, and set up a make file on Linux. I don't want to include those platform specific files in commits. Is it best practice to exclude all those files using .gitignore?
This is what first came to my mind, and I would like to know if this is how its done right. For example, I could also include projects for all different platforms in my repository.
Yes, if you use CMake as a base project file, by all means do not add the generated files to the repository. They're effectively useless as a shared thing and often very computer-specific.
As said in the comments (by #Peter, which I shamelessly copy here for better visibility), it is often a good idea to build outside your source tree (unlike the default behavior of Visual Studio). Basically, you do (from the source directory)
cd ..
mkdir project-build && cd project-build
cmake ../project
You can tell CMake to use a specific "generator" with the -G commandline option. Check its help output for details. You can also do this through CMake's GUI.

Replacement for CMake

I'm using CMake into build rules in visual studio to preprocess my files before submitting them to the compiler but my customers don't like it.
Is there any CMake alternative or method to preprocess my files and having them integrated into visual studio?
As I understand the question you are using CMake to generate files consumed by a VS project that you created by hand. For whatever reason your customers of your VS project don't like CMake. Presumably you have wired this up by creating custom build steps that invoke CMake to generate the files in your project, either as Build Events on the project, or as custom build steps on various files in the project, such as the inputs to the scripts.
VS lets you run any arbitrary set of commands as part of a Build Event or a Custom Build Step. So you could replace CMake with a suitable script or custom executable written by yourself. It is hard to be specific without knowing exactly what about CMake isn't appealing to your customers.
You can use JavaScript through Windows Script Host to create fairly complex scripts that process custom build steps. They can take command-line arguments, access the file system, etc. For some reason WSH JavaScript is a big secret among most developers and they think that all they have available to them in Windows out-of-the-box are DOS batch files and power shell scripts. JavaScript has been shipping for over 10 years as part of the OS and you can debug the scripts in Visual Studio's script debugger, which is very nice.

TeamCity MSbuild C++ only build some solutions

In our C++ project we have several solutions, is there a way to only build select solutions in TeamCity. Or is there parameters for msbuild that can accomplish this?
We are using .Net v4.0
Thanks in advance.
Another possibility is to call MSBuild just on your vcxproj files, not the whole sln files.
We use it very often - we have build batch files compiling just vcxproj files. With sln files we work only in VS IDE.
I guess you have several projects in your solution?
You can create a build configuration (something like Debug/Release) for the whole solution und choose which projects should be built in this. You can pass this configuration's name to Teamcity.

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.