9

すべてのbjamマジックを使用せずに、boost.pythonの「hello world」の例をコンパイルしようとしています。私のboost.pythonインストールは機能しています.bjamでサンプルをうまくビルドし、テストスイートに合格しました.

今私のプロジェクトでは、プレーンな Make 環境でこれらすべてのものを使用する必要があります。別のビルド ツールに移植したくありません。

したがって、私の素朴なアプローチは、もちろん、インクルード パスを正しいヘッダーに向け、正しいライブラリにリンクすることです。私はブースト python を system-layout、static、runtime-static としてビルドしました。つまり、/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 の大きな魔法は何ですか?

4

2 に答える 2

8

まあ、私はひどく愚かでした。リンクするには、シンボルを含むライブラリの前にオブジェクトを配置する必要があります。だから回す

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

cxxflags = -fPICを使用してboost.pythonを再コンパイルした後、期待どおりの結果が得られました。

于 2010-10-07T12:12:48.883 に答える
3

次のようなことを試すことができます:

 g++  -I/usr/include/python2.4 -fpic  hello.cpp -shared -lboost_python -o libhello.so
于 2011-01-19T16:03:06.573 に答える