CPack supports a wide range of packaging formats for different platforms. Here is a list of the most common ones:
- Usage: Unix-based systems (Linux, macOS).
- Command:
cpack -G TGZ
- Output: Compressed tarball (e.g.,
myapp-1.0.0-Linux.tar.gz
).
- Usage: Unix-based systems.
- Command:
cpack -G TBZ2
- Output: Bzip2 compressed tarball (e.g.,
myapp-1.0.0-Linux.tar.bz2
).
- Usage: Cross-platform.
- Command:
cpack -G ZIP
- Output: ZIP archive (e.g.,
myapp-1.0.0-Windows.zip
).
- Usage: Debian-based Linux distributions (e.g., Ubuntu).
- Command:
cpack -G DEB
- Output:
.deb
package (e.g.,myapp-1.0.0-Linux.deb
). - Requires: The
dpkg-deb
tool to be installed on the system.
- Usage: Red Hat-based Linux distributions (e.g., Fedora, CentOS).
- Command:
cpack -G RPM
- Output:
.rpm
package (e.g.,myapp-1.0.0-Linux.rpm
). - Requires: The
rpmbuild
tool to be installed.
- Usage: Windows installer (alternative to MSI).
- Command:
cpack -G NSIS
- Output: NSIS executable installer (e.g.,
myapp-1.0.0-Windows.exe
). - Requires: NSIS (Nullsoft Scriptable Install System) to be installed.
- Usage: Windows MSI installers.
- Command:
cpack -G WIX
- Output:
.msi
package (e.g.,myapp-1.0.0-Windows.msi
). - Requires: WiX Toolset to be installed.
- Usage: macOS.
- Command:
cpack -G DragNDrop
- Output:
.dmg
disk image (e.g.,myapp-1.0.0-macOS.dmg
). - Requires: macOS system to build.
- Usage: Unix-based systems (self-extracting).
- Command:
cpack -G STGZ
- Output: Self-extracting archive (e.g.,
myapp-1.0.0-Linux.sh
).
- Usage: Linux/Unix installer script.
- Command:
cpack -G SH
- Output: Shell script installer (e.g.,
myapp-1.0.0-Linux.sh
).
- Usage: Cross-platform.
- Command:
cpack -G 7Z
- Output:
.7z
archive (e.g.,myapp-1.0.0-Windows.7z
). - Requires: 7-Zip installed for extraction.
- Usage: Unix-based systems.
- Command:
cpack -G TXZ
- Output: XZ compressed tarball (e.g.,
myapp-1.0.0-Linux.tar.xz
).
- CPack can also work with custom generators if you need a different format not natively supported. You can use the
CPACK_GENERATOR
variable to specify multiple generators if you need multiple formats from a single configuration.
These options allow CPack to create a wide range of installers and package formats, providing flexibility in packaging software for different platforms and distribution channels.
To create an MSI installer with CMake and CPack, follow these steps. CPack is a packaging system that comes with CMake, and it supports various package formats, including MSI for Windows.
Make sure you have a working CMake project. Here's a simple example:
cmake_minimum_required(VERSION 3.10)
project(MyApp)
add_executable(MyApp main.cpp)
This will generate an executable called MyApp
.
Add the necessary configuration to your CMakeLists.txt
to tell CPack to generate an MSI installer:
# Include CPack module
include(CPack)
# Set CPack variables
set(CPACK_GENERATOR "WIX") # Specify the generator as WIX for MSI
set(CPACK_PACKAGE_NAME "MyApp")
set(CPACK_PACKAGE_VERSION "1.0.0")
set(CPACK_PACKAGE_VENDOR "MyCompany")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MyApp Installer")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "MyApp") # Default installation directory
set(CPACK_WIX_UPGRADE_GUID "C011FA2D-5F0D-43F7-BE94-5D6D77403435") # Replace with your own GUID
# Include Wix toolset variables if needed
set(CPACK_WIX_PRODUCT_GUID "YOUR-GUID-HERE") # Optional, for product identification
# Other useful CPack options
set(CPACK_WIX_LICENSE_RTF "${CMAKE_CURRENT_SOURCE_DIR}/license.rtf") # License file (optional)
set(CPACK_WIX_UI_BANNER "${CMAKE_CURRENT_SOURCE_DIR}/images/banner.bmp") # Custom banner (optional)
set(CPACK_WIX_UI_DIALOG "${CMAKE_CURRENT_SOURCE_DIR}/images/dialog.bmp") # Custom dialog image (optional)
Tell CMake which files to install. This is important so that CPack knows what to include in the installer. You can specify the executable, libraries, and other resources like this:
install(TARGETS MyApp DESTINATION bin)
install(FILES "${PROJECT_SOURCE_DIR}/README.md" DESTINATION .)
Once you’ve set up your project, you can use CPack to generate the MSI package. To do this, follow these steps:
-
Configure your project with CMake:
cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release .
-
Build your project:
cmake --build . --config Release
-
Run CPack to create the MSI installer:
cpack -G WIX
This will generate an MSI package for your project in the build directory.
- WIX Toolset: CPack uses the WiX (Windows Installer XML) toolset to generate MSI files. Ensure that you have WiX installed and in your system PATH for CPack to create MSI installers. You can download WiX from WiX Toolset.
- GUIDs: Windows Installer uses GUIDs to uniquely identify products and upgrades. You need to provide a unique
CPACK_WIX_PRODUCT_GUID
andCPACK_WIX_UPGRADE_GUID
for your project. You can generate GUIDs using tools likeuuidgen
.
Once you run CPack successfully, you’ll find an MSI installer for your application in the build directory.