【问题标题】:Linking boost_numpy3 with CMAKE将 boost_numpy3 与 CMAKE 链接
【发布时间】:2018-05-06 15:48:17
【问题描述】:

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

find_package(Boost COMPONENTS python3 COMPONENTS numpy3 REQUIRED)

导致 CMake 链接到 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

导致链接器错误。


我使用 CMake 构建了一个依赖于 Boost python 的软件,并且从几天前开始,似乎这条线

find_package(Boost COMPONENTS numpy3 REQUIRED)

CMake 不再足以理解它应该将程序与 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的这种配置,会出现链接器错误,当我更改添加numpy到的行时,错误仍然存​​在

find_package(Boost COMPONENTS python3 COMPONENTS numpy3 REQUIRED)

这是cmake . &amp;&amp; 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。

【问题讨论】:

  • 可以通过将python2.7 添加到您的target_link_libraries 来避免链接错误本身。

标签: c++ boost cmake boost-python


【解决方案1】:

此错误是由于 FindBoost.cmake 中的依赖项描述无效

  set(_Boost_NUMPY_DEPENDENCIES python)

此问题已修复为https://github.com/Kitware/CMake/commit/c747d4ccb349f87963a8d1da69394bc4db6b74ed

请使用最新的,也可以手动重写:

  set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})

【讨论】:

  • 固定在 3.11.3
【解决方案2】:

CMake 3.10 does not properly support Boost 1.66。 Boost 依赖项是硬编码的,如果有机会,CMake 必须采用。

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

【讨论】:

  • 谢谢,但在我的情况下,CMake 3.11 出现了问题。我试过删除构建文件,但是没有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2017-11-27
  • 2021-08-21
相关资源
最近更新 更多