MUD Game Programmming - Python Modules in C++

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Oct 13 18:34:43 EDT 2009


En Tue, 13 Oct 2009 18:08:53 -0300, Christopher Lloyd  
<llocr at btinternet.com> escribió:

> #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'

For a debug build of your program, you require a debug build of Python. A  
release build should compile and link OK with the pre-built Python  
libraries you got, as you already noticed.

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

Hung? Or waiting for you to input some Python expression to be evaluated?
That's what the std::getline( std::cin, str ) line does. Type 2+3 for  
example - you should get the answer.

> 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

No, don't do that. Just compile your application in release mode.

-- 
Gabriel Genellina




More information about the Python-list mailing list