Jump to content

  • Log In with Google      Sign In   
  • Create Account

Embedding Python in Code::Blocks


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Madd   Members   -  Reputation: 174

Like
0Likes
Like

Posted 13 July 2013 - 12:56 PM

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.



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9668

Like
2Likes
Like

Posted 13 July 2013 - 02:15 PM

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.

#3 Madd   Members   -  Reputation: 174

Like
0Likes
Like

Posted 13 July 2013 - 02:54 PM

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



#4 SiCrane   Moderators   -  Reputation: 9668

Like
2Likes
Like

Posted 13 July 2013 - 04:16 PM

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



#5 Madd   Members   -  Reputation: 174

Like
1Likes
Like

Posted 13 July 2013 - 06:24 PM

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



#6 LorenzoGatti   Crossbones+   -  Reputation: 2762

Like
0Likes
Like

Posted 19 July 2013 - 01:12 AM

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.


Produci, consuma, crepa




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS