【问题标题】:CMake's FindPkgConfig concatenates pathsCMake 的 FindPkgConfig 连接路径
【发布时间】:2018-08-01 08:10:28
【问题描述】:

我正在与 CMake 的 FindPkgConfig module 搏斗,使用此 MWE 找到 glib-2.0 并正确设置标志:

cmake_minimum_required(VERSION 2.10 FATAL_ERROR)
project(foo)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLIB REQUIRED glib-2.0)
# print everything which could be defined, as per FindPkgConfig documentation
message(WARNING "GLIB_LIBRARIES:" ${GLIB_LIBRARIES})
message(WARNING "GLIB_LIBRARY_DIRS:" ${GLIB_LIBRARY_DIRS})
message(WARNING "GLIB_LDFLAGS:" ${GLIB_LDFLAGS})
message(WARNING "GLIB_LDFLAGS_OTHER:" ${GLIB_LDFLAGS_OTHER})
message(WARNING "GLIB_INCLUDE_DIRS:" ${GLIB_INCLUDE_DIRS})
message(WARNING "GLIB_CFLAGS:" ${GLIB_CFLAGS})
message(WARNING "GLIB_CFLAGS_OTHER:" ${GLIB_CFLAGS_OTHER})
# try to set flags now:
add_executable(foo foo.cpp)
target_include_directories(foo PUBLIC ${GLIB_INCLUDE_DIRS})
target_link_libraries(foo PUBLIC ${GLIB_LIBRARIES})

这是输出:

[... toolchain detection omited ...]
-- 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):
  GLIB_LIBRARIES:glib-2.0


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


CMake Warning at CMakeLists.txt:8 (message):
  GLIB_LDFLAGS:-lglib-2.0


CMake Warning at CMakeLists.txt:9 (message):
  GLIB_LDFLAGS_OTHER:


CMake Warning at CMakeLists.txt:10 (message):

  GLIB_INCLUDE_DIRS:/usr/include/glib-2.0/usr/lib/x86_64-linux-gnu/glib-2.0/include


CMake Warning at CMakeLists.txt:11 (message):

  GLIB_CFLAGS:-I/usr/include/glib-2.0-I/usr/lib/x86_64-linux-gnu/glib-2.0/include


CMake Warning at CMakeLists.txt:12 (message):
  GLIB_CFLAGS_OTHER:


CMake Error at CMakeLists.txt:13 (target_include_directories):

target_include_directories called with invalid arguments


-- Configuring incomplete, errors occurred!

读取输出,FindPkgConfig 似乎正在连接 pkg-config 报告的编译器参数,因为例如

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

请注意这是两条路径,尽管来自 CMake 上面的消息显示它们连接在一起:

GLIB_INCLUDE_DIRS:/usr/include/glib-2.0/usr/lib/x86_64-linux-gnu/glib-2.0/include

GLIB_CFLAGS:-I/usr/include/glib-2.0-I/usr/lib/x86_64-linux-gnu/glib-2.0/include

CMake 显然对不存在的包含路径 /usr/include/glib-2.0/usr/lib/x86_64-linux-gnu/glib-2.0/include 不满意。

怎么了?我应该以某种方式手动拆分这些论点吗?

【问题讨论】:

  • 连接字符串的不是 CMake 本身,而是 message() 命令,它将所有参数连接到一个字符串中。如果您想更好地打印列表,请引用它:message(WARNING "GLIB_LIBRARIES: ${GLIB_LIBRARIES}")。至于其他命令,将 unquoted 列表传递给它们是一种正确的方法。很奇怪你从 CMake 得到这样的errors
  • 是的,message(WARNING "GLIB_INCLUDE_DIRS: ${GLIB_INCLUDE_DIRS}") 我得到GLIB_INCLUDE_DIRS: [newline] /usr/include/glib-2.0;/usr/lib/x86_64-linux-gnu/glib-2.0/include - 所以分隔符被MESSAGE 删除了,正如你所说的。

标签: cmake pkg-config


【解决方案1】:

完全令人尴尬的解决方案(对于 CMake 语法和完全无信息且具有误导性的错误消息)是将每个变量单独放在单独的行上:

add_executable(foo foo.cpp)
target_include_directories(foo PUBLIC
    ${GLIB_INCLUDE_DIRS}
)
target_link_libraries(foo PUBLIC
    ${GLIB_LIBRARIES}
)

结果:

-- Configuring done
-- Generating done

:)

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多