如何使用 CMake 链接"numpy/arrayobject.h"
How to use CMake to link the "numpy/arrayobject.h"
我正在做FRVT 1:1验证。所以我需要使用 FRVT 提供的程序。我已经连接到我编写的程序,并完成了实施。 但我想将我在 cython 中写的内容逐步移植到 FRVT 提供的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 提供查找模块,您必须在调用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)
我就是这样做的:
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是项目名称
您可以使用add_subdirectory()
在项目中添加numpy库。
示例
add_subdirectory(your/library/path)
相关文章:
- C++返回 Numpy 数组的 Python 扩展模块
- numpy C API 中的import_array如何工作?
- 如何使用C++范围来实现numpy.ndindex?
- 传递 boost::p ython::numpy::ndarray 作为 boost::p ython 函数的(默认或非
- 在 Cython 中将C++向量转换为 numpy 数组,而无需复制
- Boost Python Numpy - 要初始化的未定义引用
- 如何通过 cython 将 numpy 数组列表传递给 C++
- numpy.load 给出 ValueError: descr 不是有效的 dtype 描述符:
- Pybind11 默认参数 numpy 数组或 None
- C++ - 与 Numpy 中的矢量版本相比,Argsort 效率低的矢量版本实现
- 如何使用 CMake 链接"numpy/arrayobject.h"
- 相当于numpy.exexpand_dims()和numpy.contenate()的C++
- Pybind11:在C++中使用Pybind11转换numpy数组的问题
- 使用SWIG将numpy数组元素(int)传递给c++int
- 如何在 swig 中使用 numpy.i?
- 如何将 numpy 二维数组作为一种可以用C++读取的二进制格式存储在磁盘上
- Cython:如何在不需要 GIL 的情况下从 numpy 内存视图转到矢量[对[双,双]]?
- 致命错误:numpy/arrayobject.h:没有这样的文件或目录
- C++生成致命错误:找不到'numpy/arrayobject.h'文件
- 如何使用 <numpy/arrayobject.h> 在 c++ 中将数据从 np.array 获取到 std::vector?