除了 Boost.Python(对于需要 C++ 类到 python 类的一对一映射的大型项目,这可能是一个更友好的解决方案),您可以在 C++ 端提供一个 C 接口。它是众多解决方案中的一种,因此它有自己的权衡取舍,但我将介绍它以使那些不熟悉该技术的人受益。为了全面披露,使用这种方法不会将 C++ 连接到 python,而是将 C++ 连接到 C 到 Python。下面我提供了一个满足您要求的示例,以向您展示 C++ 编译器的 extern "c" 工具的一般概念。
//YourFile.cpp (compiled into a .dll or .so file)
#include <new> //For std::nothrow
//Either include a header defining your class, or define it here.
extern "C" //Tells the compile to use C-linkage for the next scope.
{
//Note: The interface this linkage region needs to use C only.
void * CreateInstanceOfClass( void )
{
// Note: Inside the function body, I can use C++.
return new(std::nothrow) MyClass;
}
//Thanks Chris.
void DeleteInstanceOfClass (void *ptr)
{
delete(std::nothrow) ptr;
}
int CallMemberTest(void *ptr)
{
// Note: A downside here is the lack of type safety.
// You could always internally(in the C++ library) save a reference to all
// pointers created of type MyClass and verify it is an element in that
//structure.
//
// Per comments with Andre, we should avoid throwing exceptions.
try
{
MyClass * ref = reinterpret_cast<MyClass *>(ptr);
return ref->Test();
}
catch(...)
{
return -1; //assuming -1 is an error condition.
}
}
} //End C linkage scope.
您可以使用以下代码编译此代码
gcc -shared -o test.so test.cpp
#creates test.so in your current working directory.
在您的 python 代码中,您可以执行以下操作(显示 2.7 的交互式提示):
>>> from ctypes import cdll
>>> stdc=cdll.LoadLibrary("libc.so.6") # or similar to load c library
>>> stdcpp=cdll.LoadLibrary("libstdc++.so.6") # or similar to load c++ library
>>> myLib=cdll.LoadLibrary("/path/to/test.so")
>>> spam = myLib.CreateInstanceOfClass()
>>> spam
[outputs the pointer address of the element]
>>> value=CallMemberTest(spam)
[does whatever Test does to the spam reference of the object]
我确信 Boost.Python 在幕后做了类似的事情,但也许理解较低级别的概念会有所帮助。如果您尝试访问 C++ 库的功能并且不需要一对一映射,我会对这种方法感到更加兴奋。
有关 C/C++ 交互的更多信息,请查看 Sun 的此页面:http: //dsc.sun.com/solaris/articles/mixing.html#cpp_from_c