shafe
shafe

Reputation: 179

How to get CMake find_package to find and use PkgConfig for cross-compilation

I am trying to cross-compile an application for a Beaglebone black.

I've successfully compiled the application directly on the Beaglebone while commenting out the cross-compile related section in my CMakeLists.txt below. I've also cross-compiled the hello program found in this tutorial: https://github.com/robamu-org/beaglebone-crosscompiling/tree/main

So my goal is to combine the cross-compiling approach from the tutorial, along with the insights from here: pkg-config fails to find package under sysroot directory

In order to get CMake to cross-compile correctly with pkg-config and GTKMM.

This is my CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

#  ~~~~~~~~~~~ CROSS COMPILE SECTION (commented out on successful beaglebone build)~~~~

set(CMAKE_TOOLCHAIN_FILE "BBBToolchain.cmake")

set(CMAKE_SYSROOT "/home/cole/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs")
set(ENV{PKG_CONFIG_DIR} "/home/cole/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs/usr/bin")
set(ENV{PKG_CONFIG_LIBDIR} "${CMAKE_SYSROOT}/usr/lib/pkgconfig:${CMAKE_SYSROOT}/usr/share/pkgconfig")
set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})

# ~~~~ CROSS COMPILE SECTION END

project(question-manager CXX)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)

add_executable(EMBEDDED-GUI src/view/embedded-app/opengtk-version/start-gui.cpp
                            src/view/embedded-app/opengtk-version/main-window.cpp
                            src/view/embedded-app/opengtk-version/question-card.cpp)

target_link_libraries(EMBEDDED-GUI PUBLIC ${GTKMM_LIBRARIES})
target_include_directories(EMBEDDED-GUI PUBLIC ${GTKMM_INCLUDE_DIRS})
target_link_directories(EMBEDDED-GUI PUBLIC ${GTKMM_LIBRARY_DIRS})
target_compile_options(EMBEDDED-GUI PUBLIC ${GTKMM_CFLAGS_OTHER})

However, when I try to configure CMake, it still uses the Host PkgConfig (my Host version is "0.29.2" while the targets version is "0.29"

$ cmake ..
-- Beagle Bone Black sysroot: /home/scshafe/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs
-- No CROSS_COMPILE environmental variable set, using default ARM linux cross compiler name arm-linux-gnueabihf
-- Using sysroot path: /home/scshafe/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs
-- Beagle Bone Black sysroot: /home/scshafe/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs
-- No CROSS_COMPILE environmental variable set, using default ARM linux cross compiler name arm-linux-gnueabihf
-- Using sysroot path: /home/scshafe/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs
-- The CXX compiler identification is GNU 7.5.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/scshafe/beaglebone/cross-compiler-toolchain/linaro-toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.2") 
-- Checking for module 'gtkmm-3.0'
--   No package 'gtkmm-3.0' found
CMake Error at /usr/share/cmake-3.22/Modules/FindPkgConfig.cmake:603 (message):
  A required package was not found
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPkgConfig.cmake:825 (_pkg_check_modules_internal)
  CMakeLists.txt:48 (pkg_check_modules)


-- Configuring incomplete, errors occurred!
See also "/home/scshafe/git-projects/question-list-manager/build/CMakeFiles/CMakeOutput.log".

If I add

set(ENV{PKG_CONFIG_PATH} "/home/scshafe/beaglebone/cross-compiler-toolchain/linaro-toolchain/rootfs/usr/lib/arm-linux-gnueabihf/pkgconfig")

PkgConfig will find gtkmm-3.0, but it is still using the the "0.29.2" Host version, and ultimately I get linkage errors when trying to build.

Upvotes: 0

Views: 441

Answers (1)

Yasushi Shoji
Yasushi Shoji

Reputation: 4274

Use PKG_CONFIG_LIBDIR instead.

With PKG_CONFIG_PATH, pkg-config will find all .pc files for the host target. Because it's adding the secondary directories to the primary ones.

For cross compile, you need to replace the primary ones.

Here is the quotes from the man page:

   PKG_CONFIG_PATH
           List of secondary directories where ‘.pc’ files are 
           looked up.

   PKG_CONFIG_LIBDIR
           List of primary directories where ‘.pc’ files are 
           looked up.

Upvotes: -1

Related Questions