set PKG_CONFIG_PATH in cmake

后端 未结 2 1914
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 15:07

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

相关标签:
2条回答
  • 2021-02-13 15:46

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

    Here are the steps:

    Method I :

    CMAKE_PREFIX_PATH can be set to find the .pc files

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

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

    Irrespective of which method you use,

    For old (traditional) CMake:

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

    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

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

    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.

    for examaple:

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

    For modern 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)
    

    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) 
    
    0 讨论(0)
  • 2021-02-13 15:52

    You could set PKG_CONFIG_PATH with the CMake line:

    set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:/my/path/to/pkgconfig")
    

    I did this workaround in this file

    Interestingly, it seems CMake 3.1 extends PKG_CONFIG_PATH with some CMake variable see: https://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=3df51470

    0 讨论(0)
提交回复
热议问题