简体   繁体   English

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

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

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. 我使用CMake构建所有内容,如果我可以使用CPack来制作安装程序,那将会很棒。 I already have CPack working on OSX, but I cannot get it to work on Windows. 我已经有CPack在OSX上工作,但我不能让它在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. 为了简化操作,我尝试在http://www.cmake.org/Wiki/CMake:Packaging_With_CPack上获取示例以使用NSIS安装程序软件。 I cannot find the NSIS installer anywhere after configuring (with VS 2010 Win64 generator). 配置完成后,我无法在任何地方找到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. 也许我很困惑,但我认为只需要源代码,CMake,CPack和NSIS就可以创建安装包而无需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. 完整教程的链接( 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)

... 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. ...我认为只需要源代码,CMake,CPack和NSIS就可以创建安装包,而无需Visual Studio。 Is this possible? 这可能吗?

Kind of. 的种类。 It depends on what you mean by "without any need for Visual Studio". 这取决于你的意思是“不需要Visual Studio”。 You need a build tool to actually create the lib and exe. 你需要一个构建工具来实际创建lib和exe。 On Windows, you need something like Visual Studio's msbuild, especially if you specified "Visual Studio 10 Win64" as the generator. 在Windows上,您需要像Visual Studio的msbuild,特别是如果您指定"Visual Studio 10 Win64"作为生成器。

If you mean "without running Visual Studio", then the answer is yes. 如果你的意思是“没有运行Visual Studio”,那么答案是肯定的。 You can have CMake execute your chosen build tool using the --build argument. 您可以让CMake使用--build参数执行您选择的构建工具。

After running CMake, you end up with a file PACKAGE.vcxproj in your build directory. 运行CMake后,您最终会在构建目录中找到一个文件PACKAGE.vcxproj。 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: 您可以从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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM