如何使用 CMake 链接“numpy/arrayobject.h”

abc*_*use 6 c++ linker cmake undefined-reference

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

nullimplfrvt11.cpp

....
#include <Python.h>                          //(is ok)
#include "numpy/arrayobject.h" //(error)
....
Run Code Online (Sandbox Code Playgroud)

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)

Run Code Online (Sandbox Code Playgroud)

输出:

[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.
Run Code Online (Sandbox Code Playgroud)

squ*_*les 5

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

调用target_link_libraries()必须指定将库链接到的目标。CMake 文件中定义的唯一目标是frvt_11_null_001,因此它应该是 的第一个参数target_link_libraries()。您还应该更喜欢使用include_directories()so 的目标特定变体,以免包含目录污染 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)
Run Code Online (Sandbox Code Playgroud)