CMake's FindPkgConfig concatenates paths

北战南征 提交于 2019-12-11 15:15:00

问题


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})

This is the output:

[... 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!

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

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

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?


回答1:


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}
)

Which results in:

-- Configuring done
-- Generating done

:)



来源:https://stackoverflow.com/questions/51628368/cmakes-findpkgconfig-concatenates-paths

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!