I am trying to use matplotlib-cpp for a C++ project compiled by Visual Studio using CMake.
The python distribution I use is Anaconda2 (latest 2.7 version downloaded today). I removed every other python distribution that was on my computer.
I added the paths to the Anaconda2 folder to the system AND user environment variables. (C:\Anaconda2...)
CMake is finding it correctly as when configuring the project with CMake, I have:
Found PythonInterp: C:/Anaconda2/python.exe (found suitable version "2.7.16", minimum required is "2.7")
Found PythonLibs: C:/Anaconda2/libs/python27.lib (found suitable version "2.7.16", minimum required is "2.7")
The project builds correctly with VS (no build or link error) but when I run a quick hello world, I have an error :
Hello World!
ImportError: No module named site
This is the main.cpp:
#include <iostream>
using namespace std;
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main(int argc, char **argv)
{
cout << "Hello World!" << endl;
plt::plot({ 1,3,2,4 });
plt::show();
return 0;
}
This is the CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(PLT)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
# main app
add_executable(
plt
src/main.cpp
)
# Matplotlib
find_package(PythonInterp 2.7 REQUIRED)
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
target_link_libraries(plt ${PYTHON_LIBRARIES})
I read on this forum that this might be caused by some Path issues, but the path to python directories is set:
I am using Microsoft Visual Studio 2017 to build with CMake's "Visual Studio 15 2017 Win64" generator and I run my program through "cmd" and I think the problem is configuring the default python used by "cmd".
I precise that I tried matplotlib on Anaconda Python's prompt and it is working.
Any idea on how to fix this?
This usually occurs when the PYTHONHOME path is invalid or not set
try:
set PYTHONHOME=C:\Python27
Related
I used the visual studio installer to install python and could run the basic python program.
Now I want to make a new c++ console program and call the python script from there.
When googling I saw I need to include the "Python.h" in my c++ function. I tried to include and i get the message "cannot open the source file" . Is it because I used visual studio to install the python. And most of googling have installed python separately.
Older versions of visual studio, just installed python interpretter. So installed Visual studio 2019 and had a option to select Python3
With that option , it created a new directory, C:\Python27amd64\
Now I created a new console application:
Code:
#include <iostream>
#include "C:\Python27amd64\include\Python.h"
int main()
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print('Today is',ctime(time()))\n");
//std::cout << "Hello World!\n";
}
And also followed the following steps :
Properties > C/C++ > General > Additional Include Directories. I added the "C:\Python27amd64\include"
2.Properties > Linker > General > Additional Library Directories. : I added C:\Python27amd64\libs
3.Properties > Linker > Input > Additional Dependencies: I added python27.lib
I changed the build to "Release x64". Because it couldnot find the debug libraries
From a related post, one installed package matplotlib-cpp through microsoft /
vcpkg . However, when running a simple example,
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::show();
}
an error returned stating that
Error C1083 Cannot open include file: 'Python.h': No such file or directory Examples
..\vcpkg\installed\x64-windows\include\matplotlibcpp.h 5
I have tried the methods in:
can't include Python.h in visual studio
psycopg: Python.h: No such file or directory and Cygwin gcc issue - cannot find Python.h (which didn't apply since vs managed python environment itself, and there wasn't python-dev option)
A very similiar post C++: matplotlibcpp.h and Python.h linker error also indicated the same issue in Ubuntu environment.
In matplotlibcpp.h file, it mentioned that
// Python headers must be included before any system headers, since
// they define _POSIX_C_SOURCE
#include <Python.h>
What did it mean? How to link python.h and a matplotlibcpp.h in Visual Studio 2019?
Updates:
I was able to locate "Python.h" and "numpy/arrayobject.h" in MS python folder. However, when I manually added those three ".h" files into the project header folder, the issue wasn't resolved. Especially, when I manually fixed the locations in "matplotlibcpp.h", the "Python.h" in ""numpy/arrayobject.h" still could not be located.
So, I'm trying to integrate some python code to c++ project. For that purpose I've created simple test project using clion. But met with a problem. Working on OS - Ubuntu 18.04.2
"/usr/include/boost/python/detail/wrap_python.hpp:50:11: fatal error: pyconfig.h: No such file or directory" on the line
include boost/python.hpp
Seen some solutions like:
"add export
CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/usr/include/python2.7/" to
bashrc".
Tried that - nothing.
Here is the cmakelist
cmake_minimum_required(VERSION 3.14)
project(pythonInCPPIntegration)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost 1.65.1 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(pythonInCPPIntegration main.cpp)
target_link_libraries(pythonInCPPIntegration ${Boost_LIBRARIES})
C-proj looks like that, nothing more
#include <iostream>
#include <boost/python.hpp>
int main() {
}
Appreciate any help!
Well, it was kinda linkage problem. Solved with some manipulations over cmakelists.txt
cmake_minimum_required(VERSION 3.14)
project(pythonInCPPIntegration)
set(CMAKE_CXX_STANDARD 14)
find_package(Boost 1.65.1 COMPONENTS system filesystem REQUIRED)
find_package(PythonLibs)
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${PYTHON_INCLUDE_PATH})
add_executable(pythonInCPPIntegration main.cpp)
target_link_libraries(pythonInCPPIntegration ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
I basically did the same as the person here:
Building/including Boost.Python in VS2013
However, I used an empty cpp file with only the main function and the inclusion of <boost/python.hpp>
#include <boost/python.hpp>
int main() {
return 0;
}
Now I get the strange linker error (in Visual Studio):
1>LINK : fatal error LNK1104: cannot open file 'boost_python-vc140-mt-gd-1_60.lib'
Which is strange, because I have the lib file I think, however, it is called:
libboost_python3-vc140-mt-gd-1_60.lib
You need to configure your Visual C++ project setting.
The following case operates well.
[debug platform mode] x64
[include directory] (..\;;);C:\boost\boost_1_60_0\;C:\Python35\include\; # add your actual boost and python directory path
[library directory] (..\;;);C:\Python35\libs\;C:\boost\boost_1_60_0\stage\lib; # add your actual boost and python library path
I want to build simple app with pybind11, pybind is already installed in my Ubuntu system with cmake (and make install). I use this simple cmake file:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(trt_cpp_loader )
find_package(pybind11 REQUIRED)
add_executable(trt_cpp_loader main.cpp)
set_property(TARGET trt_cpp_loader PROPERTY CXX_STANDARD 11)
This is main.cpp:
#include <iostream>
#include <pybind11/embed.h>
namespace py = pybind11;
using namespace std;
int main(){return 0;}
when I build it, I get:
In file included from /usr/local/include/pybind11/pytypes.h:12:0,
from /usr/local/include/pybind11/cast.h:13,
from /usr/local/include/pybind11/attr.h:13,
from /usr/local/include/pybind11/pybind11.h:44,
from /usr/local/include/pybind11/embed.h:12,
from /home/stiv/lpr/trt_cpp_loader/main.cpp:2:
/usr/local/include/pybind11/detail/common.h:112:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
how can I fix this problem? (python-dev and python3-dev are already installed, Python.h is available)
You'll want to use the pybind11_add_module command (see https://pybind11.readthedocs.io/en/stable/compiling.html#building-with-cmake) for the default case of creating an extension module.
If the goal is indeed to embed Python in an executable, it is your reponsibility to explicitly add python headers & libraries to the compiler/linker commands in CMake. (see https://pybind11.readthedocs.io/en/stable/compiling.html#embedding-the-python-interpreter on how to do that)
Following the Wenzel Jakob's answer I want to put an example of CMakeLists.txt for compiling the example provided in this tutorial:
// example.cpp
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
}
and
# example.py
import example
print(example.add(1, 2))
and
# CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(example)
find_package(pybind11 REQUIRED)
pybind11_add_module(example example.cpp)
now in the root run
cmake .
make
now run the python code by
python3 example.py
P.S. I have also written some instructions here for compiling/installing the pybind11.
Maybe just install the Python headers? For example, on Ubuntu you can install the sudo apt-get install python-dev (or python3-dev or pythonX.Y-dev) package. That could resolve this.