MUD Game Programmming - Python Modules in C++

Ulrich Eckhardt eckhardt at satorlaser.com
Wed Oct 14 04:07:14 EDT 2009


Christopher Lloyd wrote:
> 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.
[...]
> 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'

My installation of Python 2.6 doesn't have the debug-compiled Python
libaries, I guess yours doesn't either. Simple solution: don't link to
them, link to the release version instead, even in your debug build.
Unfortunately it requires this the hack with #undefing/#defining _DEBUG.

If you want, you can get the sourcecode for Python, compile it and then use
the generated debug libraries, but I would say it isn't worth the hassle
for now.

> 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.

If I understand the program correctly, it is waiting for you to enter
something. Each line is then given to Python to interpret, unless the line
is just the simple string "end". IOW, this isn't hanging or in any way
broken.

That said, it would hang if you feed it a file via input redirection, unless
the file ends with "end". I'd write the loop like this:

  Py_Initialize();
  while(getline(std::cin, line)) {
      if(line=="end")
          break;
      Py_RunSimpleString(line.c_str()); // use line
  }
  Py_Finalize();

...and possibly add some error handling to the Py_* calls.


> 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).

Note about the linker setup: You don't have to specify the library to link,
that is done in pyconfig.h with 'pragma comment(lib,"python26.lib")'. This
is MSVC-specific though.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list