2010-10-06 96 views
8

我只是想編譯boost.python的「hello world」例子而不使用所有的bjam魔法。我的boost.python安裝正在運行,我成功地使用bjam構建了示例並通過了測試套件。使用boost.python而不是bjam

現在對於我的項目,我需要在普通的Make環境中使用所有這些東西。我不想移植到另一個構建工具。

所以我天真的做法當然是指出包含正確頭的路徑並鏈接到正確的庫。我建立了boost python作爲系統佈局,靜態,運行時靜態,這意味着它只是一個駐留在/ usr/local/lib中的libboost_python.a。

不幸的是,我在得到的.so庫中得到了未解析的外部符號。

這是我嘗試建立從庫/蟒蛇/例子/教程/ 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 [email protected]@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 [email protected]@CXXABI_1.3 
       U [email protected]@CXXABI_1.3 
       U [email protected]@CXXABI_1.3 
       U _ZTVN5boost6python7objects21py_function_impl_baseE 
       U [email protected]@GLIBCXX_3.4 
       U [email protected]@GLIBCXX_3.4 
       U [email protected]@GLIBC_2.2.5 
       w [email protected]@GLIBC_2.2.5 
       U [email protected]@CXXABI_1.3 
       U [email protected]@CXXABI_1.3 
       U [email protected]@CXXABI_1.3 
       w __gmon_start__ 
       U [email protected]@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的,當bjam的鏈接libboost_python.a 我拿到大魔沒有未定義的符號,但是當我「手工」的時候,我得到了這些?

+0

我發現的bjam構建使用的Boost.Python的共享庫。當我部署共享庫時,上述工作。但我絕對需要一個靜態構建。那麼爲什麼鏈接libboost_python.a會產生未定義的符號,libboost_python.so的作用? – Philipp 2010-10-06 19:36:13

回答

7

那麼,我是愚蠢的愚蠢。要鏈接一個,必須將帶有符號的庫之前的對象。所以轉向

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的。

2

你可以嘗試這樣的事:

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