###発生している問題
お世話になります。pythonでの計算にかかる時間を短縮するために、boost.pythonを使ってみようとしています。
しかし、サンプルコードをコンパイルするときにエラーが出てしまい、原因としてはリンクがうまくいっていないようなのですが、どう対処すれば良いかわからず困っております。どうぞよろしくお願いいたします。
###内容
boost.pythonをhomebrewでインストールしたのち、
サンプルコードhello.cpp
C++
1#include <boost/python.hpp> 2 3int add(int lhs,int rhs){ 4 return lhs + rhs; 5} 6 7BOOST_PYTHON_MODULE(hello){ 8 using namespace boost::python; 9 def("add",add); 10}
のコンパイルをしようとしたところ、
g++ -Wall -O2 -fpic -c hello.cpp -I/Users/home/.pyenv/versions/anaconda-2.4.0/include/python2.7
は通るのですが、
g++ -shared -o hello.so -I/Users/home/.pyenv/versions/anaconda-2.4.0/include/python2.7 hello.o -lboost_python
を実行すると、
Undefined symbols for architecture x86_64: "_PyInt_FromLong", referenced from: boost::python::detail::caller_arity<2u>::impl<int (*)(int, int), boost::python::default_call_policies, boost::mpl::vector3<int, int, int> >::operator()(_object*, _object*) in hello.o "_PyInt_Type", referenced from: boost::python::detail::converter_target_type<boost::python::to_python_value<int const&> >::get_pytype() in hello.o "__Py_NoneStruct", referenced from: __GLOBAL__sub_I_hello.cpp in hello.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
というエラーが返されました。
###補足
g++のバージョンの問題かもしれないと思い、
g++-4.9 -Wall -O2 -fpic -c hello.cpp -I/Users/home/.pyenv/versions/anaconda-2.4.0/include/python2.7 g++-4.9 -shared -o hello.so -I/Users/home/.pyenv/versions/anaconda-2.4.0/include/python2.7 hello.o -lboost_python
も試してみましたが、1行目で
/var/folders/8m/f7vkk6hj709__67zk43qlskw0000gn/T//cc8XvGsD.s:19:11: warning: section "__textcoal_nt" is deprecated .section __TEXT,__textcoal_nt,coalesced,pure_instructions ^ ~~~~~~~~~~~~~ /var/folders/8m/f7vkk6hj709__67zk43qlskw0000gn/T//cc8XvGsD.s:19:11: note: change section name to "__text" .section __TEXT,__textcoal_nt,coalesced,pure_instructions ^ ~~~~~~~~~~~~~
のようなwarningが大量に出され、2行目を実行すると
Undefined symbols for architecture x86_64: "_PyInt_FromLong", referenced from: boost::python::objects::caller_py_function_impl<boost::python::detail::caller<int (*)(int, int), boost::python::default_call_policies, boost::mpl::vector3<int, int, int> > >::operator()(_object*, _object*) in hello.o "_PyInt_Type", referenced from: boost::python::detail::converter_target_type<boost::python::to_python_value<int const&> >::get_pytype() in hello.o "__Py_NoneStruct", referenced from: __GLOBAL__sub_I_hello.cpp in hello.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status
が返されて結局コンパイルできませんでした。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。