在cmake中设置PKG_CONFIG_PATH [英] set PKG_CONFIG_PATH in cmake

查看:1025
本文介绍了在cmake中设置PKG_CONFIG_PATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在本地构建了opencv并将其安装到本地目录(不是系统默认目录)。 opencv.pc 出现在此本地文件夹中的pkgconfig文件夹下。我怎么能从cmake找到这个 opencv.pc ,因为我想链接并包含程序中的opencv文件。

I have built opencv locally and installed it to a local directory (not the system default ). opencv.pc is present under a folder pkgconfig in this local folder. How can I find this opencv.pc from cmake, because I want to link and include opencv files from my program.

pkg_search_module(<PREFIX> [REQUIRED] [QUIET] <MODULE> [<MODULE>]*)

没有任何参数可以强制命令使用特定路径(类似于使用 find_package()的HINTS ),而不是系统默认值。

does not have any parameter in which I can force the command to use a specific path (similar to HINTS with find_package()) and not the system default.

因此,基本上 .cmake 起作用:

find_package(OpenCV REQUIRED HINTS "my/path/to/share/OpenCVConfig.cmake") 

但我想使用位于 my / path /下的 opencv.pc 到/pkgconfig/opencv.pc

but I would like to use a opencv.pc located under my/path/to/pkgconfig/opencv.pc.

推荐答案

经过一些研究和@提示OlivierM,我找到了答案。

After doing some research and a hint from the @OlivierM, I found the answer.

以下是步骤:

方法I

CMAKE_PREFIX_PATH 可以设置为找到 .pc 文件

set(CMAKE_PREFIX_PATH "${CMAKE_SOURCE_DIR}/libs/opencv-install")

方法II
第二种方法是使用 PKG_CONFIG_PATH ,它是系统环境变量,用于查找 .pc 文件。

Method II A second method is to use the PKG_CONFIG_PATH, which is a system environment variable to look for .pc files.

set(ENV{PKG_CONFIG_PATH} "${CMAKE_SOURCE_DIR}/libs/opencv-install/lib/pkgconfig")

无论使用哪种方法,

旧(传统)CMake:

find_package(PkgConfig REQUIRED)
pkg_search_module(PKG_OPENCV REQUIRED opencv)  # this looks for opencv.pc file

请注意, PKG_OPENCV 变量可以命名为任何东西。无论它是什么名称,都将其用作前缀。例如,如果您命名 ABCD ,则包含目录将为 ABCD_INCLUDE_DIRS

Please note that the PKG_OPENCV variable can be named anything. Whatever it is is named, its used as a prefix. For example if you name ABCD, then include directories will be ABCD_INCLUDE_DIRS

变量 PKG_OPENCV_INCLUDE_DIRS PKG_OPENCV_LIBRARIES 分别包含头文件(编译阶段)和库(链接阶段) 。

The variable PKG_OPENCV_INCLUDE_DIRS and PKG_OPENCV_LIBRARIES contains the header files (compile stage) and libraries (link stage) respectively.

我注意到的一个非常重要的事情是,变量 PKG_OPENCV_LIBRARIES 仅提供库,而不是库路径。链接阶段。为了在一个命令中同时使用库路径,必须使用

One very important thing I noticed was that the variable PKG_OPENCV_LIBRARIES just provides the libraries and not the library path during the link stage. In order to use the library path as well in one command, one has to use

PKG_OPENCV_LDFLAGS 

此变量包含库路径以及软件包配置文件中列出的所有库。

This variable contains the library path as well as all the libraries listed in the package config file.

例如:

include_directories(${PKG_OPENCV_INCLUDE_DIRS})
target_link_libraries (FINAL_BINARY ${PKG_OPENCV_LDFLAGS})

对于现代CMake:

在现代CMake中,我们不需要变量,而需要目标。

In modern CMake we don't want variables, we want targets.

find_package(PkgConfig REQUIRED)
# this looks for opencv.pc file and creates a new target
# IMPORTED_TARGET requires CMake >= 3.6.3
pkg_search_module(PKG_OPENCV REQUIRED IMPORTED_TARGET opencv)

仍将创建所有变量以实现向后兼容,但 IMPORTED_TARGET 将创建一个可以在项目中使用的目标,该目标将自动传播其所有构建和使用要求:

All variables will still be created for backwards compatibility, but IMPORTED_TARGET will create a target you can use in your project which will automatically propagate all of its build and usage requirements:

target_link_libraries(my_proj PRIVATE PkgConfig::PKG_OPENCV) 

这篇关于在cmake中设置PKG_CONFIG_PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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