0

我已经安装了zlib-1.2.3.exe

我在 CLion 2020.1 中有以下设置:

在此处输入图像描述

我的CMakeLists.txt文件中有以下几行:

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)

我在 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]);
}

并且,上面的源代码正在生成以下错误:

====================[ 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.

我怎样才能解决这个问题?

4

1 回答 1

2

CMake 中的一个重要经验法则是,如果您使用的是具有本机 CMake 支持的库,那么您应该使用find_package()+target_link_libraries()add_subdirectory()+ target_link_libraries()。手动添加标题搜索路径并链接到此类依赖项的库几乎总是错误的。

以 CMake 方式做事通常会导致为合理制作的库(zlib 当然是)正确配置事情。

选项 A:find_package()+target_link_libraries()

这需要将 zlib 安装在系统上可找到的位置。

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)

选项 B:add_subdirectory()+target_link_libraries()

这个假设 zlib 源代码的副本位于third_party/zlib相对于 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)
于 2021-05-26T19:22:30.683 回答