# CPMAddPackage lib

**URL:** https://discourse.cmake.org/t/cpmaddpackage-lib/7128
**Category:** Code
**Tags:** os:linux, os:windows, os:macos, comp:msvc
**Created:** [December 24, 2022, 4:52pm UTC](https://discourse.cmake.org/t/cpmaddpackage-lib/7128 "2022-12-24T16:52:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![john](https://discourse.cmake.org/user_avatar/discourse.cmake.org/john/32/4097_2.png) [@john](https://discourse.cmake.org/u/john)
#### Post date: [December 24, 2022, 4:52pm UTC](https://discourse.cmake.org/t/cpmaddpackage-lib/7128/1 "2022-12-24T16:52:12Z")

</div>

Hello all,

I am fairly new to CMake but am interested in using the CMake Package Management (CPM) to add a local library/source files. My question is probably simple, so please forgive.

I would like to know how to download and install shared library for **zlib** via a call(s) to **CPMAddPackage** call, then _link_ against this downloaded package for some local C++ code. Can anyone point me to an example of this or provide some simple code that illustrates this?

Bonus question, if this is okay, I would was thinking of using **Boost.Iostreams** for compression/decompression via **CPMAddPackage** call later on, but have read that there can be issues with **ZLIB** and **Boost.Iostreams** in Windows environment. I need my code to run on both Windows _and_ Linux environments. Does anyone have any information on this situation?

Thanks to any help.

---

<div class="post-metadata">

### Author: ![john](https://discourse.cmake.org/user_avatar/discourse.cmake.org/john/32/4097_2.png) [@john](https://discourse.cmake.org/u/john)
#### Post date: [December 26, 2022, 2:36pm UTC](https://discourse.cmake.org/t/cpmaddpackage-lib/7128/2 "2022-12-26T14:36:04Z")

</div>

I think I might have solved the issue. I am posting it here for anyone else who may run into the same problem.

In a Cmake script called **third\_party.cmake** in **cmake/** directory.

```auto
# CPM script
include(cmake/CPM.cmake)

# Storage location
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/third_party)

# Add ZLIB
set(ZLIB_REPO "https://github.com/madler/zlib.git"
     CACHE STRING "ZLIB repository location."
)
CPMAddPackage(
    NAME zlib 
    GIT_REPOSITORY ${ZLIB_REPO}
    GIT_TAG "v1.2.13"
    OPTIONS
    "CMAKE_POSITION_INDEPENDENT_CODE True"
)
if(zlib_ADDED)
    target_include_directories(zlib 
                               PUBLIC $<BUILD_INTERFACE:${zlib_BINARY_DIR}>
                               PUBLIC $<INSTALL_INTERFACE:include>)
    target_include_directories(zlibstatic 
                               PUBLIC $<BUILD_INTERFACE:${zlib_BINARY_DIR}>
                               PUBLIC $<INSTALL_INTERFACE:include>)
               
    message(STATUS "Cmake added local zlib: ${zlib_SOURCE_DIR}")

```

---

<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: [December 28, 2022, 12:39pm UTC](https://discourse.cmake.org/t/cpmaddpackage-lib/7128/3 "2022-12-28T12:39:42Z")

</div>

Reposted here: [Sorry for repost - CPMAddPackage with ZLIB](https://discourse.cmake.org/t/sorry-for-repost-cpmaddpackage-with-zlib/7129)
