質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

4202閲覧

boost pythonのincludeファイルが見つからないことによるエラー

catsup

総合スコア1

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/08/10 16:40

編集2021/08/17 13:04

前提・実現したいこと

科学技術計算をする上で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

です。お力添えいただけると幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jbpb0

2021/08/11 02:53 編集

g++-11のオプションですが、 -lboost_python39 -I/usr/local/lib ↓ 変更 -I/usr/local/include -L/usr/local/lib -lboost_python39 としたら、どうなりますでしょうか? 見てる環境によっては、小文字のエル(l)と大文字のアイ(I)が紛らわしいですが、 -I/usr/local/include は大文字のアイ -lboost_python39 は小文字のエル です
catsup

2021/08/12 15:33

ご質問ありがとうございます。 試しましたが、上記と同様のエラーが出ました。 $ g++-11 -Wall -DPIC -shared -fPIC -o sample.so sample.cpp -I/usr/local/include -L/usr/local/lib -lboost_python39 `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.
jbpb0

2021/08/12 15:40 編集

「/usr/local/opt/boost/include」という、ディレクトリまたはシンボリックリンクはありませんか? もしあれば、そこの中に何があるか教えてください (シンボリックリンクの場合は、実体の方の中に何があるか)
catsup

2021/08/12 16:17

質問欄の方にて回答させていただきました。
jbpb0

2021/08/13 00:20 編集

> "/usr/local/opt/boost/include"以下には"boost/"と続き、その下には以下のようなファイルがありました。 そうでしたら、g++-11のオプションに -I/usr/local/opt/boost/include を追加したら、いけそうな (大文字のアイです)
guest

回答2

0

ベストアンサー

fatal error: boost/python: No such file or directory

.

"/usr/local/opt/boost/include"以下には"boost/"と続き、その下には以下のようなファイルがありました。

そうでしたら、g++-11のオプションに

sh

1-I/usr/local/opt/boost/include

または

sh

1-I/usr/local/include

を追加してみてください

.
【追記】

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

g++-11のオプションに、さらに

sh

1-lpython3.9

を追加してみてください

投稿2021/08/15 09:51

編集2021/08/23 08:16
jbpb0

総合スコア7651

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

catsup

2021/08/17 12:57

返信が遅くなってしまい申し訳ありません。 仰る通りにオプションを 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` としましたところ、今までと同じエラーになりました。 コードのinclude部分を #include <boost/python.hpp> として同じ操作をしましたところ、数秒の沈黙の後上記(試したところ欄)のようなエラーが出ました。
jbpb0

2021/08/23 08:03 編集

> コードのinclude部分を #include <boost/python.hpp> として同じ操作をしましたところ、数秒の沈黙の後上記(試したところ欄)のようなエラーが出ました。 g++-11のオプションに、さらに -lpython3.9 を追加してみてください (小文字のエルです)
jbpb0

2021/08/23 08:04 編集

あと、おそらく /usr/local/opt/boost/include/boost/ と /usr/local/include/boost/ は、実質同じだと思います もしそうなら、 -I/usr/local/opt/boost/include は、 -I/usr/local/include に変えても大丈夫です (大文字のアイです)
catsup

2021/08/25 18:46

-lpython3.9を追加したところ、無事soファイルを作ることができました。 何度もご指導いただき大変感謝しております。
guest

0

boost 2 The Boost Distributionによると、

plain

1boost_1_76_0/ .................The “boost root directory” 2 index.htm .........A copy of www.boost.org starts here 3 boost/ .........................All Boost Header files 4 `` `` 5 libs/ ............Tests, .cpps, docs, etc., by library 6 index.html ........Library documentation starts here 7 algorithm/ 8 any/ 9 array/ 10 …more libraries… 11 status/ .........................Boost-wide test suite 12 tools/ ...........Utilities, e.g. Boost.Build, quickbook, bcp 13 more/ ..........................Policy documents, etc. 14 doc/ ...............A subset of all Boost library docs 15

というディレクトリ構成になっているそうですので、

g++の-Iオプションで、boost_1_76_0の場所を指定するか、CPLUS_INCLUDE_PATHに追加するかしないと見つけられないでしょう。

投稿2021/08/11 06:34

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

catsup

2021/08/25 18:49

私の知識不足ゆえにお返事ができなくて申し訳ありませんでした。 お答えいただきありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問