やろうとしてること
https://qiita.com/kawamou/items/4235e37441e22205de34
このサイトの,「GoからPythonをよぶ場合」というのを実行しようとしています.
環境
macOS Big Sur ver. 11.5.1(20G80) -> 12.6 (21G115)
MacBook Air M1, 2020
メモリ 8GB
Homebreq ver.3.6.3(git revision 86ac48865b7; last commit 2022-10-02)
Python 3.10.6
pip 22.2.2 from /opt/homebrew/lib/python3.10/site-packages/pip (python 3.10)
pyaudio-0.2.12
問題
完成コードが示されているのですが,確認してみたところ,自分のとこのコードではディレクトリ構成がことなっているので ,cgoの部分を変更する必要がありました.
CFLAGSのpythonの場所は,
/Library/Developer/CommandLineTools/usr/bin
にあり,LDFLAGSのlibは
/Users/usrname/Library/Python/3.9/lib
にあったのでコードを
main.go
Go
1package main 2 3// #cgo CFLAGS: -I/Library/Developer/CommandLineTools/usr/bin/python3.9 4// #cgo LDFLAGS: -L/Users/usrname/Library/Python/3.9/lib python 5// #include <Python.h> 6import "C" 7 8func main() { 9 // 最後にPythonインタプリタ終了 10 defer C.Py_Finalize() 11 // Pythonインタプリタの初期化 12 C.Py_Initialize() 13 // GoのstringをCのcharに型変換(変換しないとPyRun_SimpleStringに型合ってないよって怒られる) 14 // cannot use "print(\"Hello, World!\")" (type string) as type *_Ctype_char in argument to _Cfunc_PyRun_SimpleString 15 pyCodeStr := `print("Hello, World!")` 16 pyCodeChar := C.CString(pyCodeStr) 17 // Pythonコードを文字列として受け取ってインタプリタ上で実行 18 C.PyRun_SimpleString(pyCodeChar) 19}
のようにしました.
この状態でビルドを行うと
MacBook-Air sound_wav % go build -o main go build sound_wav: invalid flag in #cgo LDFLAGS: python
と言われたので,調べて
https://stackoverflow.com/questions/62580446/invalid-flag-in-cgo-ldflags
このサイトに辿り着き,元の完成コードにもあった"-l"をLDFLAGSの方につけなくてはいけないのかもしれないとおもい.
// #cgo CFLAGS: -I/Library/Developer/CommandLineTools/usr/bin/python3.9 // #cgo LDFLAGS: -L/Users/usrname/Library/Python/3.9/lib -lpython // #include <Python.h>
と修正し,再度ビルドすると
MacBook-Air sound_wav % go build -o main # sound_wav ./main.go:5:11: fatal error: 'Python.h' file not found #include <Python.h> ^~~~~~~~~~ 1 error generated.
と出力されました.
これもまた調べると,
https://mio.yokohama/?p=1235
このサイトが出てきたので,書いてあるように,verが3.10.6だったので,それに対応するのをPythonのサイトからGzipped source tarballで持ってきて,そこにあるIncludemをLDFLAGSのPATHにあるpythonの下に入れました.
しかし,その後ビルドしても,Python.hが見つからないと言われ,それでは強硬策をとおもい,main.goと同じディレクトリにIncludeディレクトリをコピーして.
// #include <Include/Python.h>
としたのですが,
MacBook-Air sound_wav % go build -o main # sound_wav In file included from ./main.go:5: ./Include/Python.h:8:10: fatal error: 'pyconfig.h' file not found #include "pyconfig.h" ^~~~~~~~~~~~ 1 error generated.
と,pyconfig.hがないと言われました.
実際にincludeディレクトリにはこのファイルはなかったので.
https://svn.python.org/projects/python/trunk/PC/pyconfig.h
このサイトのをコピペしてincludeディレクトリにおくと,
#include <io.h>
が使えないと出ていたので,コメントアウト(拡張機能によるエラーの通知はなかった)し,再度ビルドしたのですが.
MacBook-Air sound_wav % go build -o main # sound_wav In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:73:9: warning: 'PY_UINT32_T' macro redefined [-Wmacro-redefined] #define PY_UINT32_T uint32_t ^ ./Include/pyconfig.h:402:9: note: previous definition is here #define PY_UINT32_T unsigned int ^ In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:74:9: warning: 'PY_UINT64_T' macro redefined [-Wmacro-redefined] #define PY_UINT64_T uint64_t ^ ./Include/pyconfig.h:412:9: note: previous definition is here #define PY_UINT64_T unsigned PY_LONG_LONG ^ In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:77:9: warning: 'PY_INT32_T' macro redefined [-Wmacro-redefined] #define PY_INT32_T int32_t ^ ./Include/pyconfig.h:419:9: note: previous definition is here #define PY_INT32_T int ^ In file included from ./main.go:5: In file included from ./Include/Python.h:50: ./Include/pyport.h:78:9: warning: 'PY_INT64_T' macro redefined [-Wmacro-redefined] #define PY_INT64_T int64_t ^ ./Include/pyconfig.h:429:9: note: previous definition is here #define PY_INT64_T PY_LONG_LONG ^ In file included from ./main.go:5: In file included from ./Include/Python.h:83: ./Include/unicodeobject.h:68:2: error: Must define SIZEOF_WCHAR_T #error Must define SIZEOF_WCHAR_T ^ ./Include/unicodeobject.h:71:9: warning: 'Py_UNICODE_SIZE' macro redefined [-Wmacro-redefined] #define Py_UNICODE_SIZE SIZEOF_WCHAR_T ^ ./Include/pyconfig.h:560:9: note: previous definition is here #define Py_UNICODE_SIZE 2 ^ 5 warnings and 1 error generated.
となってしまいました.
追記へ続く
結局,どこで何を入れれば成功するのでしょうか.
ご教授よろしくお願いいたします.
これ以前の経過も書き記したnoteを以下にリンクしておきます.
https://note.com/clean_camel994/n/nd573d3211451
追記
MacBook-Air ~ % python3 -c "import sys, pprint; pprint.pprint(sys.path)" ['', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/opt/homebrew/lib/python3.10/site-packages']
となったので,これの真ん中を辿って
MacBook-Air / % cd opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10 MacBook-Air 3.10 % ls Headers Resources bin lib Python _CodeSignature include share MacBook-Air 3.10 % cd include MacBook-Air include % ls python3.10 MacBook-Air include % cd python3.10 MacBook-Air python3.10 % ls Python.h genobject.h pyhash.h
となったので,
// #cgo CFLAGS: -I/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/python3.10 // #cgo LDFLAGS: -L/Users/sonoyamayuto/Library/Python/3.9/lib -lpython // #include <Python.h>
としたら先ほどのエラーと変わり,
MacBook-Air sound_wav % go build -o main # sound_wav ld: library not found for -lpython clang: error: linker command failed with exit code 1 (use -v to see invocation)
となりました.
追記2
LDFLAGSの方も修正して
// #cgo CFLAGS: -I/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/include/python3.10 // #cgo LDFLAGS: -L/opt/homebrew/Cellar/python@3.10/3.10.6_2/Frameworks/Python.framework/Versions/3.10/lib -lpython3.10 // #include <Python.h>
のようにしたらエラー内容が変わりました.
エラー内容.
MacBook-Air sound_wav % go build -o main # sound_wav Undefined symbols for architecture arm64: "_Pa_CloseStream", referenced from: _main in _x003.o "_Pa_GetDefaultOutputDevice", referenced from: _main in _x003.o "_Pa_GetDeviceInfo", referenced from: _main in _x003.o "_Pa_Initialize", referenced from: _main in _x003.o "_Pa_OpenStream", referenced from: _main in _x003.o "_Pa_StartStream", referenced from: _main in _x003.o "_Pa_StopStream", referenced from: _main in _x003.o "_Pa_Terminate", referenced from: _main in _x003.o ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/08 12:17 編集
2022/10/08 12:13
2022/10/08 12:33
2022/10/08 12:37
2022/10/08 12:52
2022/10/08 12:59 編集
2022/10/09 12:11
2022/10/09 13:08
2022/10/10 11:02