Boost, Python: Linker error with Visual Studio 2015 - python

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

Related

Build errors created by PYBIND11_MOUDLE when binding python and cpp using pybind11 in visual studio

I am trying to bind my CPP function and use it in python code using pybind11. I have created a visual studio project that includes the function (does not have the main function). I cloned pybind11 from git and added it and python38 /include folders to Configuration Properties/VC++ Directories/Include Directories and python38 /libs folder to Configuration Properties/VC++ Directories/Library Directories. Also, I added the python38.lib file to Linker/input/Additional Dependencies.
I added
#include <Python.h>
#include <pybind11/pybind11.h>
to the top of my code. And the lines
And I get build errors of the form "unresolved external symbol
"LNK2001 unresolved external symbol __imp_PyThread_tss_create"
PYBIND11_MODULE(my_kmeans, handle)
{
handle.doc() = "Kmeans Clustering";
handle.def("my_kmeans", &kmeans, "kmeans function");
}
Can you please help me fix them?
I tried looking for a .lib file in pybind11 folder but I did not find one.

python from c++ in visual studio

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

ImportError when importing pybind11 module compiled with cmake/mingw32-make

Im currently trying to write a c++ extension to python and only have a little bit of c++ code so far ... just to test the workflow/compilation.
#include "conservation.h"
#include <pybind11/pybind11.h>
double calculateMomentum(double mass, double velocity) {
return mass * velocity;
}
PYBIND11_MODULE(conservation, m) {
m.doc() = "Conservation-quantity calculator";
m.def("calculate_momentum", &calculateMomentum, "Returns Momentum of given parameters");
}
Then i create a makefile with the following configuration:
cmake_minimum_required(VERSION 3.4...3.19)
project(Calculation LANGUAGES CXX)
set(pybind11_DIR $CACHE{pybind11_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../extensions)
find_package(pybind11 REQUIRED)
pybind11_add_module(conservation ./src/calculation/conservation.cpp)
Afterwards i can compile the extension without any problem on Windows and Linux (using mingw32-make and make respectively)
When I try importing it in Linux it works without any problems or issues and I can run the calculate_momentum function.
But when I try importing the extension on a Windows machine I get the following Error:
ImportError: DLL load failed while importing conservation: The parameter is incorrect.
I'm not a very experienced user of cmake or pybind11 so it is entirely possible that I am doing something completely wrong and any input would be very much appreciated.

How to link python.h and matplotlibcpp.h in Visual Studio 2019?

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.

Matplotlib-cpp : ImportError: No module named site

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

Categories

Resources