6

I am doing FRVT 1:1 verification. So I need to use the program provided by FRVT. I have connected to the program I wrote, and completed implementation. But I want to transplant what I wrote in cython step by step in the NullImp Example provided by FRVT. But I got this result:

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)

output:

[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.
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • What version of CMake are you using? Please add this information to your question post. – Kevin Nov 27 '19 at 13:30

3 Answers3

5

There are a few issues with the CMake file. The use of find_package(PythonLibs ...) has been deprecated since CMake 3.12. You should consider using the newer commands, such as find_package(Python2 ...). Also, CMake does not provide a find module specifically for NumPy, you have to specify NumPy as a COMPONENT when you call find_package(Python2 ...). This way, you can use the imported target Python2::NumPy defined by the FindPython module to get the Numpy includes and library.

The call to target_link_libraries() must specify a target to link the libraries to. The only target defined in your CMake file is frvt_11_null_001, so that should be the first argument to target_link_libraries(). You should also prefer to use the target-specific variant of include_directories() so to not pollute the CMake directory scope with include directories.

With these fixes, your CMake can look something like this.

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)
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • i get the "fatal error: Python.h: No such file or directory" :( – abc in the house Nov 27 '19 at 05:33
  • @abcinthehouse I adjusted my answer to be specific to Python2. It looked like you are using Python2 so that may work better. Although you shouldn't need it if `find_package()` worked correctly, you can try adding `${Python2_INCLUDE_DIRS}` to your directory list in `target_include_directories()`. This may help with the error. – Kevin Nov 27 '19 at 13:35
3

This is how I did it:

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

where 3.7 is your version and c__14 is the project name

Vega
  • 27,856
  • 27
  • 95
  • 103
1

You can use add_subdirectory() to add numpy library in project.

example add_subdirectory(your/library/path)

Bharat Vankar
  • 317
  • 2
  • 13