# Generate individual package for each component

**URL:** https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686
**Category:** Usage
**Created:** [December 16, 2021, 12:15pm UTC](https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686 "2021-12-16T12:15:16Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hachanuy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hachanuy/32/902_2.png) [@hachanuy](https://discourse.cmake.org/u/hachanuy)
#### Post date: [December 16, 2021, 12:15pm UTC](https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686/1 "2021-12-16T12:15:16Z")

</div>

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:

```cmake
# 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
  )

```

```cpp
//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.

```auto
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.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [December 16, 2021, 12:57pm UTC](https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686/2 "2021-12-16T12:57:48Z")

</div>

You have to tell CPack to do so.

```auto
# 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](https://cmake.org/cmake/help/latest/module/CPackComponent.html#module:CPackComponent)

---

<div class="post-metadata">

### Author: ![hachanuy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/hachanuy/32/902_2.png) [@hachanuy](https://discourse.cmake.org/u/hachanuy)
#### Post date: [December 16, 2021, 1:00pm UTC](https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686/3 "2021-12-16T13:00:05Z")

</div>

Thank you very much

---

<div class="post-metadata">

### Author: ![rygood](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/ecc23a/32.png) [@rygood](https://discourse.cmake.org/u/rygood)
#### Post date: [September 23, 2025, 2:20am UTC](https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686/4 "2025-09-23T02:20:18Z")

</div>

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.

```auto
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:

```auto
$ 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

```

---

<div class="post-metadata">

### Author: ![leaveye](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leaveye/32/1742_2.png) [@leaveye](https://discourse.cmake.org/u/leaveye)
#### Post date: [October 28, 2025, 2:49pm UTC](https://discourse.cmake.org/t/generate-individual-package-for-each-component/4686/5 "2025-10-28T14:49:04Z")

</div>

I am finding a solution to build a library on busybox with OPKG package format, which documented supported by DEB CPack generator. This made me to give `CPACK_DEBIAN_FILE_NAME` in cmake script.

To make both `devel` and `runtime` package from the same source repo, I have to make the `CPACK_DEBIAN_FILE_NAME` contains the component part.

Your solution gives me a method that done it:

```auto
install(FILES foo.h COMPONENT dev)
install(FILES libfoo.so COMPONENT runtime)

set(CPACK_PACKAGE_NAME libfoo)
set(CPACK_PACKAGE_VERSION "0.1.2")
set(CPACK_PACKAGE_CONTACT "Name <name@example.com>")

set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_COMPONENT_GROUPING ONE_PER_GROUP)

set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "all")

# Do NOT set the CPACK_DEBIAN_FILE_NAME
#set(CPACK_DEBIAN_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.ipk")
# use CPACK_DEBIAN_<COMPONENT>_FILE_NAME instead
# and <COMPONENT> part *must* be UPPERCASE
set(CPACK_DEBIAN_DEVEL_FILE_NAME "${CPACK_PACKAGE_NAME}-devel-${CPACK_PACKAGE_VERSION}-${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.ipk") set(CPACK_DEBIAN_RUNTIME_FILE_NAME "${CPACK_PACKAGE_NAME}-runtime-${CPACK_PACKAGE_VERSION}-${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}.ipk")

set(CPACK_DEBIAN_PACKAGE_DEBUG TRUE)
include(CPack)

# group name *can* be lowercase
cpack_add_component(dev GROUP devel)
cpack_add_component(runtime GROUP runtime)

```

Thanks again.
