hachanuy
(Uy Ha)
December 16, 2021, 12:15pm
1
I want to build multiple packages from a single project, and I’m trying to use cpack component to do it.
I have a toy project set up as follows:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(Sandbox VERSION 0.2)
add_executable(main main.cpp)
add_executable(main2 main.cpp)
install(TARGETS main
COMPONENT FirstComp)
install(TARGETS main2
COMPONENT SecondComp)
set(CPACK_PACKAGE_CONTACT "Name Surname <name@domain.com>")
set(CPACK_GENERATOR DEB)
include(CPack)
cpack_add_component(FirstComp
DISPLAY_NAME first_comp
)
cpack_add_component(SecondComp
DISPLAY_NAME SecondComp
)
//main.cpp
#include <iostream>
int main(){
std::cout << "Hello, World\n";
}
Normally, if I don’t use component, i can just run the following command to generate the .deb package.
cmake -B build -S .
cmake --build build --target package
But I’m not sure what I should do to generate a seperate .deb package for my FristComp
and SecondComp
components.
erk
(Eric Noulard)
December 16, 2021, 12:57pm
2
You have to tell CPack to do so.
# tell CPack generator to use component packaging
set(CPACK_DEB_COMPONENT_INSTALL 1)
# tell CPack which kind of grouping you want
set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)
see:
https://cmake.org/cmake/help/latest/module/CPackComponent.html#module:CPackComponent
1 Like
rygood
September 23, 2025, 2:20am
4
Thank you so much for this! Building upon the toy example and the provided solution I think I figured out my similar issue of wanting to group multiple targets into separate debs and correct the file names. Posting for anyone else who ends up here since I could not find any other examples.
cmake_minimum_required(VERSION 3.21)
project(Sandbox VERSION 1.2.3)
add_executable(main main.cpp)
install(TARGETS main
COMPONENT FirstComp)
add_executable(main2 main.cpp)
install(TARGETS main2
COMPONENT SecondComp)
add_executable(main3 main.cpp)
install(TARGETS main3
COMPONENT ThirdComp)
set(CPACK_PACKAGE_CONTACT "Name Surname <name@domain.com>")
set(CPACK_GENERATOR DEB)
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "arm64")
set(CPACK_DEBIAN_GROUP1_FILE_NAME "Sandbox-group1_${PROJECT_VERSION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.deb")
set(CPACK_DEBIAN_GROUP1_PACKAGE_DEPENDS "libc6")
set(CPACK_DEBIAN_GROUP2_FILE_NAME "Sandbox-group2_${PROJECT_VERSION}_${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.deb")
set(CPACK_DEBIAN_GROUP2_PACKAGE_DEPENDS "libc6")
include(CPack)
cpack_add_component(FirstComp GROUP group1)
cpack_add_component(SecondComp GROUP group2)
cpack_add_component(ThirdComp GROUP group2)
This will result in:
$ dpkg -c ./Sandbox-group1_1.2.3_arm64.deb
drwxr-xr-x root/root 0 2025-09-23 02:15 ./usr/
drwxr-xr-x root/root 0 2025-09-23 02:15 ./usr/bin/
-rwxr-xr-x root/root 9752 2025-09-23 01:29 ./usr/bin/main
$ dpkg -c ./Sandbox-group2_1.2.3_arm64.deb
drwxr-xr-x root/root 0 2025-09-23 02:15 ./usr/
drwxr-xr-x root/root 0 2025-09-23 02:15 ./usr/bin/
-rwxr-xr-x root/root 9752 2025-09-23 01:29 ./usr/bin/main2
-rwxr-xr-x root/root 9752 2025-09-23 01:29 ./usr/bin/main3