1

我正在尝试使用 libboost 库编译我的代码,方法是放入#include <boost/python.hpp>我的 C++ 代码。有人可以帮我正确的命令来运行它,尤其是包含和链接库。我在这方面很基础。

使用的命令(但不工作):

g++ try.cpp -L /usr/lib/libboost_python.so -o try

编辑:

测试代码:

#include <boost/python.hpp>
#include <iostream>

int main() 
{

    std::cout << "Yes, it works :-)" << std::endl;

    return 0;
}

错误信息:

from try.cpp:1:
/usr/include/boost/python/enum.hpp:31: error: expected ‘;’ before ‘*’ token
/usr/include/boost/python/enum.hpp:32: error: expected ‘;’ before ‘(’ token
/usr/include/boost/python/enum.hpp:33: error: ‘PyObject’ has not been declared
/usr/include/boost/python/enum.hpp:52: error: expected constructor, destructor, or     type conversion before ‘*’ token
/usr/include/boost/python/enum.hpp:67: error: ‘void*       boost::python::enum_<T>::convertible_from_python’ is not a static member of ‘struct     boost::python::enum_<T>’
/usr/include/boost/python/enum.hpp:67: error: template definition of non-template ‘    void* boost::python::enum_<T>::convertible_from_python’
/usr/include/boost/python/enum.hpp:67: error: ‘PyObject’ was not declared in this     scope
/usr/include/boost/python/enum.hpp:67: error: ‘obj’ was not declared in this scope
    /usr/include/boost/python/enum.hpp:80: error: variable or field ‘construct’     declared void 
/usr/include/boost/python/enum.hpp:80: error: ‘PyObject’ was not declared in this     scope
/usr/include/boost/python/enum.hpp:80: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: expected primary-expression before ‘*’ token
/usr/include/boost/python/enum.hpp:80: error: ‘data’ was not declared in this scope

另一件事是,当我编译时g++ -Wall thread_one.cpp -o thread_one -lboost_thread,它可以使用 boost_thread 库。

4

2 回答 2

1

尝试这个:

g++ try.cpp -o try -lboost_python

添加到您的编译器调用中也是一种很好的风格-W -Wall -Wextra -pedantic(这样您的下一个 SO 问题可以更具体:-))。此外,-O2或者-O3用于优化可能是一个非常好的主意,尤其是对于 Boost。最后,当您有多个文件时,将构建拆分为单独的阶段可以更快地重新编译:

g++ -c -o try.o try.cpp -W -Wall -Wextra -pedantic -O2
g++ -o try try.o -s -lboost_python
于 2012-07-15T11:50:25.963 回答
1

最后,它正在工作。使用的命令如下:

g++ -I/usr/include/python2.6 try.cpp -o try -lboost_python -lpython2.6

于 2012-07-16T13:14:39.203 回答