gpt4 book ai didi

c++ - 如何使用CMake链接 "numpy/arrayobject.h"

转载 作者:行者123 更新时间:2023-12-01 14:49:28 25 4
gpt4 key购买 nike

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

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 文件存在一些问题。使用find_package(PythonLibs ...)自 CMake 3.12 起已被弃用。您应该考虑使用较新的命令,例如 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)

关于c++ - 如何使用CMake链接 "numpy/arrayobject.h",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59062047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com