Skip to content
GitLab
  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • CMake CMake
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,874
    • Issues 3,874
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 19
    • Merge requests 19
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Releases
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • CMake
  • CMakeCMake
  • Issues
  • #22148
Closed
Open
Created May 04, 2021 by Dax Koci@daxkoci

FindPkgConfig: Fails on non-system paths in LIBRARY_PATH

I have a separate development environment with privately compiled gcc and certain libraries. Lately I stumbled across an issue where CMake's PkgConfig uses the system-wide library, instead of from my private environment, though I think I have done everything according to the documentation.

The library in my case is x265. The experiment uses the latest snapshot of CMake (3.20.20210504-g576a070) from the repository. Let's assume my environment is at /home/SpaceX/gcc-env. The following paths are set in the system's environment:

PATH:
    /home/SpaceX/gcc-env/bin
    ...
LD_LIBRARY_PATH:
    /home/SpaceX/gcc-env/lib
LIBRARY_PATH:
    /home/SpaceX/gcc-env/lib
PKG_CONFIG_PATH:
    /home/SpaceX/gcc-env/lib/pkgconfig
C_INCLUDE_PATH:
    /home/SpaceX/gcc-env/include
CPLUS_INCLUDE_PATH:
    /home/SpaceX/gcc-env/include

pkg-config correctly finds information about x265:

$ pkg-config --variable=prefix x265
/home/SpaceX/gcc-env

Let's consider the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)

project(mwe)

add_executable(mwe mwe.cpp)

find_package(PkgConfig REQUIRED)
pkg_check_modules(X265 REQUIRED IMPORTED_TARGET x265)

target_include_directories(mwe PRIVATE
    ${X265_INCLUDE_DIRS}
)
target_compile_options(mwe PRIVATE
    ${X265_CFLAGS_OTHER}
)
target_link_libraries(mwe
    PkgConfig::X265
)
target_link_options(mwe PRIVATE
    ${X265_LDFLAGS_OTHER}
)
message(STATUS "X265_FOUND = ${X265_FOUND}")
message(STATUS "X265_VERSION = ${X265_VERSION}")
message(STATUS "X265_PREFIX = ${X265_PREFIX}")
message(STATUS "X265_INCLUDEDIR = ${X265_INCLUDEDIR}")
message(STATUS "X265_LIBDIR = ${X265_LIBDIR}")

The following command

cmake -DCMAKE_BUILD_TYPE=Debug \
      -DCMAKE_C_COMPILER=gcc \
      -DCMAKE_CXX_COMPILER=g++ \
      -H. -Bbuild -G Ninja

will output

$ cmake --version
cmake version 3.20.20210504-g576a070
$ cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -H. -Bbuild -G Ninja
-- The C compiler identification is GNU 11.1.1
-- The CXX compiler identification is GNU 11.1.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /home/SpaceX/gcc-env/bin/gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/SpaceX/gcc-env/bin/g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "1.7.3") 
-- Checking for module 'x265'
--   Found x265, version 3.5
-- X265_FOUND = 1
-- X265_VERSION = 3.5
-- X265_PREFIX = /home/SpaceX/gcc-env
-- X265_INCLUDEDIR = /home/SpaceX/gcc-env/include
-- X265_LIBDIR = /home/SpaceX/gcc-env/lib
-- X265_MODULE_NAME = x265

So far everything is alright. However, the compilation produces an unexpected result:

$ cmake --build build --verbose --config Debug --target all
[1/2] /home/SpaceX/gcc-env/bin/g++   -g -MD -MT CMakeFiles/mwe.dir/mwe.cpp.o -MF CMakeFiles/mwe.dir/mwe.cpp.o.d -o CMakeFiles/mwe.dir/mwe.cpp.o -c ../mwe.cpp
[2/2] : && /home/SpaceX/gcc-env/bin/g++ -g  CMakeFiles/mwe.dir/mwe.cpp.o -o mwe  /usr/lib/libx265.so && :

I.e. the generated linker command for some reason uses a full path to the system library: /usr/lib/libx265.so. Not even simply libx265.so, but an exact path nobody asked about. The library in question in fact exists at the X265_LIBDIR:

$ ls -lA /home/SpaceX/gcc-env/lib/*x265*
-rw-r--r-- 1 SpaceX SpaceX 12359618 мая  3 21:43 /home/SpaceX/gcc-env/lib/libx265.a
lrwxrwxrwx 1 SpaceX SpaceX       14 мая  3 20:51 /home/SpaceX/gcc-env/lib/libx265.so -> libx265.so.200*
-rwxr-xr-x 1 SpaceX SpaceX  8022848 мая  3 21:43 /home/SpaceX/gcc-env/lib/libx265.so.200*

I attached the MWE with x265.pc for reference. mwe.tar.xz

Edited Jul 06, 2021 by Brad King
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking