2010-10-06 5 views
8

J'essaie juste de compiler l'exemple "hello world" de boost.python SANS utiliser toute la magie de bjam. Mon installation boost.python fonctionne, j'ai réussi à construire l'exemple avec bjam et j'ai réussi la suite de tests. Maintenant, pour mon projet, j'ai besoin d'utiliser toutes ces choses dans un environnement simple. Je ne veux pas porter vers un autre outil de construction.Utiliser boost.python avec make au lieu de bjam

Donc, mon approche naïve est bien sûr de simplement pointer le chemin d'inclusion vers les bons en-têtes et le lien avec la bonne bibliothèque. J'ai construit boost python en tant que system-layout, static, runtime-static, ce qui veut dire que c'est juste un libboost_python.a qui réside dans/usr/local/lib.

Malheureusement, j'obtiens des symboles externes non résolus dans la bibliothèque .so résultante.

Voici mon essai de construire l'exemple de libs/python/exemple/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 [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 

Alors, quelle est la grande magie de bjam que lorsque des liens bjam libboost_python.a je reçois pas de symboles indéfinis, mais quand je le fais "à la main" je reçois ces?

+0

Je trouve que la construction de bjam utilise la bibliothèque partagée de Boost.Python. Lorsque je déploie la bibliothèque partagée, les opérations ci-dessus fonctionnent. Mais j'ai définitivement besoin d'une construction statique. Alors pourquoi la liaison de libboost_python.a génère-t-elle des symboles indéfinis, où fonctionne libboost_python.so? – Philipp

Répondre

7

Eh bien, j'étais obiously stupide. Pour lier un doit mettre l'objet AVANT la bibliothèque avec les symboles. Donc tourner

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

dans

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

m'a donné le résultat attendu, après avoir recompilé Boost.Python avec CXXFLAGS = -fPIC.

2

Vous pouvez essayer quelque chose comme ceci:

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