从“cmake”中使用`pkg-config'的正确方法是什么? [英] What is the proper way to use `pkg-config` from `cmake`?

查看:5859
本文介绍了从“cmake”中使用`pkg-config'的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络上看到了很多这样的代码:

Looking around on the net I have seen a lot of code like this:

include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)

target_include_directories(app SYSTEM PUBLIC ${SDL2_INCLUDE_DIRS}
target_link_libraries(app ${SDL2_LIBRARIES})

然而,这似乎是错误的做法,因为它只使用include目录和库,但忽略的定义,库路径和其他标志可能返回 pkg-config

However that seems to be the wrong way about doing it, as it only uses the include directories and libraries, but ignored defines, library paths and other flags that might be returned by pkg-config.

这将是正确的方式,并确保所有的编译和链接标志返回通过 pkg-config 被编译的 app ?使用,并且有一个命令来完成这个, target_use(app SDL2)

What would be the correct way to do this and ensure that all compile and link flags returned by pkg-config are used by the compiled app? And is there a single command to accomplish this, i.e. something like target_use(app SDL2)?

推荐答案

include(FindPkgConfig)

应替换为:

find_package(PkgConfig)

find_package()调用更灵活,允许使用 REQUIRED ,自动执行必须使用 include()手动执行的操作。

The find_package() call is more flexible and allows options such as REQUIRED, that do things automatically that one would have to do manually with include().

其次,如果可能,应该避免手动调用 pkg-config 。 CMake提供了一套丰富的包定义,可在Linux中的 /usr/share/cmake-3.0/Modules/Find*cmake 下找到。这些为用户提供了更多的选项和选择,然后对 pkg_search_module()进行原始调用。

Secondly, manually calling pkg-config should be avoid when possible. CMake comes with a rich set of package definitions, found in Linux under /usr/share/cmake-3.0/Modules/Find*cmake. These provide more options and choice for the user then a raw call to pkg_search_module().

假设的 target_use()命令,CMake实际上已经有了PUBLIC | PRIVATE | INTERFACE的方式。像 target_include_directories(mytarget PUBLIC ...)的调用将导致include目录自动用于使用 mytarget target_link_libraries(myapp mytarget)。但是这种机制似乎只适用于在 CMakeLists.txt 文件中创建的库,不适用于使用 pkg_search_module() add_library(bar SHARED IMPORTED)可能会用到,但我还没有调查。

As for the mentioned hypothetical target_use() command, CMake actually already has that build-in in a way with PUBLIC|PRIVATE|INTERFACE. A call like target_include_directories(mytarget PUBLIC ...) will cause the include directories to be automatically used in every target that uses mytarget, e.g. target_link_libraries(myapp mytarget). However this mechanism seems to be only for libraries created within the CMakeLists.txt file and does not work for libraries acquired with pkg_search_module(). The call add_library(bar SHARED IMPORTED) might be used for that, but I haven't yet looked into that.

对于主要问题,这在大多数情况下适用:

As for the main question, this here works in most cases:

find_package(PkgConfig REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)
...
target_link_libraries(testapp ${SDL2_LIBRARIES})
target_include_directories(testapp PUBLIC ${SDL2_INCLUDE_DIRS})
target_compile_options(testapp PUBLIC ${SDL2_CFLAGS_OTHER})

SDL2_CFLAGS_OTHER 一个成功的编译。然而,仍然忽略标志 SDL2_LIBRARY_DIRS SDL2_LDFLAGS_OTHER ,不知道这将成为一个问题的频率。

The SDL2_CFLAGS_OTHER contains defines and other flags necessary for a successful compile. The flags SDL2_LIBRARY_DIRS and SDL2_LDFLAGS_OTHER are however still ignored, no idea how often that would become a problem.

有关更多文档,请访问 http:// www。 cmake.org/cmake/help/v3.0/module/FindPkgConfig.html

More docu at http://www.cmake.org/cmake/help/v3.0/module/FindPkgConfig.html

这篇关于从“cmake”中使用`pkg-config'的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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