CMake的FindPkgConfig连接路径

时间:2018-08-01 08:10:28

标签: cmake pkg-config

我正在使用此MWE与CMake的FindPkgConfig module进行角力,以找到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报告的编译器args,例如。

$ 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不满意。

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

1 个答案:

答案 0 :(得分:0)

完全尴尬的解决方案(针对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

:)