# Please Help CPMAddPackage install zlib in build directory

**URL:** <https://discourse.cmake.org/t/please-help-cpmaddpackage-install-zlib-in-build-directory/7286>\
**Category:** Code\
**Tags:** os:linux, os:windows, os:macos, comp:msvc\
**Created:** [January 20, 2023, 12:19am UTC](https://discourse.cmake.org/t/please-help-cpmaddpackage-install-zlib-in-build-directory/7286 "2023-01-20T00:19:02Z")\
**Posts on this page:** 2\
**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:** [January 20, 2023, 12:19am UTC](https://discourse.cmake.org/t/please-help-cpmaddpackage-install-zlib-in-build-directory/7286/1 "2023-01-20T00:19:02Z")

</div>

Please help,

I have been trying to figure out a way to download and compile **zlib** in a build directory via **CPMAddPackage** with **Cmake 3.25.1**. The reason is that I don’t want the user to need to have **zlib** previously installed on the system.

For example, say I have a **CPMAddPackage** in an **import\_libraries.cmake** defined as follows:

```auto
set(REPO_ZLIB "https://github.com/madler/zlib.git"
      CACHE STRING "ZLIB repo location.")
CPMAddPackage(
      NAME zlib
      GIT_REPOSITORY ${REPO_ZLIB}
      GIT_TAG "v1.2.13"
      OPTIONS
      "CMAKE_POSITION_INDEPENDENT_CODE True"
)

```

I would like **CMake** to use the above **zlib** rather than any locally defined **zlib** that may be on the current machine. _Also_ the **CMake** should be able to work with windows _or_ linux systems.

Can anyone point me in the direction as to how, or even if, this can be accomplished?

Thanks in advance for 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:** [January 21, 2023, 5:19pm UTC](https://discourse.cmake.org/t/please-help-cpmaddpackage-install-zlib-in-build-directory/7286/2 "2023-01-21T17:19:26Z")

</div>

Okay. I have been doing some thinking about it and would like to go _simpler_ if possible. Not using **CPMAddPackage** , just a plain **CMakeLists.txt** file(s).

How would I build **zlib** using source files defined within my particular project? Ignoring any currently existing **zlib** that may or may not be installed on the system, using one or more **CMakeList.txt** files?

For example in my project, say I have a directory called **deps** that exists whereby I have downloaded the **zlib** source _previously_. I would like to build the **zlib** source creating the corresponding libraries and then link theses _deps based_ **zlib** libraries and includes for my project using **CMake** , e.g. within the definitions of **CMakeLists.txt** file(s).

long story short, I would like my project to fully contain its own **zlib** definitions that do not relate back to any system **zlib** definitions.

If this is possible, can anyone point me in the right direction?
