将boost_numpy3与CMAKE链接

时间:2018-05-06 15:48:17

标签: c++ boost cmake boost-python

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 . && 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的更新版本。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

此错误是由FindBoost.cmake

中的无效依赖关系描述引起的
  set(_Boost_NUMPY_DEPENDENCIES python)

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

修复此问题

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

  set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})