使用 boost.python 与 make 而不是 bjam

发布于 2024-09-26 07:00:22 字数 2969 浏览 4 评论 0原文

我只是想编译 boost.python 的“hello world”示例,而不使用所有 bjam 魔法。我的 boost.python 安装正在运行,我成功地使用 bjam 构建了示例并通过了测试套件。

现在,对于我的项目,我需要在普通的 Make 环境中使用所有这些东西。我不想移植到另一个构建工具。

因此,我天真的方法当然是将包含路径指向正确的标头并链接到正确的库。我将 boost python 构建为系统布局、静态、运行时静态,这意味着它只是驻留在 /usr/local/lib 中的 libboost_python.a。

不幸的是,我在生成的 .so 库中得到了无法解析的外部符号。

这是我尝试从 libs/python/example/tutorial/hello.cpp 构建示例:

$ cat hello.cpp
//  Copyright Joel de Guzman 2002-2004. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
//  or copy at http://www.boost.org/LICENSE_1_0.txt)
//  Hello World Example from the tutorial
//  [Joel de Guzman 10/9/2002]

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

char const* greet()
{
   return "hello, world";
}

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

$ g++ -I/usr/local/include -I/usr/include/python -fpic -c -o hello.o
hello.cpp
$ g++ -shared -Wl,-soname,"libhello.so" -L/usr/local/lib -lboost_python -fpic -o libhello.so hello.o
$ nm -u libhello.so
                 U PyString_Type
                 w _Jv_RegisterClasses
                 U _Py_NoneStruct
                 U _Unwind_Resume@@GCC_3.0
                 U _ZN5boost6python6detail11init_moduleEPKcPFvvE
                 U _ZN5boost6python6detail12gcc_demangleEPKc
                 U
_ZN5boost6python6detail17scope_setattr_docEPKcRKNS0_3api6objectES3_
                 U
_ZN5boost6python7objects15function_objectERKNS1_11py_functionE
                 U _ZN5boost6python7objects21py_function_impl_baseD2Ev
                 U _ZN5boost6python9converter19do_return_to_pythonEPKc
                 U _ZN5boost6python9converter8registry5queryENS0_9type_infoE
                 U
_ZNK5boost6python7objects21py_function_impl_base9max_arityEv
                 U
_ZNK5boost6python9converter12registration25expected_from_python_typeEv
                 U _ZTIN5boost6python7objects21py_function_impl_baseE
                 U _ZTIPKc@@CXXABI_1.3
                 U _ZTIc@@CXXABI_1.3
                 U _ZTVN10__cxxabiv120__si_class_type_infoE@@CXXABI_1.3
                 U _ZTVN5boost6python7objects21py_function_impl_baseE
                 U _ZdlPv@@GLIBCXX_3.4
                 U _Znwm@@GLIBCXX_3.4
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
                 U __cxa_guard_abort@@CXXABI_1.3
                 U __cxa_guard_acquire@@CXXABI_1.3
                 U __cxa_guard_release@@CXXABI_1.3
                 w __gmon_start__
                 U __gxx_personality_v0@@CXXABI_1.3
$ python
>>> import libhello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./libhello.so: undefined symbol:
_ZNK5boost6python7objects21py_function_impl_base9max_arityEv

那么当 bjam 链接 libboost_python.a 时,bjam 的最大魔力是什么? 我没有得到未定义的符号,但是当我“手动”执行时,我得到了这些?

I'm just trying to compile the "hello world" example of boost.python WITHOUT using all the bjam magic. My boost.python installation is working, I did succesfully build the example with bjam and passed the test suite.

Now for my project I need to use all this stuff in a plain Make environment. I don't want to port to another build tool.

So my naive approach is of course to just point the include path to the right headers and link against the right library. I built boost python as system-layout, static, runtime-static, which means it is just a libboost_python.a that resides in /usr/local/lib.

Unfortunately, I get unresolved external symbols in the resulting .so library.

