C++ 如何在 Windows 上使用 CMake + CPack + NSIS 创建安装程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13144181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:01:31  来源:igfitidea点击:

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

c++installercmakensiscpack

提问by sklum

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

我想为我正在构建的基于 C++ 的系统创建一个跨平台安装程序。

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_CPackto work with the NSIS installer software. I cannot find the NSIS installer anywhere after configuring (with VS 2010 Win64 generator).

我使用 CMake 来构建所有东西,如果我可以使用 CPack 来制作安装程序就太好了。我已经在 OSX 上使用了 CPack,但我无法让它在 Windows 上工作。为了让事情更简单,我尝试在http://www.cmake.org/Wiki/CMake:Packaging_With_CPack获取示例以使用 NSIS 安装程序软件。配置后我在任何地方都找不到 NSIS 安装程序(使用 VS 2010 Win64 生成器)。

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?

也许我很困惑,但我认为可以创建仅包含源代码、CMake、CPack 和 NSIS 的安装包,而无需任何 Visual Studio。这可能吗?

A link to a full tutorial (the one at http://www.cmake.org/Wiki/CMake:Component_Install_With_CPackskips 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.

完整教程的链接(http://www.cmake.org/Wiki/CMake:Component_Install_With_CPack跳过相关信息以使 NSIS 工作并且没有提及生成器或编译器)将非常有帮助,或者关于我如何获得这个神话般的生成 NSIS 安装程序的基本解释会很棒。

Here is CMakeLists.txt for the example above:

这是上面示例的 CMakeLists.txt:

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)

回答by Fraser

... 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?

...我认为可以仅使用源代码、CMake、CPack 和 NSIS 创建安装包,而无需任何 Visual Studio。这可能吗?

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.

的种类。这取决于您所说的“不需要 Visual Studio”是什么意思。您需要一个构建工具来实际创建 lib 和 exe。在 Windows 上,您需要像 Visual Studio 的 msbuild 之类的东西,尤其是当您指定"Visual Studio 10 Win64"为生成器时。

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

如果您的意思是“不运行 Visual Studio”,那么答案是肯定的。您可以使用--build参数让 CMake 执行您选择的构建工具。

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:

运行 CMake 后,您最终会在构建目录中得到一个文件 PACKAGE.vcxproj。它正在构建这将创建安装程序。您可以从 Visual Studio 内部构建 PACKAGE 项目,也可以通过执行以下操作直接调用 msbuild:

msbuild /P:Configuration=Release PACKAGE.vcxproj

from your build directory in a VS command prompt.

从 VS 命令提示符中的构建目录。

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

这应该会生成名为 MyLib-1.0.0-win64.exe 的安装程序,也在您的构建目录中。


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


如果您只想使用 CMake,那么调用 msbuild 的替代方法是:

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


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


或者您可以先构建解决方案,然后调用 CPack 来创建安装程序:

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