CMake的FindPkgConfig连接路径 [英] CMake's FindPkgConfig concatenates paths

查看:622
本文介绍了CMake的FindPkgConfig连接路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMake的 FindPkgConfig模块 MWE查找glib-2.0并正确设置标志:

I am wresting with CMake's FindPkgConfig module using this MWE to find glib-2.0 and set flags correctly:

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报告的编译器arg,因为eg

Reading the output, it seems that FindPkgConfig is concatenating compiler args reported by pkg-config, since e.g.

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

注意这虽然是两条路径上面的CMake消息显示它们是串联的:

Notice it is two paths though messages from CMake above show them concatenated:

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

and CMake is obviously not happy with non-existent include path /usr/include/glib-2.0/usr/lib/x86_64-linux-gnu/glib-2.0/include.

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

What's wrong? Am I supposed to split those arguments by hand somehow?

推荐答案

完全尴尬的解决方案(对于CMake语法和完全不兼容的解决方案) -信息性和误导性错误消息)是将每个变量本身放在单独的行上:

The solution which is utterly embarassing (for CMake syntax and the completely non-informative & misleading error message) is to put each variable on separate line by itself:

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

这会导致以下结果:

-- Configuring done
-- Generating done

:)

这篇关于CMake的FindPkgConfig连接路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