Home > Enterprise >  C : numpy/arrayobject.h: No such file or directory CMake VSCode
C : numpy/arrayobject.h: No such file or directory CMake VSCode

Time:03-29

I want to include Python.h and numpy/arrayobject.h in my C script. But it cannot open these source files.

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(myProj VERSION 0.1.0 DESCRIPTION "myProj")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c  11")
find_package(PythonLibs REQUIRED)                                   <--- work
find_package(Python3 3.6 COMPONENTS Interpreter NumPy REQUIRED)     <--- not work
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(myProj main.cc)
target_link_libraries(myProj ${PYTHON_LIBRARIES} Python3:NumPy)

Error

[cmake] CMake Error at CMakeLists.txt:9 (find_package):
[cmake]   By not providing "FindPython3.cmake" in CMAKE_MODULE_PATH this project has
[cmake]   asked CMake to find a package configuration file provided by "Python3", but
[cmake]   CMake did not find one.
[cmake] 
[cmake]   Could not find a package configuration file provided by "Python3"
[cmake]   (requested version 3.6) with any of the following names:
[cmake] 
[cmake]     Python3Config.cmake
[cmake]     python3-config.cmake
[cmake] 
[cmake]   Add the installation prefix of "Python3" to CMAKE_PREFIX_PATH or set
[cmake]   "Python3_DIR" to a directory containing one of the above files.  If
[cmake]   "Python3" provides a separate development package or SDK, be sure it has
[cmake]   been installed.

Path of NumPy

(venv) cyan@linux01:~/TEMP$ python -c "import numpy; print(numpy.get_include())"
/home/cyan/venv/lib/python3.6/site-packages/numpy/core/include

Question

How can I revise my CMakeLists.txt so to include the header numpy/arrayobject.h in C ?

CodePudding user response:

I'm not seeing a lot of documentation for FindPython3 or FindPython for CMake 3.0.0 so I'm not sure if that module included before CMake 3.12.0. Version 3.12 is the first occurence I could find in the cmake documentation.

The most likely thing is that you just need to change to using

cmake_minimum_required(VERSION 3.12.0)

Updating your CMake version is probably the simplest solution but again you might have good reason to limit the version and just telling you to update isn't much of a solution.

Another hackier solution would be to use one of the paths from Python_INCLUDE_DIRS or Python_LIBRARIES do some relative pathing from there. Something like

set(numpy_INCLUDE_DIR "${Python_INCLUDE_DIR}/../site-packages/numpy
/core/include")
include_directories(${Python_INCLUDE_DIRS} ${numpy_INCLUDE_DIR})

This however assumes that your python directories are setup as if they came from a python distribution and would in turn assume that everyone that uses this also has a distribution with the same file structure.

  • Related