possible to compile python.exe without Visual Studio, with MinGw - python

I am trying to achieve things laid out on this page:
https://blogs.msdn.microsoft.com/pythonengineering/2016/04/26/cpython-embeddable-zip-file/
The code I am trying to compile is just this:
#include "Python.h"
int
wmain(int argc, wchar_t **argv)
{
return Py_Main(argc, argv);
}
In VisualStudio 15 I have to add the python/include and link to the python libs directories in the project and also add:
#include "stdafx.h"
and then it compiles and works fine. I'm just curious, is it possible to do this with mingw, or another open source C/C++ compiler?
If I place a file my_python.cpp which contains the following:
#include "include/Python.h"
int
wmain(int argc, wchar_t **argv)
{
return Py_Main(argc, argv);
}
in the root directory of a fresh 32-bit python 3.5 install (windows 7 x64), cd to that directory and try to run:
gcc my_python.cpp -Llibs -lpython35 -o my_python.exe
I get this error:
c:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain#16'
Any way to fix this, get it running without Visual Studio?

The error is unrelated to Python. wmain is Visual Studio specific. GCC does not treat wmain as entry point, it just sits there as a function which never gets called.
GCC requires main or WinMain as entry point. If neither of those entry points is found, then the compiler will complain. So let's just use main as entry point.
Py_Main presumably expects wide character string input. CommandLineToArgvW will always provide that. Example:
#include <Windows.h>
#include "include/Python.h"
int main()
{
int argc;
wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &argc );
return Py_Main(argc, argv);
}
If you still get the same error, just provide WinMain entry point to make it happy
#include <Windows.h>
#include "include/Python.h"
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
int argc;
wchar_t** argv = CommandLineToArgvW( GetCommandLineW(), &argc );
return Py_Main(argc, argv);
}
Also note, *.lib files are usually for Visual Studio. GCC version expects library names with *.a extension.

Related

How do I compile a C program which includes Python.h on Windows?

I am trying to learn how to embed a Python interpreter into my C/C++ programs.
I like this concept because it may enable me to extend C programs at run time as well as enable users to write custom scripts/plugins.
Detailed instruction on embedding Python in C is provided at: https://docs.python.org/3/extending/embedding.html
I'm using example code provided in the Python documentation to figure out the mechanics involved:
embedded.c
#include <Python.h>
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
The problem is I can't figure out how to compile it on Windows using gcc.
C:\Users\user\embedded_python> gcc embedded.c
embedded.c:2:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
I think the problem is I need to link to the Python.h file, but I can't seem to get the compiler flags right:
C:\Users\user\Desktop\embedded_python>gcc -IC:\Users\user\AppData\Local\Programs\Python\Python38 -lPython38 embedded.c
embedded.c:2:10: fatal error: Python.h: No such file or directory
#include <Python.h>
^~~~~~~~~~
compilation terminated.
How can I get this program to compile on Windows (preferably with gcc/g++ to avoid Visual Studio bloat)?
In Linux, but I suppose if you activate your Bash from windows and can perform a work around. First you have to install python3-dev
Then use this command line where your file is located, to compile (test is your filename)
gcc -Wall test.c -o test $(pkg-config --cflags --libs python3)

Can't link Python libraries inside C program

I want to run a basic python script inside a C program using Eclipse. This is the code:
#include <Python.h>
int main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
I am trying to link to several shared libraries like libpython2.7.so in Eclipse linker settings but I always get this error:
Invoking: GCC C Linker
gcc -L/usr/lib/x86_64-linux-gnu/ -o "Test" ./src/Test.o -llibpython2.7
/usr/bin/x86_64-linux-gnu-ld: cannot find -llibpython2.7
collect2: error: ld returned 1 exit status
I can't find any tutorial with the name of the lib that should be linked.
Typically -l doesn't require the lib prefix or the .so suffix...
Try using -lpython2.7 instead of -llibpython2.7.

Qt: Undefined symbols when trying to use python.h in qt

I am doing some tiny work with Qt on Mac. I need to call a python function in my main function. But when I try to call Py_initialize() as everyone said, compiler throws a error like this:
Undefined symbols for architecture x86_64:
"_Py_Initialize", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
make: *** [video.app/Contents/MacOS/video] Error 1
07:48:07: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project video (kit: Desktop Qt 5.8.0
clang 64bit)
When executing step "Make"
My code is trivial and there should not be anything weird about this. Here it is:
// bunch of headers
#include <python2.7/Python.h>
using namespace std;
int main(int argc, char *argv[])
{
Py_Initialize();
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:///main.qml")));
QObject *object = component.create();
QObject *mainForm = object->findChild<QObject*>("mainForm");
SignalNexus sn;
QObject::connect(mainForm, SIGNAL(populateToken(QString)), &sn, SLOT(pushToPipe(QString)));
QObject::connect(&sn, SIGNAL(populateResult(QVariant, QVariant)), mainForm, SLOT(handleResult(QVariant, QVariant)));
return app.exec();
}
When I comment the Py_initalize() line, everything works fine. I figure that this is something to do with OSx. But I really don't know how to fix this thing. Help needed.
Change your include line to this
// bunch of headers
#include <Python/Python.h>
using namespace std;
int main(int argc, char *argv[])
{
Py_Initialize();

Unable to call custom module using Python/C API

File structure:
Foo/
list.so
main.cpp
list.cpp
boost_wrapper.cpp
main.cpp code:
#include <Python.h>
#include "list.cpp"
int main(int argc, char *argv[]){
PyObject *pimport;
pimport=PyString_FromString("list");
Py_SetProgramName(argv[0]);
Py_Initialize();
PyImport_Import(pimport);
/*PyRun_SimpleString("l=list.LinkedList()");
PyRun_SimpleString("l.insert(\"N\", 9)");
PyRun_SimpleString("l.display()");*/
Py_Finalize();
}
ERROR:
ImportError: No module named list
However if I run python from bash, I'm able to successfully import the module and use all functions defined. I've also tried to import using just PyRun_SimpleString with no avail.
I suspect the current working directory is invisible to the Python Interpreter called by Py_Initialize().
Add these lines after Py_Initialize(); to append your PYTHONPATH
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append(\".\")");

How can I link python embedded in a c/c++ application

I would like to use python embedded in a c++ application.
Here is my code:
#include <Python.h>
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
I have included in the c:\log\python27\include to get Python.h and also c:\log\python27\libs to have the corresponding linked library of python which is installed in c:\log\python27
It's compiling but not linking... Why?
I always have followings errors:
main.o:main.cpp:(.text+0x17): undefined reference to `_imp__Py_SetProgramName'
main.o:main.cpp:(.text+0x1e): undefined reference to `_imp__Py_Initialize'
main.o:main.cpp:(.text+0x34): undefined reference to `_imp__PyRun_SimpleStringFlags'
main.o:main.cpp:(.text+0x3b): undefined reference to `_imp__Py_Finalize'
You can use this simple command if you are using python3.5
g++ test.cpp -I/usr/include/python3.5 -lpython3.5m -o call_function
depending where your libraries are!

Categories

Resources