Embedding Python in Code::Blocks

Started by
4 comments, last by LorenzoGatti 10 years, 9 months ago

Hello.

I am writing a game engine and wanted to use Python for game scripts. My Main.cpp file is as follows:


// Main.cpp
// This software, including the source code, is release under the terms of
// the License.

#include "Common.h"
#include <sstream>
#include <iostream>
#include "Util/Vector.h"
#include <Python.h>

using namespace std;

int main(int argc, char *argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();

    Py_Finalize();

    while (1);

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        stringstream ss;
        ss << "SDL failed to initialise: " << SDL_GetError();
        abortEngine(ss.str());
    };
    wasInitSDL = true;

    if (SDL_SetVideoMode(640, 480, 32, SDL_OPENGL) == NULL)
    {
        stringstream ss;
        ss << "Failed to open window: " << SDL_GetError();
        abortEngine(ss.str());
    };

    SDL_WM_SetCaption("Rage Engine", "Rage Engine");

    bool quitEngine = false;

    while (!quitEngine)
    {
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
            {
                quitEngine = true;
            };
        };

        SDL_GL_SwapBuffers();
    };

    SDL_Quit();
    return 0;
};

As you can see, I include Python.h and call Py_Initialize() etc. I specified in the settings the path to Python libs and the include directory, and passed the -lpython26 flag. However, when I compile, I get the following linking errors:


Linking console executable: bin\RageEngine.exe
obj\Release\Main.o:Main.cpp:(.text+0x4c): undefined reference to `_imp__Py_SetProgramName'
obj\Release\Main.o:Main.cpp:(.text+0x52): undefined reference to `_imp__Py_Initialize'
obj\Release\Main.o:Main.cpp:(.text+0x58): undefined reference to `_imp__Py_Finalize'
collect2: ld returned 1 exit status

When I opened python26.lib with notepad I could see that there were symbol names like __imp_Py_Initialize (so the number of underscores disagrees with what the IDE is trying to link with). Does anyone know what may be causing this, and how I could fix it?

Thank you.

Advertisement
The short answer is that the .lib file that comes with the Python distribution is meant for use with MSVC. You'll need to get an import library for your particular compiler. IIRC, Code::Blocks uses MinGW by default, so these instructions might help. The author of the blog entry also posted the .a for Python 2.6, but I haven't tried it myself, so I can't vouch for whether or not it works correctly.

Now it compiled, but when I try to run it an error message comes up saying "The application was not started properly (0xc000007b)".

One possible cause for that error is that your program is 64-bit and the Python DLL is 32-bit, or vice versa.

Thank you SiCrane, I changed python26.dll to 32-bit and it is now working. :)

Can you update your Python version? 2.6 is very obsolete: current versions are 3.3.2 and, if you really depend on old or abandoned libraries, 2.7.5.

Omae Wa Mou Shindeiru

This topic is closed to new replies.

Advertisement