c++ - 如何使用 CMake 链接“numpy/arrayobject.h”
问题描述
我正在做 FRVT 1:1 验证。所以我需要使用FRVT提供的程序。我已经连接到我编写的程序,并完成了实现。但是我想在FRVT提供的Example中一步步移植我用cython写的东西NullImp
。但我得到了这个结果:
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 文件存在一些问题。自 CMake 3.12以来find_package(PythonLibs ...)
已弃用 。您应该考虑使用较新的命令,例如find_package(Python2 ...)
. 此外,CMake 没有专门为 NumPy 提供查找模块,您必须NumPy
在COMPONENT
调用find_package(Python2 ...)
. 这样,您可以使用Python2::NumPy
FindPython 模块定义的导入目标来获取 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)
推荐阅读
- mysql - 如何连接两个表以排序两个唯一的列?
- javascript - 拖放块的最佳位置
- html - 按方向移动浏览的统计信息
- r - 如何使用 R 在散点图中的线之间添加不同的背景颜色?
- swift - 带 URLSession 的 PromiseKit
- javascript - 未捕获的类型错误:$(...)[0].reset 不是函数
- android - 膨胀类 TextView 的错误导致活动无法启动
- java - 通过 JNI 从 Java 代码调用 .net DLL 不返回
- java - 在 Android 的 ArrayList 中添加项目时出错
- c++ - 在 Windows 中配置 CMake 以从命令行使用 clang 以获得现代 OpenMP 支持