在Visual Studio 2005中轻松获得Python中的c++扩展

Painless way to get C++ extensions in Python in Visual Studio 2005

本文关键字:Python 中的 c++ 扩展 Studio Visual 2005      更新时间:2023-10-16

我在STLPort 5.1.0和Boot中遇到了一些非常严重的兼容性问题。Python 1.46.1在Visual Studio 2005中,我想知道是否有任何其他方法让Python调用c++代码。

万一有人能帮忙:下面的代码编译和运行没有问题:Char const* greet(){返回"hello, world";}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

当我做一些稍微复杂的事情时,链接错误开始:

#include <iostream>
#include <boost/python.hpp>
using namespace boost::python;
struct World
{
    std::string msg;
    double mypi;
    World(std::string msg): msg(msg) {} // added constructor
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    double get() const { return mypi; }
    void setter(double mypi) { this->mypi = mypi; }

};
BOOST_PYTHON_MODULE(hello_ext)
{
    class_<World>("World", init<std::string>())
        .def("greet", &World::greet)
        .def("set", &World::set)
        .def_readonly("msg",  &World::msg)
        .def_readwrite("mypi", &World::mypi)
        .add_property("rovalue", &World::get)
        .add_property("value", &World::get, &World::setter)
    ;
}

不幸的是,我一直很沮丧,我已经把我的代码弄乱了,以至于我可以重现链接错误,因为其他编译错误正在出现。但是错误是链接以'stlp'开头的未定义符号的错误,我认为这是指STLPort方法。

所以在这一点上,我只是在寻找Boost的替代方案,它在兼容性方面更容易处理。

尝试使用SWIG。