1

I have installed zlib-1.2.3.exe.

I have the following settings in CLion 2020.1:

enter image description here

I have the following lines in my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.16)
project(ZLIB__test)

set(CMAKE_CXX_STANDARD 11)

set(ZLIB_LIBRARY "C:/Program Files (x86)/GnuWin32")
set(ZLIB_INCLUDE_DIR "C:/Program Files (x86)/GnuWin32/include")    
include_directories(${ZLIB_INCLUDE_DIR})    

add_executable(ZLIB__test main.cpp)

I have the following source code in main.cpp:

#include <cstdio>
#include <zlib.h>

// Demonstration of zlib utility functions
unsigned long file_size(char *filename) {
    FILE *pFile = fopen(filename, "rb");
    fseek(pFile, 0, SEEK_END);
    unsigned long size = ftell(pFile);
    fclose(pFile);
    return size;
}

int decompress_one_file(char *infilename, char *outfilename) {
    gzFile infile = gzopen(infilename, "rb");
    FILE *outfile = fopen(outfilename, "wb");
    if (!infile || !outfile) return -1;

    char buffer[128];
    int num_read = 0;
    while ((num_read = gzread(infile, buffer, sizeof(buffer))) > 0) {
        fwrite(buffer, 1, num_read, outfile);
    }

    gzclose(infile);
    fclose(outfile);
}

int compress_one_file(char *infilename, char *outfilename) {
    FILE *infile = fopen(infilename, "rb");
    gzFile outfile = gzopen(outfilename, "wb");
    if (!infile || !outfile) return -1;

    char inbuffer[128];
    int num_read = 0;
    unsigned long total_read = 0, total_wrote = 0;
    while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
        total_read += num_read;
        gzwrite(outfile, inbuffer, num_read);
    }
    fclose(infile);
    gzclose(outfile);

    printf("Read %ld bytes, Wrote %ld bytes,Compression factor %4.2f%%\n",
           total_read, file_size(outfilename),
           (1.0 - file_size(outfilename) * 1.0 / total_read) * 100.0);
}


int main(int argc, char **argv) {
    compress_one_file(argv[1], argv[2]);
    decompress_one_file(argv[2], argv[3]);
}

and, the above source code is generating the following error:

====================[ Clean | Debug ]===========================================
"C:\Program Files\JetBrains\CLion 2020.1.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\pc\CLionProjects\ZLIB__test\cmake-build-debug --target clean

Clean finished

====================[ Build | all | Debug ]=====================================
"C:\Program Files\JetBrains\CLion 2020.1.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\pc\CLionProjects\ZLIB__test\cmake-build-debug --target all
Scanning dependencies of target ZLIB__test
[ 50%] Building CXX object CMakeFiles/ZLIB__test.dir/main.cpp.obj
In file included from C:\Users\pc\CLionProjects\ZLIB__test\main.cpp:2:
In file included from C:\PROGRA~2\GnuWin32\include\zlib.h:34:
C:\PROGRA~2\GnuWin32\include/zconf.h(289,12): fatal error: 'unistd.h' file not found
#  include <unistd.h>    /* for SEEK_* and off_t */
           ^~~~~~~~~~
1 error generated.
NMAKE : fatal error U1077: 'C:\PROGRA~1\LLVM\bin\clang-cl.exe' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

How can I fix this?

user366312
  • 16,949
  • 65
  • 235
  • 452

1 Answers1

2

A big rule of thumb in CMake is that if you are using a library with native CMake support, then you should be using either find_package() + target_link_libraries() or add_subdirectory() + target_link_libraries(). Manually adding header search paths and linking against libraries for these kinds of dependencies is almost always wrong.

Doing things the CMake-way will normally lead to things getting configured correctly for a reasonably-made library (which zlib certainly is).

Option A: find_package() + target_link_libraries()

This requires zlib to be installed in a findable place on your system.

cmake_minimum_required(VERSION 3.16)
project(ZLIB__test)

set(CMAKE_CXX_STANDARD 11)

find_package(zlib REQUIRED)

add_executable(ZLIB__test main.cpp)
target_link_libraries(ZLIB__test ZLIB::ZLIB)

Option B: add_subdirectory() + target_link_libraries()

This one assumes that a copy of zlib's source code is located at third_party/zlib relative to the CMakeLists.txt.

cmake_minimum_required(VERSION 3.16)
project(ZLIB__test)

set(CMAKE_CXX_STANDARD 11)

add_subdirectory("third_party/zlib")

add_executable(ZLIB__test main.cpp)
target_link_libraries(ZLIB__test ZLIB::ZLIB)
  • I used the first option and the following error is generated: `CMake Error at CMakeLists.txt:8 (add_executable): Target "ZLIB__test" links to target "ZLIB::ZLIB" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?` – user366312 May 26 '21 at 19:27
  • I've ammended the answer to add `REQUIRED` to the find_package, this way you'll get a clearer error about it not being bale to find zlib. –  May 26 '21 at 19:34
  • Besides, why are you skipping `set(ZLIB_LIBRARY "C:/Program Files (x86)/GnuWin32")` and `set(ZLIB_INCLUDE_DIR "C:/Program Files (x86)/GnuWin32/include")`? – user366312 May 26 '21 at 19:38
  • Because it's work that's `find_package()` does, but in a much cleaner and more portable manner. Where zlib is is a property of your system, not of the project. So it should be done when invoking cmake, with `cmake -DZLIB_ROOT=...`, but if zlib is actually **installed** on your system, it will be found. –  May 26 '21 at 19:40
  • My source code doesn't compile without those two lines. – user366312 May 26 '21 at 19:41
  • Well, your code doesn't compile *with* them either, so I'm not sure why you are convinced they are correct and/or useful. –  May 26 '21 at 19:44
  • My suspicion is that zlib is simply not installed correctly on your system (as evidenced that's it's trying to load a Unix-specific header on windows, and can't find a compatibility equivalent). I can assure you that the two CMakeLists.txt I put in my answer are **correct**. The next step is to actually install zlib, or use its sources. –  May 26 '21 at 19:47