使用 CMake 查找 GLib:target_include_directories 使用无效参数调用

Finding GLib with CMake: target_include_directories called with invalid arguments

我有以下(最小)CMakeLists.txt 应该通过 pkg-config 找到 GLib 并将库添加到 foo 目标:

cmake_minimum_required(VERSION 2.10 FATAL_ERROR)
project(foo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLib REQUIRED glib-2.0)
add_executable(foo foo.cpp)
message(WARNING "libs:" ${GLIB_LIBRARIES})
message(WARNING "includes:" ${GLIB_INCLUDE_DIRS})
target_link_libraries(foo PUBLIC ${GLIB_LIBRARIES})
target_include_directories(foo PUBLIC ${GLIB_INCLUDE_DIRS})

无论我尝试什么,我都会得到(注意 Found glib-2.0 部分):

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Checking for module 'glib-2.0'
--   Found glib-2.0, version 2.56.1
CMake Warning at CMakeLists.txt:6 (message):
  libs:


CMake Warning at CMakeLists.txt:7 (message):
  includes:


CMake Error at CMakeLists.txt:9 (target_include_directories):
  target_include_directories called with invalid arguments


-- Configuring incomplete, errors occurred!
See also "/tmp/aaa/CMakeFiles/CMakeOutput.log".

我看不到,阅读 the CMake reference what arguments are invalid (note: this question is different from ). I also looked at CMake's FindPkgConfig documentationglib 为例,我无法重现它 (${GLIB_VERSION})。我尝试了 GLIB_GLIB2_ 前缀,但我得到的只是空字符串。

消息显示变量为空,但 pkg-config 报告值正确:

$ pkg-config glib-2.0 --cflags --libs
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0

我的 CMake 版本是 2.10。

有人可以阐明这个问题吗?

我认为你的问题是 GLIB_INCLUDE_DIRS 和 GLIB_LIBRARIES 的大小写问题。它们应该是 GLib_INCLUDE_DIRSGLib_LIBRARIES,因为您将 "GLib" 指定为 pkg_check_modules 的第一个参数。我猜 target_include_directories() 不喜欢在 PUBLIC(尽管它在 cmake 3.5 中对我有用)。