3

I'm trying to embed Python within my C++ project (Qt5). My project looks like this:

python_test.pro:

QT += core
QT -= gui

TARGET = python_test
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

INCLUDEPATH += C:\Tools\Python\Python35_64\include

LIBS += -LC:\Tools\Python\Python35_64\ -lpython3

main.cpp:

#include <Python.h>
#include <QCoreApplication>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                   "print('Today is', ctime(time()))\n");
    Py_Finalize();
    return a.exec();
}

When compiling I get a linker error telling me this: main.cpp:-1: Error: undefined reference to `__imp_PyRun_SimpleStringFlags'.

The funny thing is that Py_Initialize() and Py_Finalize() can be found. I read something about the define Py_LIMITED_API which hides the function PyRun_SimpleStringFlags. But I don't get it.

How am I supposed to run a Python script/file/string without these functions being available within the C API?

Setup:

3
  • It may make a difference if you link -lpython35 and not -lpython3. Commented Jan 26, 2016 at 10:57
  • [offtopic] I would suggest to look at pythonqt (pythonqt.sourceforge.net) it's a really powerful way to bind Qt applications and python (it's PyQt the other way round) Commented Jan 26, 2016 at 11:06
  • @bibi I already have taken a look at this library and it seems perfect. But I had problems compiling it/debugging projects with this library included. Tried to contact the developers but didn't get an answer. So I decided to stick to CPython API since I need only to run scripts for converting strings. Commented Jan 26, 2016 at 11:47

1 Answer 1

1

I tested this on my computer (without Qt though), with -lpython35 compiling succeeded and with -lpython3 it did not.

So

LIBS += -LC:\Tools\Python\Python35_64\ -lpython35

instead of

LIBS += -LC:\Tools\Python\Python35_64\ -lpython3
Sign up to request clarification or add additional context in comments.

3 Comments

This did the job, thank you! Is it correct that python3 (python3.dll) creates the define Py_LIMITED_API to keep API compatibility over all python 3 versions (3.2, 3.4 etc) ?
@Burner If you put #define Py_LIMITED_API in your source file before #include <Python.h>, and then try to compile, you will notice that function PyRun_SimpleString won't be available and python3.dll corresponds to this limited API.
Thanks for the explanation! I'm now facing a problem while deploying my application to another pc. The application starts fine but as soon as a script is executed (PyRun_SimpleString is reached) the application crashes with an error within the file: C:\windows\system32\ucrtbase.dll. The dll is available on all the PCs I'm working at. I don't know what to make of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.