如何正确链接到python库?

时间:2014-11-01 03:15:08

标签: python c++ gcc boost

我使用本教程构建了静态python :( python版本3.4.2)

  

Compile the Python interpreter statically?

一切正常,差不多。当我的简单测试应用程序链接到库时:

  

libpython3.4m.a

这是简单演示的源代码:

using namespace boost::python;
using namespace std;

class World {
private:
    string name;
public:
    void set(string name) {
        this->name = name;
    }
    void greet() {
        printf("name is %s\n", this->name.c_str());
    }
};

typedef boost::shared_ptr< World > world_ptr;

BOOST_PYTHON_MODULE(hello) {
    class_<World>("World")
    .def("greet", &World::greet)
    .def("set", &World::set)
    ;
};
int main ( int argc, char** argv ) {


    Py_NoSiteFlag = 1;
    Py_FrozenFlag = 1;
    //Py_IgnoreEnvironmentFlag = 1;


    Py_SetPythonHome(L".");
    Py_SetProgramName(L"");

    PyImport_AppendInittab("hello",PyInit_hello);

    Py_Initialize();

    try {


        PyRun_SimpleStringFlags("import hello", NULL);
        PyRun_SimpleStringFlags("ha = hello.World()", NULL);
        PyRun_SimpleStringFlags("ha.set('tango')", NULL);
        PyRun_SimpleStringFlags("ha.greet()", NULL);

    }
    catch(error_already_set) {
        PyErr_Print();
    }

    Py_Finalize();
    return 0;
}

问题在于,当我运行演示时,它说:

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

然后我将Lib目录从python目录复制到demo目录,然后在运行我的应用程序之前导出它:

export PYTHONPATH="Lib/"

然后问题就消失了,但问题是:

Can I compile all the package in the Lib to static library libpython3.4m.a? so that I don't have to carry Lib files?

0 个答案:

没有答案