How to create an installer with CMake + CPack + NSIS on Windows?

sklum picture sklum · Oct 30, 2012 · Viewed 26.4k times · Source

I'd like to create a cross-platform installer for a C++ based system I am building.

I use CMake to build everything, and it would be great if I could use CPack to make the installer. I already have CPack working on OSX, but I cannot get it to work on Windows. To make things easier, I tried to get the example at http://www.cmake.org/Wiki/CMake:Packaging_With_CPack to work with the NSIS installer software. I cannot find the NSIS installer anywhere after configuring (with VS 2010 Win64 generator).

Maybe I am confused, but I thought it would be possible to create the installation package with only the source, CMake, CPack, and NSIS without any need for Visual Studio. Is this possible?

A link to a full tutorial (the one at http://www.cmake.org/Wiki/CMake:Component_Install_With_CPack skips over the relevant information to get NSIS working and doesn't mention generators or compilers) would be very helpful, or a basic explanation of how I can get to this mythical generated NSIS installer would be great.

Here is CMakeLists.txt for the example above:

cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR)
project(StPMS)

add_library(mylib mylib.cpp)

add_executable(mylibapp mylibapp.cpp)
target_link_libraries(mylibapp mylib)

 install(TARGETS mylib 
   ARCHIVE
   DESTINATION lib
   COMPONENT libraries)
 install(TARGETS mylibapp
   RUNTIME
   DESTINATION bin
   COMPONENT applications)
 install(FILES mylib.h
   DESTINATION include
   COMPONENT headers)

set(CPACK_GENERATOR NSIS)
set(CPACK_PACKAGE_NAME "MyLib")
set(CPACK_PACKAGE_VENDOR "CMake.org")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MyLib - CPack Component Installation Example")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_VERSION_MAJOR "1")
set(CPACK_PACKAGE_VERSION_MINOR "0")
set(CPACK_PACKAGE_VERSION_PATCH "0")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "CPack Component Example")
SET(CPACK_NSIS_MODIFY_PATH ON)

INCLUDE(CPack)

Answer

Fraser picture Fraser · Oct 31, 2012

... I thought it would be possible to create the installation package with only the source, CMake, CPack, and NSIS without any need for Visual Studio. Is this possible?

Kind of. It depends on what you mean by "without any need for Visual Studio". You need a build tool to actually create the lib and exe. On Windows, you need something like Visual Studio's msbuild, especially if you specified "Visual Studio 10 Win64" as the generator.

If you mean "without running Visual Studio", then the answer is yes. You can have CMake execute your chosen build tool using the --build argument.

After running CMake, you end up with a file PACKAGE.vcxproj in your build directory. It is building this which will create the installer. You can either build the PACKAGE project from inside Visual Studio, or you can invoke msbuild directly by doing:

msbuild /P:Configuration=Release PACKAGE.vcxproj

from your build directory in a VS command prompt.

This should yield your installer named MyLib-1.0.0-win64.exe, also in your build directory.


If you want to just use CMake, then an alternative to invoking msbuild is:

cmake --build . --target PACKAGE.vcxproj --config Release


Or you can build the solution first, then invoke CPack to create the installer:

cmake --build . --config Release
cpack -C Release