14

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

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
AKJ
  • 950
  • 2
  • 13
  • 18

2 Answers2

22

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)
Richard
  • 56,349
  • 34
  • 180
  • 251
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
7

It seems this old post is getting a lot of traction. The solutions to linking zlib with CMake are either:

  1. To download zlib, if on Linux with

     sudo apt-get install zlib1g-dev
    

    and then following what Matthieu proprosed.

  2. Or download zlib like in 1 and do:

     add_executable(my_executable main.cpp)
     target_link_libraries(my_executable z)
    
  3. 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_DIR})
     target_link_libraries(my_executable zlib)
    
umläute
  • 28,885
  • 9
  • 68
  • 122
AKJ
  • 950
  • 2
  • 13
  • 18
  • `sudo apt-get install zlib1g-dev` should be highlighted, it's important. –  Nov 02 '19 at 01:32
  • In `3.`, where is `ZLIB_DEPS` defined? – Dotl May 02 '20 at 21:41
  • `ZLIB_DEPS` is not defined anywhere. It is being assigned. Like x = 2, where x is the `ZLIB_DEPS_DIR` and CMAKE_CURRENT_BINARY_DIR is 2. – AKJ May 07 '20 at 16:01
  • I 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) ? – WhozCraig Jan 20 '22 at 10:31
  • @WhozCraig you are right. I made a mistake. ZLIB_DEPS and ZLIB_DEPS_DIR should be the same variables. – AKJ Jan 21 '22 at 11:01