Using CMake with MSVC

CMake doesn't build code. CMake "Generates" a build environment for different build systems. The build system performs the build.

Run cmake --help to get a list of options including the build "Generators" which you define with option -G "name of generator". Generators are provided for unix makefiles as well as popular IDEs such as Visual Studio, Eclipse, Xcode and others.

You want to execute a command such as c:\> cmake -G "Visual Studio Visual Studio 2005" to generate MSVC .sln and .vcproj files for a 32bit build using Visual Studio 2005. The Generator -G "Visual Studio 2005 Win64" generates for a 64bit build.

There’s a batch file <exiv2-dir>/cm.bat to help you build with CMake”

c:\> cd c:\gnu\exiv2\trunk
c:\gnu\exiv2\trunk> vcvars32.bat   *** see Note 1 ***
c:\gnu\exiv2\trunk> cm.bat 2005 64 *** see Note 2 ***
… rattle roll …
… generates Visual Studio projects and stuff …
c:\gnu\exiv2\trunk> open exiv2.sln *** see Note 3 ***
… starts up Visual Studio …
… select the BUILD_ALL target in the project explorer (on the left)
... Build (F7 or Alt-B)
… rattle rattle rattle ...
c:\gnu\exiv2\trunk> devenv /build exiv2.sln *** see Note 3 ***

Note 1

vcvars32.bat is a Microsoft script that’s installed with Visual Studio. You have to run that in DOS to set some environment strings. It’s a little different for every version of Visual Studio.

You can see me calling him for MSVC 2005 in jenkins_build.bat. I still have work to get jenkins_build.bat to build for MSVC versions other than 2005.

Note 2

cm.bat 2005 64 => cmake -G “Visual Studio 2005 Win64”
Generates a 64 bit build environment

cm.bat 2005 => cmake -G “Visual Studio 2005"
This would be a 32 bit build environment

You can pass other parameters via cm.bat to tell him to build static/dynamic, release/debug and stuff.
We need those additional parameters for “out of source” builds * see Note 4 *

Note 3

devenv.exe is Visual Studio.
When you say devenv exiv2.sln, he opens a window and you use the UI to tell him to build
When you say devenv exiv2.sln /build Release - he runs the Release build from the command line
(You’ll see that being use in jenkins_build.bat)

Note 4

Out of source builds.
I suspect this isn’t working very well (could be broken)
Out of source is when you build in a different tree from the source.
For example:
cd c:\gnu\exiv2
mkdir build
cd build
..\trunk\cm.bat 2005 64 ..\trunk # generate a Visual Studio 2005 Win64 build file using the source in ..\trunk

Updated by Robin Mills about 7 years ago · 3 revisions