前提・実現したいこと
科学技術計算をする上でpythonで書かれた関数をc++で呼び出し、またはその逆を行う必要が出たので、boost pythonをhomebrew経由でインストールしたのですが、コンパイルの際にincludeエラーが出てしまいます。
参考にしたwebサイト様は(https://seiichiinoue.github.io/post/boost/)で、解説されている通りに以下の手順でセットアップを行いました。
① "brew install boost-python3"でboost-python3をインストール。
② boost-python3の場所を確認。
$ ls /usr/local/lib | grep libboost_python39 libboost_python39-mt.a libboost_python39-mt.dylib libboost_python39.a libboost_python39.dylib
③ "find /usr/local -name pyconfig.h"でpyconfig.hの場所が
/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyconfig.h
/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/include/python3.9/pyconfig.h
にあることを確認。
④ .bashrcに
export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/include/python3.9/" ```と打ち込み反映させる。 ⑤ sample.cpp(以下に示します)をhomebrewのg++-11で以下のようにコンパイル。
g++-11 -Wall -DPIC -shared -fPIC -o sample.so sample.cpp -lboost_python39 -I/usr/local/lib python3-config --include
python3-config --ldflags
としたところ以下のようなインクルードエラーが出てしまいます。 ### 発生している問題・エラーメッセージ
sample.cpp:1:10: fatal error: boost/python: No such file or directory
1 | #include <boost/python>
| ^~~~~~~~~~~~~~
compilation terminated.
### 該当のソースコード > https://seiichiinoue.github.io/post/boost/ のサンプルコードからの引用です。 ```c++ #include <boost/python> int add(int v1, int v2) { return v1 + v2; } BOOST_PYTHON_MODULE(test) { using namespace boost::python; def("add", add); }
試したこと
・clang++によるコンパイル
・python.hppが"/usr/local/Cellar/boost/1.76.0/include/boost/python.hpp"にあったので、#includeにフルのパスを入れる
jbpb0様の質問に関しまして、"/usr/local/opt/boost/include"以下には"boost/"と続き、その下にはpythonとpython.hppのファイルがありました。
・コードのinclude部分を"#include <boost/python.hpp>"として、
g++-11 -Wall -DPIC -shared -fPIC -o sample.so sample.cpp -I/usr/local/opt/boost/include -L/usr/local/lib -lboost_python39 `python3-config --include` `python3-config --ldflags`
としたところ、
In file included from /usr/local/opt/boost/include/boost/python/detail/is_xxx.hpp:8, from /usr/local/opt/boost/include/boost/python/detail/is_auto_ptr.hpp:9, from /usr/local/opt/boost/include/boost/python/detail/copy_ctor_mutates_rhs.hpp:8, from /usr/local/opt/boost/include/boost/python/detail/value_arg.hpp:7, from /usr/local/opt/boost/include/boost/python/object/forward.hpp:10, from /usr/local/opt/boost/include/boost/python/object/pointer_holder.hpp:16, from /usr/local/opt/boost/include/boost/python/to_python_indirect.hpp:10, from /usr/local/opt/boost/include/boost/python/converter/arg_to_python.hpp:10, from /usr/local/opt/boost/include/boost/python/call.hpp:15, from /usr/local/opt/boost/include/boost/python/object_core.hpp:14, from /usr/local/opt/boost/include/boost/python/args.hpp:22, from /usr/local/opt/boost/include/boost/python.hpp:11, from sample.cpp:1: /usr/local/opt/boost/include/boost/python/detail/is_auto_ptr.hpp:17:40: warning: 'template<class> class std::auto_ptr' is deprecated: use 'std::unique_ptr' instead [-Wdeprecated-declarations] 17 | BOOST_PYTHON_IS_XXX_DEF(auto_ptr, std::auto_ptr, 1) | ^~~~~~~~ /usr/local/opt/boost/include/boost/detail/is_xxx.hpp:20:4: note: in definition of macro 'BOOST_DETAIL_IS_XXX_DEF' 20 | qualified_name< BOOST_PP_ENUM_PARAMS_Z(1, nargs, T) > \ | ^~~~~~~~~~~~~~ /usr/local/opt/boost/include/boost/python/detail/is_auto_ptr.hpp:17:1: note: in expansion of macro 'BOOST_PYTHON_IS_XXX_DEF' 17 | BOOST_PYTHON_IS_XXX_DEF(auto_ptr, std::auto_ptr, 1) | ^~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/local/Cellar/gcc/11.1.0_1/include/c++/11.1.0/memory:76, from /usr/local/opt/boost/include/boost/function/function_base.hpp:16, from /usr/local/opt/boost/include/boost/function/detail/prologue.hpp:17, from /usr/local/opt/boost/include/boost/function/function_template.hpp:13, from /usr/local/opt/boost/include/boost/function/detail/maybe_include.hpp:15, from /usr/local/opt/boost/include/boost/function/function0.hpp:11, from /usr/local/opt/boost/include/boost/python/errors.hpp:13, from /usr/local/opt/boost/include/boost/python/handle.hpp:11, from /usr/local/opt/boost/include/boost/python/args_fwd.hpp:10, from /usr/local/opt/boost/include/boost/python/args.hpp:10, from /usr/local/opt/boost/include/boost/python.hpp:11, from sample.cpp:1: /usr/local/Cellar/gcc/11.1.0_1/include/c++/11.1.0/bits/unique_ptr.h:57:28: note: declared here 57 | template<typename> class auto_ptr; | ^~~~~~~~ In file included from /usr/local/opt/boost/include/boost/preprocessor/iteration/detail/iter/limits/forward1_256.hpp:14, from /usr/local/opt/boost/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:1343, from /usr/local/opt/boost/include/boost/python/call_method.hpp:32, from /usr/local/opt/boost/include/boost/python.hpp:17, from sample.cpp:1: /usr/local/opt/boost/include/boost/python/call_method.hpp: In function 'typename boost::python::detail::returnable<T>::type boost::python::call_method(PyObject*, const char*, boost::type<Target>*)': /usr/local/opt/boost/include/boost/python/call_method.hpp:61:26: warning: 'PyObject* PyEval_CallMethod(PyObject*, const char*, const char*, ...)' is deprecated [-Wdeprecated-declarations] 61 | PyObject* const result = | ~~~~~~~~~~~~~~~~~^~~ 62 | PyEval_CallMethod( | ~~~~~~~~~~~~~~~~~~ 63 | self | ~~~~ 64 | , const_cast<char*>(name) | ~~~~~~~~~~~~~~~~~~~~~~~~~ 65 | , const_cast<char*>("(" BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FIXED, "O") ")") | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66 | BOOST_PP_REPEAT_1ST(N, BOOST_PYTHON_FAST_ARG_TO_PYTHON_GET, nil) | ~ In file included from /usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/include/python3.9/Python.h:154, from /usr/local/opt/boost/include/boost/python/detail/wrap_python.hpp:178, from /usr/local/opt/boost/include/boost/python/detail/prefix.hpp:13, from /usr/local/opt/boost/include/boost/python/args.hpp:8, from /usr/local/opt/boost/include/boost/python.hpp:11, from sample.cpp:1: /usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/include/python3.9/ceval.h:28:43: note: declared here 28 | Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallMethod( | ^~~~~~~~~~~~~~~~~ In file included from /usr/local/opt/boost/include/boost/smart_ptr/detail/sp_thread_sleep.hpp:22, from /usr/local/opt/boost/include/boost/smart_ptr/detail/yield_k.hpp:23, from /usr/local/opt/boost/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp:14, from /usr/local/opt/boost/include/boost/smart_ptr/detail/spinlock.hpp:42, from /usr/local/opt/boost/include/boost/smart_ptr/detail/spinlock_pool.hpp:25, from /usr/local/opt/boost/include/boost/smart_ptr/shared_ptr.hpp:29, from /usr/local/opt/boost/include/boost/shared_ptr.hpp:17, from /usr/local/opt/boost/include/boost/python/converter/shared_ptr_to_python.hpp:1, from /usr/local/opt/boost/include/boost/python/converter/arg_to_python.hpp:15, from /usr/local/opt/boost/include/boost/python/call.hpp:15, from /usr/local/opt/boost/include/boost/python/object_core.hpp:14, from /usr/local/opt/boost/include/boost/python/args.hpp:22, from /usr/local/opt/boost/include/boost/python.hpp:11, from sample.cpp:1: /usr/local/opt/boost/include/boost/bind.hpp: At global scope: /usr/local/opt/boost/include/boost/config/pragma_message.hpp:24:34: note: '#pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.' 24 | # define BOOST_PRAGMA_MESSAGE(x) _Pragma(BOOST_STRINGIZE(message(x))) | ^~~~~~~ /usr/local/opt/boost/include/boost/bind.hpp:36:1: note: in expansion of macro 'BOOST_PRAGMA_MESSAGE' 36 | BOOST_PRAGMA_MESSAGE( | ^~~~~~~~~~~~~~~~~~~~ Undefined symbols for architecture x86_64: "_PyLong_FromLong", referenced from: __ZNK5boost6python15to_python_valueIRKiEclES3_ in ccI3wkJk.o "_PyLong_Type", referenced from: __ZNK5boost6python15to_python_valueIRKiE10get_pytypeEv in ccI3wkJk.o "__Py_Dealloc", referenced from: __Py_DECREF in ccI3wkJk.o "__Py_NoneStruct", referenced from: __ZN5boost6python3api6objectC2Ev in ccI3wkJk.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status
のエラーが出ました。
補足情報
環境
MacOS Mojave 10.14.6
Python 3.9.6
gcc version 11.1.0 (Homebrew GCC 11.1.0_1)
boost-python3 1.76.0
です。お力添えいただけると幸いです。
回答2件
あなたの回答
tips
プレビュー