即使 ZLIB 存在,编译器也找不到它

时间:2021-05-26 18:04:04

标签: cmake clang clion zlib

我在 CLion 2020.1 中有以下设置:

enter image description here

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

set(ZLIB_LIBRARY "C:\\Program Files (x86)\\GnuWin32")
set(ZLIB_INCLUDE_DIR "C:\\Program Files (x86)\\GnuWin32\\include")
find_package(ZLIB REQUIRED)
find_package (ZLIB)
if (ZLIB_FOUND)
  include_directories(${ZLIB_INCLUDE_DIR})
endif (ZLIB_FOUND)

并且,编译器生成以下错误:

C:\Users\pc\CLionProjects\myproject\src\utils/io_utils.hh(8,10): fatal error: 'zlib.h' file not found
#include <zlib.h>
         ^~~~~~~~
11 warnings and 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.

我该如何纠正?

1 个答案:

答案 0 :(得分:0)

您只需要以下行:

find_package(ZLIB REQUIRED)

# To use zlib:
add_executable(main main.cpp)
target_link_libraries(main PRIVATE ZLIB::ZLIB)

然后,在命令行中,将 "-DCMAKE_PREFIX_PATH=C:/Program Files (x86)/GnuWin32" 作为参数传递给 CMake 配置步骤。 CMAKE_PREFIX_PATH 也可以设置为环境变量,其格式类似于您系统的 PATH env-var(所以 ; 在 Windows 上是分隔符,在其他地方是 :)。

通过 CMAKE_PREFIX_PATH 命令查询变量 find_* 以获取 sysroot 列表。此变量中的目录应包含子目录,如 includelibbin 等。在此处阅读有关 CMake 搜索过程的更多信息:https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure


所以你可以确切地看到我从 VS 2019 的 x64 本机工具命令提示符中所做的:

D:\>dir "C:\Program Files (x86)\GnuWin32"
 Volume in drive C has no label.
 Volume Serial Number is B854-7CB4

 Directory of C:\Program Files (x86)\GnuWin32

05/26/2021  11:40 AM    <DIR>          .
05/26/2021  11:40 AM    <DIR>          ..
05/26/2021  11:40 AM    <DIR>          bin
05/26/2021  11:40 AM    <DIR>          contrib
05/26/2021  11:40 AM    <DIR>          doc
05/26/2021  11:40 AM    <DIR>          include
05/26/2021  11:40 AM    <DIR>          lib
05/26/2021  11:40 AM    <DIR>          man
05/26/2021  11:40 AM    <DIR>          manifest
05/26/2021  11:40 AM    <DIR>          uninstall
               0 File(s)              0 bytes
              10 Dir(s)  204,769,185,792 bytes free

D:\>dir "C:\Program Files (x86)\GnuWin32\lib"
 Volume in drive C has no label.
 Volume Serial Number is B854-7CB4

 Directory of C:\Program Files (x86)\GnuWin32\lib

05/26/2021  11:40 AM    <DIR>          .
05/26/2021  11:40 AM    <DIR>          ..
07/20/2005  08:52 AM            77,534 libz.a
07/20/2005  08:50 AM            43,738 libz.dll.a
07/20/2005  08:50 AM             6,656 zlib-bcc.lib
07/20/2005  08:46 AM             1,868 zlib.def
07/20/2005  08:50 AM            14,778 zlib.lib
               5 File(s)        144,574 bytes
               2 Dir(s)  204,769,185,792 bytes free

D:\>mkdir test

D:\>cd test

D:\test>notepad CMakeLists.txt

D:\test>type CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(test)

find_package(ZLIB REQUIRED)

D:\test>cmake -S . -B build "-DCMAKE_PREFIX_PATH=C:/Program Files (x86)/GnuWin32"
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.28.29915.0
-- The CXX compiler identification is MSVC 19.28.29915.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found ZLIB: C:/Program Files (x86)/GnuWin32/lib/zlib.lib (found version "1.2.3")
-- Configuring done
-- Generating done
-- Build files have been written to: D:/test/build

它确实找到了这样的 ZLIB。