FindPython returns host library when cross-compiling on Ubuntu
This issue refers to the new FindPython
, FindPython2
, and FindPython3
scripts in the master branch (!1819 (merged)).
On an Ubuntu 14.04 amd64
machine, I am trying to cross-compile an application that embeds the Python interpreter to the armhf
architecture.
Given the following CMakeLists.txt
file and cross-arm32.cmake
toolchain file:
cmake_minimum_required(VERSION 3.11)
project(test_cmake_python C)
find_package(Python2 2.7 COMPONENTS Development REQUIRED)
add_executable(main src/main.c)
target_include_directories(main PRIVATE ${Python2_INCLUDE_DIRS})
target_link_libraries(main PRIVATE ${Python2_LIBRARIES})
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR armv7)
set(CMAKE_C_COMPILER /usr/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabihf-g++)
set(CMAKE_FIND_ROOT_PATH /usr/arm-linux-gnueabihf)
On Ubuntu 14.04 with crossbuild-essential-armhf
, python-dev
, and python-dev:armhf
packages installed, I executed the following commands:
mkdir build
cd build
~/cmake/bin/cmake .. -DCMAKE_TOOLCHAIN_FILE=../cross-arm32.cmake
Among other things, the following is printed:
-- Found Python2: /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so (found suitable version "2.7.6", minimum required is "2.7") found components: Development
This is the Python library for the host architecture rather than the target architecture. This results in a link error during subsequent build.
The correct Python library for armhf
should be:
/usr/lib/python2.7/config-arm-linux-gnueabihf/libpython2.7.so
Additional comments:
- Using
FindPython
rather thanFindPython2
triggers the same issue. - This issue is also reproducible on Ubuntu 16.04.
- The same issue is encountered when I attempt to cross-compile to
aarch64
rather thanarmhf
. - Because the Python multiarch libraries and include directories under Ubuntu all reside under
/usr/lib
and/usr/include
, settingCMAKE_FIND_ROOT_PATH_MODE_LIBRARY
andCMAKE_FIND_ROOT_PATH_MODE_INCLUDE
toONLY
appears to be not a solution.
Edited by Brad King