Reputation: 16918
I have installed zlib-1.2.3.exe.
I have the following settings in CLion 2020.1:
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?
Upvotes: 1
Views: 2324
Reputation:
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)
Upvotes: 3