Can't link Python libraries inside C program - python

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.

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)

Calling Python from C++, how to link particular Anaconda enviroment?

I am trying to embed Python into C++ following this guide here:
https://docs.python.org/3.7/extending/embedding.html
This works very well for an older system-wide Python 2.7 installation. I am now trying to do the same using a particular Anaconda enviroment where I installed a library I want to use.
My hello world example for this is
#include <iostream>
#include <Python.h>
int main(int argc, char* argv[]){
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
Py_SetProgramName(program);
Py_Initialize();
std::cout << "trying to import dolfin" << std::endl;
PyRun_SimpleString("import dolfin");
std::cout << "success?" << std::endl;
Py_Finalize();
return 0;
}
Trying to compile this via
g++ -I/home/peter/anaconda3/envs/fenicsproject/include/python3.7m/ python_from_cpp.cpp -L/home/peter/anaconda3/envs/fenicsproject/lib/python3.7/config-3.7m-x86_64-linux-gnu/ -lpython3.7m
(Note1: The include part works, the other flags are recommended linking flags, see 1.6. in above link)
(Note2: I want to compile via mpic++ in the end, but trying to get this going first.)
This gives the output:
lto1: internal compiler error: in lto_tag_to_tree_code, at lto-streamer.h:1005
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
lto-wrapper: fatal error: g++ returned 1 exit status
compilation terminated.
/usr/bin/ld: error: lto-wrapper failed
collect2: error: ld returned 1 exit status
Any help/insights are appreciated.

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();

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

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.

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