代码之家  ›  专栏  ›  技术社区  ›  Rastapopoulos

将boost\u numpy3与CMAKE链接

  •  3
  • Rastapopoulos  · 技术社区  · 6 年前

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

    find\u包(需要Boost COMPONENTS python3 COMPONENTS numpy3)

    导致针对3个不同库的CMake链接

    -- 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 相反

    这里有一个简单的工作示例来重现我所说的内容。

    测验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.1 ,则, boost 1.66.0-2 ,并运行更新版本的Archlinux。

    2 回复  |  直到 6 年前
        1
  •  3
  •   termoshtt    6 年前

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

      set(_Boost_NUMPY_DEPENDENCIES python)
    

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

    请使用最新版本,或手动重写:

      set(_Boost_NUMPY_DEPENDENCIES python${component_python_version})
    
        2
  •  2
  •   usr1234567    6 年前

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

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