Introduction to CMake Build System – Complete Guide to CPack Packaging: Cross-Platform Installer Creation

Introduction

Have you ever encountered the following issues:

  • • Not knowing how to distribute the compiled program to users
  • • Manually copying files to system directories, which is prone to errors
  • • Different platforms require different packaging methods, leading to high maintenance costs
  • • Users are unsure how to uninstall after installation
  • • Lack of professional installation packages affects project image

These problems can be solved using CMake’s installation and packaging features. CMake provides a complete installation configuration and CPack packaging tool, making it easy to create professional installation packages.

Why Installation and Packaging are Necessary

Pain Points of Manual Deployment

File Dispersal

# Manual deployment requires copying multiple files
cp myapp /usr/local/bin/
cp libmylib.so /usr/local/lib/
cp mylib.h /usr/local/include/
cp config.json /etc/myapp/
# Easy to miss, inconsistent paths

Platform Differences

  • • Windows requires NSIS or WiX installation packages
  • • Linux requires deb/rpm packages
  • • macOS requires dmg/pkg packages
  • • Manual creation is time-consuming and labor-intensive

Uninstallation Difficulties

  • • Users do not know which files were installed
  • • Manual deletion is prone to omissions
  • • Cannot completely clean up

The Value of CMake Installation and Packaging

Automated Installation

# One command completes the installation
cmake --install build

# Automatically handles all files
# - Executables to bin directory
# - Library files to lib directory
# - Header files to include directory
# - Configuration files to etc directory

Cross-Platform Packaging

# Automatically generate installation packages for corresponding platforms
cpack -G NSIS      # Windows
cpack -G DEB       # Debian/Ubuntu
cpack -G RPM       # RedHat/CentOS
cpack -G DragNDrop # macOS

Core Advantages

  • • ✅ Automation:One command completes installation and packaging
  • • ✅ Cross-Platform:Unified configuration, automatically adapts to different platforms
  • • ✅ Professional:Generates standard installation package formats
  • • ✅ Maintainable:Configuration is centrally managed, easy to update

Basics of the install Command (Prerequisite Knowledge for CPack)

CPack needs to package based on the installation rules defined by the install command, so let’s first understand the install command briefly.

Basic Usage

Installing Executables and Libraries

include(GNUInstallDirs)

# Install executable files to bin directory
install(TARGETS myapp
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# Install library files
install(TARGETS mylib
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}      # Static library
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}      # Dynamic library
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}      # Windows DLL
)

# Install multiple targets simultaneously
install(TARGETS myapp mylib
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

Installing Files

# Install header files
install(FILES include/mylib.h
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/myproject
)

# Install directory
install(DIRECTORY include/
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/myproject
    FILES_MATCHING PATTERN "*.h"
)

# Install configuration files
install(FILES config/config.json
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/myproject
)

Standard Path Variables(Recommended to use):

  • <span>CMAKE_INSTALL_BINDIR</span>:bin directory
  • <span>CMAKE_INSTALL_LIBDIR</span>:lib directory
  • <span>CMAKE_INSTALL_INCLUDEDIR</span>:include directory
  • <span>CMAKE_INSTALL_DATADIR</span>:share directory
  • <span>CMAKE_INSTALL_SYSCONFDIR</span>:etc directory

Complete Example

cmake_minimum_required(VERSION 3.15)
project(MyProject VERSION 1.0.0)

add_executable(myapp src/main.cpp)
add_library(mylib STATIC src/mylib.cpp)

include(GNUInstallDirs)

install(TARGETS myapp mylib
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

install(FILES include/mylib.h
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/myproject
)

Detailed Explanation of CPack Packaging Tool

Introduction to CPack

CPack is a built-in packaging tool in CMake that can automatically generate installation packages for various platforms without the need to manually write packaging scripts.

Core Features

  • • 🎯 Cross-Platform:Supports Windows, Linux, macOS
  • • 📦 Multiple Formats:NSIS, DEB, RPM, DMG, ZIP, etc.
  • • 🔧 Easy Configuration:Based on CMake configuration, integrated with the build process
  • • 📊 Automation:One command generates the installation package
  • • 🆓 Completely Free:Built into CMake, no additional tools required (some generators may require them)

Enabling CPack

Basic Configuration

# Add at the end of CMakeLists.txt
include(CPack)

Basic Settings

set(CPACK_PACKAGE_NAME "MyProject")
set(CPACK_PACKAGE_VENDOR "MyCompany")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "My Project Description")
set(CPACK_PACKAGE_DESCRIPTION "Detailed description of the project")
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_GENERATOR "ZIP")  # Default generator

