How to link Zlib with Cmake
Solution 1
You have to link against zlib.
If you used:
find_package(ZLIB)
Then you should have:
target_link_libraries(GzipTest ZLIB::ZLIB)
Also don't add the headers to your source files:
add_executable(GzipTest main.cpp)
Solution 2
It seems this old post is getting a lot of traction. The solutions to linking zlib with CMake are either:
-
To download zlib, if on Linux with
sudo apt-get install zlib1g-dev
and then following what Matthieu proprosed.
-
Or download zlib like in 1 and do:
add_executable(my_executable main.cpp) target_link_libraries(my_executable z)
-
Or just download zlib from their homepage: https://zlib.net/, then save it in a 'deps' folder. Modify the CMakeList in the zlib folder with
set(ZLIB_DEPS_DIR ${CMAKE_CURRENT_BINARY_DIR} PARENT_SCOPE)
and in the main CMakeList, do
add_executable(my_executable main.cpp) add_subdirectory(deps) include_directories(my_executable ${ZLIB_DEPS}) target_link_libraries(my_executable zlib)
Related videos on Youtube

AKJ
I am just a fan of Science and of technology but here on stackflow i am a fan of computers and webdesigning. I learnt (self-teaching) and mastered HTML, CSS and SQL and a bit of JavaScript (i skipped it in fact). I am now learning web scripting and focusing myself into it (javascript, Jquery and php).
Updated on June 24, 2022Comments
-
AKJ 11 months
I am trying to link my file with the zlib libray but still get: undefined reference to `deflateInit_'.
I am currently using CLion, have downloaded the zLib file from the homepage and added it into the project. This is how my CmakeLists.txt looks like
cmake_minimum_required(VERSION 3.10) project(GzipTest) set(CMAKE_CXX_STANDARD 11) include_directories(ZLIB zlib-1.2.11) add_executable(GzipTest main.cpp zlib-1.2.11/zlib.h)
And the code (Copying from the zpipe.c):
include "iostream" include "zlib.h" include "iostream" define CHUNK 1639 FILE *fp; int def(FILE *source, FILE *dest, int level){ int ret, flush; unsigned have; z_stream strm; unsigned char in[CHUNK]; unsigned char out[CHUNK]; // Allocate Deflate state strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; ret = deflateInit(&strm, level); if (ret != Z_OK){ return ret; } } int main(){ fp = fopen("inputFile.txt", "r"); if (fp == nullptr){ perror("Could not open data"); exit(EXIT_FAILURE); } def(fp, fp, 1); }
What could be missing? Thanks in advance
-
Felix Xu over 3 years
sudo apt-get install zlib1g-dev
should be highlighted, it's important. -
Dotl about 3 yearsIn
3.
, where isZLIB_DEPS
defined? -
AKJ about 3 years
ZLIB_DEPS
is not defined anywhere. It is being assigned. Like x = 2, where x is theZLIB_DEPS_DIR
and CMAKE_CURRENT_BINARY_DIR is 2. -
WhozCraig over 1 yearI understand, but that wasn't the question. The actual ZLIB_DEPS variable in the above is never set, and assumed to be an artifact of the subdirectory addition? The question wasn't about ZLIB_DEPS_DIR ; it was about ZLIB_DEPS. Or is the latter just wrong and it was supposed to be ZLIB_DEPS_DIR in all occurrences in (3) ?
-
AKJ over 1 year@WhozCraig you are right. I made a mistake. ZLIB_DEPS and ZLIB_DEPS_DIR should be the same variables.