Python Forum
[CPP] Embedding Python and use GDB
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[CPP] Embedding Python and use GDB
#1
Hi, I'm using Python 3.6 in a C ++ application. (Mingw 5.3 and GDB 7.10.1).
main.cpp :
#include <include/Python.h>

int main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  //optional but recommended
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}
project.pro :
LIBS += "D:\Python 3.6.3\libs\libpython36.a"INCLUDEPATH += "D:\\Python 3.6.3\\"DEPENDPATH += "D:\\Python 3.6.3\\"
QMAKE_CXXFLAGS += -D_hypot=hypot
This code works perfectly in release.

My problem is with Debug launch (with GDB).
Error:
Fatal Python error: Py_Initialize: unable to load the file system codec ModuleNotFoundError: No module named 'encodings' Current thread 0x00002f24 (most recent call first): Fatal Python error:  Py_Initialize: unable to load the file system codec
Thank you
Reply
#2
This is an old article, (2011), but the concepts probably haven't changed too much.
http://www.linuxjournal.com/article/11027
Probably of little use, but maybe not.
I used gdb with embedded C, and still remember that setup was a pain.
And running as well, since Rom variables must be created at run time.
Reply
#3
Under linux, with g ++ 4.6 and GDB 7.12.50.
I can debug C ++ code that uses Python3.6m without errors.
I deploy the Windows version later.
Reply
#4
I also found this, which looks a bit more promising:
https://misspent.wordpress.com/2012/03/2...n-support/
Reply
#5
Thank you for you answers.

I test that tomorrow.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Embedding python script into html via pyscript pyscript_dude 7 1,548 Apr-16-2023, 11:17 PM
Last Post: pyscript_dude
  C++ python embedding comarius 0 825 Aug-26-2022, 02:01 AM
Last Post: comarius
Question Embedding a python file online Dreary35 0 1,520 Jun-10-2021, 05:05 PM
Last Post: Dreary35
  Embedding python cause crash when use boost::asio multi threading udvatt108 0 1,716 Oct-04-2020, 03:15 PM
Last Post: udvatt108
  Need help merging/embedding duckredbeard 10 3,379 Aug-13-2020, 04:48 AM
Last Post: duckredbeard
  Embedding return in a print statement Tapster 3 2,273 Oct-07-2019, 03:10 PM
Last Post: Tapster
  Calling Extended Embedding Python as shared library jibarra 0 2,214 Jul-19-2019, 05:22 PM
Last Post: jibarra
  More Python Embedding Trouble jibarra 3 2,912 Jul-11-2019, 09:25 PM
Last Post: Gribouillis
  Python Embedding jibarra 4 3,051 Jul-11-2019, 03:25 PM
Last Post: jibarra
  Embedding or adding IDE like "repl" inside Flask app sray 1 2,218 Jul-03-2019, 03:13 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020