问题
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 the man page of pkg-config, /usr/local/lib/pkgconfig
is supposed to be one of the default search paths.
回答1:
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.
回答2:
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. 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
来源:https://stackoverflow.com/questions/9221236/pkg-config-fails-to-find-package-under-sysroot-directory