将 boost_numpy3 与 CMAKE 链接

Linking boost_numpy3 with CMAKE

TL;DR 在 archlinux 上从 CMake 3.10 更新到 CMake 3.11.1 时,以下配置行:

find_package(提升组件 python3 需要 numpy3 组件)

导致 CMake link针对 3 个不同的库

-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   python3
--   numpy3
--   python

而不是以前的行为:

-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   python3
--   numpy3

导致 link 错误。


我使用 CMake 构建了一个依赖于 Boost python 的软件,而且,从几天前开始,

find_package(Boost COMPONENTS numpy3 REQUIRED)

不再足以让 CMake 理解它应该 link 针对 Boost python3 库的程序,它使用 Boost 库 python 代替。

这是一个最小的工作示例,可以重现我所说的内容。

test.cpp

#include <iostream>

using namespace std;
int main()
{
    cout << "Hello, world!" << endl;
}

CMakeList.txt

set(CMAKE_VERBOSE_MAKEFILE ON)
find_package(PythonLibs 3 REQUIRED)
find_package(Boost COMPONENTS numpy3 REQUIRED)
add_executable (test test.cpp)
target_link_libraries(test ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

使用CMake的这种配置,会出现linker错误,当我更改将numpy添加到

的行时,错误仍然存​​在
find_package(Boost COMPONENTS python3 COMPONENTS numpy3 REQUIRED)

这是 cmake . && make 的结果:

/home/rastapopoulos/test $ cmake .
-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   numpy3
--   python
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.11)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /home/rastapopoulos/test
/home/rastapopoulos/test $ make
/usr/bin/cmake -H/home/rastapopoulos/test -B/home/rastapopoulos/test --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/rastapopoulos/test/CMakeFiles /home/rastapopoulos/test/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/rastapopoulos/test'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/home/rastapopoulos/test'
cd /home/rastapopoulos/test && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/rastapopoulos/test /home/rastapopoulos/test /home/rastapopoulos/test /home/rastapopoulos/test /home/rastapopoulos/test/CMakeFi
les/test.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/rastapopoulos/test'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/home/rastapopoulos/test'
[ 50%] Linking CXX executable test
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/c++    -rdynamic CMakeFiles/test.dir/test.o  -o test -lboost_numpy3 -lboost_python -lpython3.6m
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_Size'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyUnicodeUCS4_FromEncodedObject'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyFile_FromString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_Type'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyInt_Type'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_FromString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyUnicodeUCS4_AsWideChar'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_FromStringAndSize'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `Py_InitModule4_64'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_FromFormat'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyNumber_Divide'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyNumber_InPlaceDivide'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyInt_AsLong'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_InternFromString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyClass_Type'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyString_AsString'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyInt_FromLong'
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.1/../../../../lib/libboost_python.so: undefined reference to `PyFile_AsFile'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test.dir/build.make:90: test] Error 1
make[2]: Leaving directory '/home/rastapopoulos/test'
make[1]: *** [CMakeFiles/Makefile2:71: CMakeFiles/test.dir/all] Error 2
make[1]: Leaving directory '/home/rastapopoulos/test'
make: *** [Makefile:87: all] Error 2

有没有人遇到过类似的问题并设法解决了?我使用 cmake 3.11.1boost 1.66.0-2 和 运行 Archlinux 的更新版本。

。 Boost 依赖项是硬编码的,如果有机会,CMake 必须采用。

删除构建目录并重新配置。配置步骤使用缓存变量,以防止使用较新的例程进行重新检测。

此错误是由于 FindBoost.cmake

中的依赖项描述无效所致
  set(_Boost_NUMPY_DEPENDENCIES python)

这已在 https://github.com/Kitware/CMake/commit/c747d4ccb349f87963a8d1da69394bc4db6b74ed

修复

请使用最新的,或者您可以手动重写:

  set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})