简体   繁体   English

Clang 错误 - 致命错误:找不到“unistd.h”文件

[英]Clang error - fatal error: 'unistd.h' file not found

I have installed zlib-1.2.3.exe .我已经安装了zlib-1.2.3.exe

I have the following settings in CLion 2020.1:我在 CLion 2020.1 中有以下设置:

在此处输入图像描述

I have the following lines in my CMakeLists.txt file:我的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)

I have the following source code in 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]);
}

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?我怎样才能解决这个问题?

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() . CMake 中的一个重要经验法则是,如果您使用具有原生 CMake 支持的库,那么您应该使用find_package() + target_link_libraries target_link_libraries()add_subdirectory() + target_link_libraries() Manually adding header search paths and linking against libraries for these kinds of dependencies is almost always wrong.手动添加 header 搜索路径并针对此类依赖项链接库几乎总是错误的。

Doing things the CMake-way will normally lead to things getting configured correctly for a reasonably-made library (which zlib certainly is).以 CMake 方式做事通常会导致为合理制作的库(zlib 当然是)正确配置事情。

Option A: find_package() + target_link_libraries()选项 A: find_package() + target_link_libraries target_link_libraries()

This requires zlib to be installed in a findable place on your system.这需要将 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)

Option B: add_subdirectory() + target_link_libraries()选项 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.这个假设 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM