MUD Game Programmming - Python Modules in C++

Christopher Lloyd llocr at btinternet.com
Tue Oct 13 17:08:53 EDT 2009


Hello all,

I'm new to Python and new to this list, although I've done some digging in the archives and already read up on the problem I'm about to describe.

I'm a relatively inexperienced programmer, and have been learning some basic C++ and working through the demos in Ron Penton's "MUD Game Programming" book. In it, Python modules are run from inside a C++ program.

The problem that I'm having is making the Python part work. Take the following code:

// This program shows you how to integrate Python in a very basic manner

#include <iostream>
#include <string>
#include "Python.h"

int main()
{
    std::cout << "Starting Python Demo Test" << std::endl;

    Py_Initialize();                // initialize python

    std::string str;
    std::getline( std::cin, str );
    while( str != "end" )
    {
        PyRun_SimpleString( const_cast<char*>( str.c_str() ) );
        std::getline( std::cin, str );
    }

    Py_Finalize();                  // shut down python
    
	std::cout << "Demo Complete!" << std::endl;
    
	return 0;
}

If I try to compile this in MS Visual C++ 2008 (debug mode), I get the following error:

LINK : fatal error LNK1104: cannot open file 'python26_d.lib'

>From my reading, it looks like there's a problem with compiling in release mode or debug mode either in C++ or in Python. At the moment, I'm using the Python Windows .exe download. I'm not using version 2.6 for any particular reason - I'm also trying 2.2 and 2.3.

So I don't have the Python source code downloaded (and I'm not entirely sure what to do with it if I do download it, since the instructions for the .tar file are for Linux, not windows). The Windows executables for 2.2 and 2.3 came on a CD with the book, so it seems clear that the author thought that the source code wasn't required anyway.

So, what happens if I try compiling the above C++ in release mode? Actually, nothing - It compiles just fine. However, upon running the resulting program, my command line box displays the following:

>   Starting Python Demo Test

That's all. The program has hung halfway through and the test isn't completed.

It's been suggested that I replace the first part of my C++ code with the following, and then try to compile in release mode:

#ifdef _DEBUG
#undef _DEBUG
#include <Python.h>
#define _DEBUG
#else
#include <Python.h>
#endif

I've tried this, and it compiles successfully but when run, the program is the same - It doesn't work.

I've correctly set up all my library files and link (at least, lets assume its not that, since I've already spent several hours checking and re-checking that).

I'd be very grateful for any help or asvice people might have on this.

Thanks,

Chris Lloyd

-- 





More information about the Python-list mailing list