Embedding python in Visual C++ - python

I am trying to embed python in a Visual C++ application using visual studio 2017.
I correctly included the header files path and the lib path from my Python 3.7.0 installation.
I can include the Python.h and pylifecycle.h files but get an error when I try to run the program. My code and error is below.
// TestPythonEmbedding.cpp : Defines the entry point for the console application.
//
#include "Python.h"
#include "pylifecycle.h"
#include "stdafx.h"
int main()
{
PyObject* myPointer;
Py_Initialize();
return 0;
}
Here are the errors I get when I try to build the solution.
testpythonembedding.cpp(11): error C2065: 'PyObject': undeclared
identifier
testpythonembedding.cpp(11): error C2065: 'myPointer': undeclared
identifier
testpythonembedding.cpp(13): error C3861: 'Py_Initialize': identifier
not found
1>Done building project "TestPythonEmbedding.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Strangely enough Visual studio gives information about Pyobject* and highlights it as if it is identified. I included an image of that.
image

Related

Including Boost Python libraries produces C2039 compiler error

I have built Boost_1.76 with the following configurations.
Compiler: MSVC 143
Build Tools: bootstrap.bat, b2 engine
Python Version: 3.7
b2 Command: b2 --build-dir=build/x64 address-model=64 threading=multi --build-type=complete --stagedir=./stage/x64 -j 12
// Conversion.hpp
#include <boost/python.hpp> // import produces C2039
#include <boost/python/numpy.hpp> // import produces C2039
#include <boost/tuple/tuple.hpp>
#include <boost/shared_ptr.hpp>
// Conversion.cpp
#include "Conversion.hpp" // importing the header file produces the compiler error
I am compiling the project with the following specs.
Compiler: MSVC cl.exe
C++ Standard: ISO C++17
Platform Toolset: Visual Studio 2022 (v143)
The compilation generates the following error.
C2039: '_copysign' is not a member of 'std'
File: boost_1_76_python37\boost\core\cmath.hpp
I looked at python.hpp and numpy.hpp and could only find a reference to std::cmath inside boost\python\detail\wrap_python.py, which is used over boost::core::cmath. I checked the std::cmath header and copysign is neither declared nor defined. How do I make sure that my Boost library uses copysign in boost::core::cmath?

Project File Line Suppression State Error LNK1104 cannot open file 'python310_d.lib'

I wrote a program for learning how to embed python in c++ . But when I try to run it in visual studio It shows a linker error that python310_d.lib is not found. I searched for this file in my python directory lib folder but there was no such file with this name. So now I need a way to somehow get that file. Please help me.
Here is my code
#include <iostream>
#include <Python.h>
using namespace std;
int main(int argc, char const* argv[])
{
Py_Initialize();
PyRun_SimpleString("print(\'Hellow Python\')");
Py_Finalize();
return 0;
}
Here are my errors
Severity Code Description Project File Line Suppression State
Error LNK1104 cannot open file 'python310_d.lib' Project1 C:\Users\noob\source\repos\Project1\Project1\LINK 1
It appears that the problem is you don't have the debug libraries installed. By default the installer will not install these unless you check the option to download these:
The picture and information for this answer was found in the following link:
https://github.com/pybind/pybind11/issues/3403#issuecomment-951485263

Embed python2.7 in a C program, using CMakeLists.txt. Static libraries linking

I'm doing some trials to embed python in a c program.
I use CMake 2.8.12.2 and gcc 4.8.1 to build the application. My Python version is 2.7.11. Running on W7.
This is how I'm linking the libraries in the CMakeLists.txt, which has always worked for me before with static libraries:
CMakeLists.txt
cmake_minimum_required(VERSION 2.7 FATAL_ERROR)
project(testpy)
include_directories("C:\\Anaconda\\include")
link_directories("C:\\Anaconda\\libs")
set(CMAKE_BUILD_TYPE Release)
set(SOURCE_FILES testpy.c)
add_executable(testpy ${SOURCE_FILES} Python.h)
set(PYLIB_DIR "C:\\Anaconda\\libs")
message(${PYLIB_DIR})
find_library(PYlib python27.lib HINTS ${PYLIB_DIR})
include_directories(${PYLIB_DIR})
message(${PYlib})
TARGET_LINK_LIBRARIES(testpy ${PYlib})
testpy.c
#include <Python.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\nprint('Today is',ctime(time())\n)");
Py_Finalize();
return 0;
}
The errors I am getting when making:
CMakeFiles\testpy.dir/objects.a(testpy.c.obj):testpy.c:(.text.startup+0x10): und
efined reference to `_imp__Py_Initialize'
CMakeFiles\testpy.dir/objects.a(testpy.c.obj):testpy.c:(.text.startup+0x25): und
efined reference to `_imp__PyRun_SimpleStringFlags'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: CMakeFiles
\testpy.dir/objects.a(testpy.c.obj): bad reloc address 0x25 in section `.text.st
artup'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
CMakeFiles\testpy.dir\build.make:90: recipe for target 'testpy.exe' failed
mingw32-make[2]: *** [testpy.exe] Error 1
CMakeFiles\Makefile2:62: recipe for target 'CMakeFiles/testpy.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/testpy.dir/all] Error 2
Makefile:74: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
I'm pretty shure is a linking problem, but I still not seing where it is, and how to fix it. Seen and tried also a lot of solutions suggested here and in other forums without success.
In both \include and \libs folders can be found the python.h and the python27.lib, respectively.
Thanks in advance for your feedback.

Boost, Python: Linker error with Visual Studio 2015

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

use Python/C API on msys2-mingw

I try to use Python/C API on msys2-mingw, so I install
this package mingw-w64-x86_64-python2, and I write main.cpp:
#include <python2.7\Python.h>
int main(){
return 0;
}
And when I enter this $ g++ main.cpp -o main,
It gives me these msg:
In file included from \msys64\mingw64\include/python2.7\Python.h:58:0,
from main.cpp:1:
\msys64\mingw64\include/python2.7\pyport.h:907:2: 錯誤:#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
#error "LONG_BIT definition appears wrong for platform (bad gcc/glibc config?)."
^
In file included from \msys64\mingw64\include/python2.7\Python.h:8:0,
from main.cpp:1:
/usr/include/cygwin/types.h:78:20: 錯誤:一個宣告指定了多個類型
typedef __uint32_t uid_t;
^
/usr/include/cygwin/types.h:78:20: 錯誤:沒有宣告任何東西 [-fpermissive]
/usr/include/cygwin/types.h:84:20: 錯誤:一個宣告指定了多個類型
typedef __uint32_t gid_t;
^
/usr/include/cygwin/types.h:84:20: 錯誤:沒有宣告任何東西 [-fpermissive]
makefile:2: recipe for target 'main' failed
make: *** [main] Error 1
I have no idea why these happen. It's not a error about linking file(since I hadn't link any file.). Is my method wrong, or I include the wrong file ?
You mix GCC's: mingw-w64 GCC and msys GCC. For mingw-w64 GCC under MSYS2 you must not have includes from /usr. See your error message /usr/include/cygwin/types.h

Categories

Resources