【问题标题】:How to use CMake to link the "numpy/arrayobject.h"如何使用 CMake 链接“numpy/arrayobject.h”
【发布时间】:2020-03-22 12:40:48
【问题描述】:

我正在进行 FRVT 1:1 验证。所以我需要使用FRVT提供的程序。我已经连接到我编写的程序,并完成了实现。 但是我想在FRVT提供的NullImpExample中一步步移植我用cython写的东西。但我得到了这个结果:

nullimplfrvt11.cpp

....
#include <Python.h>                          //(is ok)
#include "numpy/arrayobject.h" //(error)
....

CMakelists.txt

cmake_minimum_required(VERSION 2.8)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories (${CMAKE_CURRENT_SOURCE_DIR}/../include ${CMAKE_CURRENT_SOURCE_DIR}/../../../common/src/include)

# Configure built shared libraries in top-level lib directory
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../lib)

find_package(numpy REQUIRED)

find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(${PYTHON_LIBRARIES})




# Build the shared libraries
add_library (frvt_11_null_001 SHARED nullimplfrvt11.cpp)

输出:

[root@4d3eca5735a2 11]# bash run_validate_11.sh
Checking installation of required packages [SUCCESS]
Looking for core implementation library in /frvt/11/lib.[SUCCESS] Found core implementation library /frvt/11/lib/libfrvt_11_null_001.so.
Attempting to compile and link /frvt/11/lib/libfrvt_11_null_001.so against test harness.
Scanning dependencies of target validate11
[ 50%] Building CXX object src/testdriver/CMakeFiles/validate11.dir/frvt/common/src/util/util.cpp.o
[100%] Building CXX object src/testdriver/CMakeFiles/validate11.dir/validate11.cpp.o
Linking CXX executable ../../../bin/validate11
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyErr_Format'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyCObject_AsVoidPtr'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyExc_RuntimeError'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyObject_GetAttrString'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyExc_AttributeError'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyImport_ImportModule'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyErr_SetString'
../../../lib/libfrvt_11_null_001.so: undefined reference to `PyCObject_Type'
collect2: error: ld returned 1 exit status
make[2]: *** [../bin/validate11] Error 1
make[1]: *** [src/testdriver/CMakeFiles/validate11.dir/all] Error 2
make: *** [all] Error 2
[ERROR] There were errors during compilation of your library with the validation test harness.  Please investigate and re-compile.

【问题讨论】:

  • 您使用的是什么版本的 CMake?请将此信息添加到您的问题帖子中。

标签: c++ cmake linker undefined-reference


【解决方案1】:

CMake 文件存在一些问题。自 CMake 3.12 起,find_package(PythonLibs ...) 的使用已被弃用。您应该考虑使用较新的命令,例如find_package(Python2 ...)。此外,CMake 没有专门为 NumPy 提供查找模块,您必须在调用 find_package(Python2 ...) 时将 NumPy 指定为 COMPONENT。这样,您可以使用 FindPython 模块定义的导入目标Python2::NumPy 来获取 Numpy 包含和库。

target_link_libraries() 的调用必须指定一个目标 以将库链接到。 CMake 文件中定义的唯一目标是frvt_11_null_001,因此它应该是target_link_libraries() 的第一个参数。您还应该更喜欢使用 include_directories() 的特定于目标的变体,以免包含目录污染 CMake 目录范围。

通过这些修复,您的 CMake 可以看起来像这样。

cmake_minimum_required(VERSION 2.8)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Configure built shared libraries in top-level lib directory
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../lib)

find_package (Python2 COMPONENTS Interpreter NumPy)

# Build the shared libraries
add_library (frvt_11_null_001 SHARED nullimplfrvt11.cpp)
target_include_directories (frvt_11_null_001 PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/../include
    ${CMAKE_CURRENT_SOURCE_DIR}/../../../common/src/include
)
target_link_libraries(frvt_11_null_001 PUBLIC Python2::NumPy)

【讨论】:

  • 我得到“致命错误:Python.h:没有这样的文件或目录”:(
  • @abcinthehouse 我将答案调整为特定于 Python2。看起来您正在使用 Python2,这样可能会更好。尽管如果find_package() 工作正常,则不需要它,但您可以尝试将${Python2_INCLUDE_DIRS} 添加到target_include_directories() 的目录列表中。这可能有助于解决错误。
【解决方案2】:

您可以使用add_subdirectory()在项目中添加numpy库。

示例add_subdirectory(your/library/path)

【讨论】:

【解决方案3】:

我就是这样做的:

find_package(Python3 3.7 COMPONENTS Interpreter NumPy REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(c__14 ${PYTHON_LIBRARIES} Python3::NumPy)

其中 3.7 是您的版本,c__14 是项目名称

【讨论】:

    猜你喜欢
    • 2019-12-22
    • 2010-11-07
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-18
    • 2017-03-04
    • 2021-12-01
    相关资源
    最近更新 更多