9

Can anyone please tell me why this might fail:

afeder@ubuntu:~/android/toolchain/sysroot$ ls $PKG_CONFIG_SYSROOT_DIR/usr/local/lib/pkgconfig/mozjs185.pc
/home/afeder/android/toolchain/sysroot/usr/local/lib/pkgconfig/mozjs185.pc

afeder@ubuntu:~/android/toolchain/sysroot$ pkg-config mozjs185 --cflags
Package mozjs185 was not found in the pkg-config search path.
Perhaps you should add the directory containing `mozjs185.pc'
to the PKG_CONFIG_PATH environment variable
No package 'mozjs185' found

According to man pkg-config(1), /usr/local/lib/pkgconfig is supposed to be one of the default search paths.

pevik
  • 4,523
  • 3
  • 33
  • 44
Anders Feder
  • 595
  • 2
  • 5
  • 18

2 Answers2

21

I found the answer here: http://www.flameeyes.eu/autotools-mythbuster/pkgconfig/cross-compiling.html

The wrapper script should not only set the PKG_CONFIG_SYSROOT_DIR variable: when cross-compiling you want to ignore the packages installed in the system, and instead rely only on those installed in the cross-compiled environment. This is achieved by resetting PKG_CONFIG_DIR (which lists additional search paths), and at the same time setting PKG_CONFIG_LIBDIR to override the default base search paths.


The resulting CMake File would be something like this:

set(CMAKE_SYSROOT "/path/to/sysroot")

set(ENV{PKG_CONFIG_DIR} "")
set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})

Disclaimer: I used the CMAKE_SYSROOT variable which is useful when you want to pass -sysroot to g++. If you don't want this you should name your variable differently.

pevik
  • 4,523
  • 3
  • 33
  • 44
Anders Feder
  • 595
  • 2
  • 5
  • 18
  • 4
    Hi, could you possibly show the command you issued to get this working, thanks – puk Dec 02 '13 at 10:41
  • Per https://linux.die.net/man/1/pkg-config, I think you mean `PKG_CONFIG_PATH` instead of `PKG_CONFIG_DIR`. – wulfgarpro Jan 29 '22 at 08:06
1

If you're using a cross-compiler, then you need to 1) install the appropriate pkg-config wrapper and 2) set the appropriate environment variables to be picked up by the wrapper. You can find the appropriate wrapper with apt-cache search pkg-config- and then install the appropriate one for the target system. E.g. if cross-compiling to armhf: sudo apt-get install pkg-config-arm-linux-gnueabihf. Then, set environment variables PKG_CONFIG_DIR, PKG_CONFIG_LIBDIR, and PKG_CONFIG_SYSROOT_DIR as required before invoking the wrapper.

If you're cross compiling using CMake, note that CMake's pkg_search_module command (provided by FindPkgConfig.cmake) doesn't appear to correctly set the environment for the wrapper. Rather, you should use the PKG_CONFIG_PATH environment variable.

# Set these in your toolchain.cmake file
set(triple arm-linux-gnueabihf)
set(PKG_CONFIG_EXECUTABLE ${triple}-pkg-config)
set(ENV{PKG_CONFIG_PATH} "$ENV{PKG_CONFIG_PATH}:${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/lib/${triple}/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")

See also set PKG_CONFIG_PATH in cmake

bleater
  • 5,098
  • 50
  • 48