# Set the installation package filename format (optional)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CMAKE_SYSTEM_NAME}")

include(CPack)

Important Notes

  • <span>include(CPack)</span> must be after all CPack configurations
  • • Version information is recommended to use<span>PROJECT_VERSION</span> variable for consistency
  • • Package names are recommended to be lowercase, in accordance with Linux package management standards

Multi-Generator Configuration

Support for Multiple Platforms Simultaneously

# Detect platform and set generator
if(WIN32)
    set(CPACK_GENERATOR "NSIS;ZIP")
elseif(APPLE)
    set(CPACK_GENERATOR "DragNDrop;TGZ")
elseif(UNIX)
    set(CPACK_GENERATOR "DEB;RPM;TGZ")
endif()

include(CPack)

Generate All Formats

cpack -G "NSIS;ZIP"  # Windows
cpack -G "DEB;RPM;TGZ"  # Linux

Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer Creation

?

Common Questions

Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer CreationIntroduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer Creation

What to do if CPack cannot find the generator?

Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer Creation

  • NSIS:NSIS needs to be installed on Windows (https://nsis.sourceforge.io/)
  • DEB:dpkg-dev needs to be installed on Linux:<span>sudo apt-get install dpkg-dev</span>
  • RPM:rpm-build needs to be installed on Linux:<span>sudo yum install rpm-build</span>

Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer Creation

How to generate different installation packages for different platforms?

Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer Creation

if(WIN32)
    set(CPACK_GENERATOR "NSIS;ZIP")
elseif(APPLE)
    set(CPACK_GENERATOR "DragNDrop")
elseif(UNIX)
    set(CPACK_GENERATOR "DEB;RPM")
endif()

Summary and Outlook

Core Points Review

The Core Value of CPack

  • • ✅ Automated Packaging:One command generates the installation package
  • • ✅ Cross-Platform Support:Unified configuration for Windows/Linux/macOS
  • • ✅ Professional Format:Generates standard installation package formats
  • • ✅ Easy Integration:Seamlessly integrates with CMake build process

Usage Recommendations

  • Basic Projects:Use install command + basic CPack configuration
  • Complex Projects:Add dependency declarations, icons, licenses, etc.
  • CI/CD:Integrate into automation processes to automatically generate installation packages

Best Practices

  • • Use standard installation paths (GNUInstallDirs)
  • • Clearly declare runtime dependencies
  • • Provide clear package description information
  • • Integrate into CI/CD processes for automation

Recommended Resources

Official Documentation

  • CMake install command
  • CPack documentation
  • GNUInstallDirs

Community Resources

  • CPack generator list
  • CMake packaging examples

Tags:#CMake #Installation Configuration #CPack #Packaging Tool #Cross-Platform Deployment #C++ Engineering

I hope this article helps you create professional installation packages and enhance the distribution experience of your project! If you have any questions, feel free to discuss in the comments.

Appendix: Quick Reference

Common Patterns of the install Command

# Install executable files
install(TARGETS myapp RUNTIME DESTINATION bin)

# Install library files
install(TARGETS mylib 
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
)

# Install header files
install(FILES mylib.h DESTINATION include)

# Install directory
install(DIRECTORY include/ DESTINATION include)

Common CPack Configurations

set(CPACK_PACKAGE_NAME "MyProject")
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_GENERATOR "NSIS;DEB;RPM")
include(CPack)

Standard Installation Paths

include(GNUInstallDirs)
# ${CMAKE_INSTALL_BINDIR}      # bin
# ${CMAKE_INSTALL_LIBDIR}      # lib
# ${CMAKE_INSTALL_INCLUDEDIR}  # include
# ${CMAKE_INSTALL_DATADIR}     # share

If you like it, please follow us!Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer CreationGive us a thumbs up!Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer CreationClick on “See First” to see the best content!Introduction to CMake Build System - Complete Guide to CPack Packaging: Cross-Platform Installer Creation

Leave a Comment