# Can I make two separate library packages in one CMakelIsts.txt file?

**URL:** https://discourse.cmake.org/t/can-i-make-two-separate-library-packages-in-one-cmakelists-txt-file/15168
**Category:** Code
**Tags:** os:linux
**Created:** [September 15, 2025, 7:07pm UTC](https://discourse.cmake.org/t/can-i-make-two-separate-library-packages-in-one-cmakelists-txt-file/15168 "2025-09-15T19:07:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![chriskot870](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/53a042/32.png) [@chriskot870](https://discourse.cmake.org/u/chriskot870)
#### Post date: [September 15, 2025, 7:07pm UTC](https://discourse.cmake.org/t/can-i-make-two-separate-library-packages-in-one-cmakelists-txt-file/15168/1 "2025-09-15T19:07:03Z")

</div>

I am currently creating one library built from two source files. I want to generate both shared library and static library. The static libraries and headers only get loaded when loading a dev version of the package.

```auto
set(QW_WEATHER_DEVICES_LIB weatherdevices)

add_library(${QW_WEATHER_DEVICES_LIB} SHARED
  anemometer_adafruit.cpp
  ecowitt_ln90lp.cpp
)

# Also make a static library for dev package
add_library(${QW_WEATHER_DEVICES_LIB}_static STATIC
  anemometer_adafruit.cpp
  ecowitt_ln90lp.cpp
)

install( FILES
    include/anonemometer_adafruit.h
    include/ecowitt_ln90lp.h
  DESTINATION include/qw/devices
  COMPONENT dev
)
# Install static library for dev package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}_static
  DESTINATION lib/qw/devices/i2c/lib/lib${QW_WEATHER_DEVICES_LIB}.a
  COMPONENT dev
)
# Install shared library for runtime package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}
  DESTINATION lib/qw/devices/lib
  COMPONENT runtime
  )

```

But after thinking about it these two source files are really mutually exclusive of each other. Essentially, if you wanted one you would not want the other. So, I would like to separate these into two separate libraries with separate install and packaging rules for each.

Can I do that within this same CMakeLists.txt or do I need to move each one into it’s own sub-directory?

I think I can make the separate libraries

```auto
set(QW_WEATHER_DEVICES_LIB weatherdevices)

add_library(${QW_WEATHER_DEVICES_LIB} SHARED
  anemometer_adafruit.cpp
)

# Also make a static library for dev package
add_library(${QW_WEATHER_DEVICES_LIB}_static STATIC
  anemometer_adafruit.cpp
)

set(QW_ECOWITT_LN90LP_LIB ecowittlp90lndevice)

add_library(${QW_ECOWITT_LN90LP_LIB} SHARED
  ecowitt_ln90lp.cpp
)

# Also make a static library for dev package
add_library(${QW_ECOWITT_LN90LP_LIB}_static STATIC
  ecowitt_ln90lp.cpp
)

```

But I am not sure how to get these libraries into separate runtime and dev packages.

Any pointers appreciated.

Thanks

Chris

---

<div class="post-metadata">

### Author: ![chriskot870](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/53a042/32.png) [@chriskot870](https://discourse.cmake.org/u/chriskot870)
#### Post date: [September 15, 2025, 9:07pm UTC](https://discourse.cmake.org/t/can-i-make-two-separate-library-packages-in-one-cmakelists-txt-file/15168/2 "2025-09-15T21:07:50Z")

</div>

OK I think I have a solution. I used the COMPONENTS to have two separate packages made. I separated out and created two libraries each with shard and static:

```auto
set(QW_WEATHER_DEVICES_LIB weatherdevices)

add_library(${QW_WEATHER_DEVICES_LIB} SHARED
  anemometer_adafruit.cpp
)

# Also make a static library for dev package
add_library(${QW_WEATHER_DEVICES_LIB}_static STATIC
  anemometer_adafruit.cpp
)

set(QW_ECOWITT_LN90LP_DEVICE_LIB ecowittln90lp)

add_library(${QW_ECOWITT_LN90LP_DEVICE_LIB} SHARED
  ecowitt_ln90lp.cpp
)

# Also make a static library for dev package
add_library(${QW_ECOWITT_LN90LP_DEVICE_LIB}_static STATIC
  ecowitt_ln90lp.cpp
)

Then made different install rules with different components:

install( FILES
    include/anemometer_adafruit.h
  DESTINATION include/qw/devices
  COMPONENT weatherdevices_dev
)
# Install static library for dev package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}_static
  DESTINATION lib/qw/devices/lib${QW_WEATHER_DEVICES_LIB}.a
  COMPONENT weatherdevices_dev
)
# Install shared library for runtime package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}
  DESTINATION lib/qw/devices/lib
  COMPONENT weatherdevices_runtime
  )

install( FILES
    include/ecowitt_ln90lp.h
  DESTINATION include/qw/devices
  COMPONENT ecowittln90lp_dev
)
# Install static library for dev package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}_static
  DESTINATION lib/qw/devices/lib${QW_WEATHER_DEVICES_LIB}.a
  COMPONENT ecowittln90lp_dev
)
# Install shared library for runtime package
install(
  TARGETS ${QW_WEATHER_DEVICES_LIB}
  DESTINATION lib/qw/devices/lib
  COMPONENT ecowittln90lp_runtime
  )

```

I then added these components to the ALL COMPONENTS value in my cpack\_debian.cmake file:

```auto
set(CPACK_COMPONENTS_ALL
  runtime
  weatherdevices_runtime
  ecowittln90lp_runtime
  dev
  weatherdevices_dev
  ecowittln90lp_dev
)

```

I then got a different deb package for each one.

```auto
[cpack] CPack: Create package
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/libqwunits-dev_0.1.1-2_arm64.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-ecowittln90lp_dev.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-ecowittln90lp_runtime.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/libqwunits_0.1.1-2_arm64.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-weatherdevices_dev.deb generated.
[cpack] CPack: - package: /home/chrisk/Projects/src/qwunits/build/arm64-debug/qwlibs-0.1.1-Linux-weatherdevices_runtime.deb generated.

```

It seemed to have the expected files in them.

I’ll have to think if I like that better than giving each one their own directory and leave the COMPONENT for each as runtime and dev like all my other libraries.

Also, one thing I noticed is in order to add both a static and a shared library. In order for them to exist together I had to use a different name. so I appended “\_static” to the static library definition. I ended up with a \_static.a and a .a file. I would like to figure out how to only end up with a \<library.a file. But, that is a different question.

Thanks

Chris

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 20, 2026, 2:26pm UTC](https://discourse.cmake.org/t/can-i-make-two-separate-library-packages-in-one-cmakelists-txt-file/15168/3 "2026-02-20T14:26:04Z")

</div>

> [@chriskot870](#):
>
> I would like to figure out how to only end up with a \<library.a file. But, that is a different question.

Set the [`OUTPUT_NAME`](https://cmake.org/cmake/help/latest/prop_tgt/OUTPUT_NAME.html) target property.