Here is my try to build the example from libs/python/example/tutorial/hello.cpp:

$ cat hello.cpp
//  Copyright Joel de Guzman 2002-2004. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt
//  or copy at http://www.boost.org/LICENSE_1_0.txt)
//  Hello World Example from the tutorial
//  [Joel de Guzman 10/9/2002]

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

char const* greet()
{
   return "hello, world";
}

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

$ g++ -I/usr/local/include -I/usr/include/python -fpic -c -o hello.o
hello.cpp
$ g++ -shared -Wl,-soname,"libhello.so" -L/usr/local/lib -lboost_python -fpic -o libhello.so hello.o
$ nm -u libhello.so
                 U PyString_Type
                 w _Jv_RegisterClasses
                 U _Py_NoneStruct
                 U _Unwind_Resume@@GCC_3.0
                 U _ZN5boost6python6detail11init_moduleEPKcPFvvE
                 U _ZN5boost6python6detail12gcc_demangleEPKc
                 U
_ZN5boost6python6detail17scope_setattr_docEPKcRKNS0_3api6objectES3_
                 U
_ZN5boost6python7objects15function_objectERKNS1_11py_functionE
                 U _ZN5boost6python7objects21py_function_impl_baseD2Ev
                 U _ZN5boost6python9converter19do_return_to_pythonEPKc
                 U _ZN5boost6python9converter8registry5queryENS0_9type_infoE
                 U
_ZNK5boost6python7objects21py_function_impl_base9max_arityEv
                 U
_ZNK5boost6python9converter12registration25expected_from_python_typeEv
                 U _ZTIN5boost6python7objects21py_function_impl_baseE
                 U _ZTIPKc@@CXXABI_1.3
                 U _ZTIc@@CXXABI_1.3
                 U _ZTVN10__cxxabiv120__si_class_type_infoE@@CXXABI_1.3
                 U _ZTVN5boost6python7objects21py_function_impl_baseE
                 U _ZdlPv@@GLIBCXX_3.4
                 U _Znwm@@GLIBCXX_3.4
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
                 U __cxa_guard_abort@@CXXABI_1.3
                 U __cxa_guard_acquire@@CXXABI_1.3
                 U __cxa_guard_release@@CXXABI_1.3
                 w __gmon_start__
                 U __gxx_personality_v0@@CXXABI_1.3
$ python
>>> import libhello
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ./libhello.so: undefined symbol:
_ZNK5boost6python7objects21py_function_impl_base9max_arityEv

So what is the big magic of bjam that when bjam links libboost_python.a
I get no undefined symbols, but when i do it "by hand" i get these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半仙 2024-10-03 07:00:22

嗯,我显然是愚蠢的。要链接,必须将对象放在带有符号的库之前。所以在我用 cxxflags=-fPIC 重新编译 boost.python 后,变成

g++ -shared -Wl,-soname,"libhello.so" -L/usr/local/lib -lboost_python -fpic -o libhello.so hello.o

给了

g++ -shared -Wl,-soname,"libhello.so" -L/usr/local/lib hello.o -lboost_python -fpic -o libhello.so

我预期的结果。

Well, I was obiously stupid. To link one has to put the object BEFORE the library with the symbols. So turning

g++ -shared -Wl,-soname,"libhello.so" -L/usr/local/lib -lboost_python -fpic -o libhello.so hello.o

into

g++ -shared -Wl,-soname,"libhello.so" -L/usr/local/lib hello.o -lboost_python -fpic -o libhello.so

Gave me the expected result, after I recompiled boost.python with cxxflags=-fPIC.

待天淡蓝洁白时 2024-10-03 07:00:22

你可以尝试这样的事情:

 g++  -I/usr/include/python2.4 -fpic  hello.cpp -shared -lboost_python -o libhello.so

You could try something like this:

 g++  -I/usr/include/python2.4 -fpic  hello.cpp -shared -lboost_python -o libhello.so
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文