• Advertisement
Sign in to follow this  

Embedding Python in Code::Blocks

This topic is 1647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement