技術(tech)

python 3.7.3のインストールでエラー「error: implicit declaration of function ‘sendfile’ is invalid in C99」

AWSのLambdaでseleniumを動かしたく、動作確認のためにpython 3.7.3をローカル環境にインストールしようとしていたところ、標記のエラーに遭遇しました。
(seleniumを動かすために相性が良いバージョンが3.7.3しか無かった…)

error: implicit declaration of function ‘sendfile’ is invalid in C99

 

ちなみにエラーの全文は以下の通り。

~/D/g ❯❯❯ pyenv install 3.7.3                                                                                         ✘ 1 main ✭ ✱
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.3.tar.xz...
-> https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
Installing Python-3.7.3...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use ncurses from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 11.7.4 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/8l/_lhkkm892750t_w85r8gcmhr0000gp/T/python-build.20231021174059.18456
Results logged to /var/folders/8l/_lhkkm892750t_w85r8gcmhr0000gp/T/python-build.20231021174059.18456.log

Last 10 log lines:
clang -c -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/yutasasaki/.pyenv/versions/3.7.3/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/yutasasaki/.pyenv/versions/3.7.3/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include  -DPy_BUILD_CORE -o Modules/gcmodule.o Modules/gcmodule.c
1 warning generated.
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/yutasasaki/.pyenv/versions/3.7.3/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/yutasasaki/.pyenv/versions/3.7.3/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include  -DPy_BUILD_CORE_BUILTIN  -DPy_BUILD_CORE -c ./Modules/posixmodule.c -o Modules/posixmodule.o
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration   -I. -I./Include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/yutasasaki/.pyenv/versions/3.7.3/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/usr/local/opt/ncurses/include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/yutasasaki/.pyenv/versions/3.7.3/include -I/usr/local/opt/zlib/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include  -DPy_BUILD_CORE_BUILTIN  -c ./Modules/errnomodule.c -o Modules/errnomodule.o
./Modules/posixmodule.c:8431:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        ret = sendfile(in, out, offset, &sbytes, &sf, flags);
              ^
1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....

 

筆者の環境は以下の通り。

  • macOS Big Sur version 11.7.4
  • pyenv: stable 2.3.31

 

結論だけ述べると、以下のコマンドを実行することで無事にインストール出来ました。

CFLAGS=”-I$(brew –prefix openssl)/include -I$(brew –prefix bzip2)/include -I$(brew –prefix readline)/include -I$(xcrun –show-sdk-path)/usr/include” LDFLAGS=”-L$(brew –prefix openssl)/lib -L$(brew –prefix readline)/lib -L$(brew –prefix zlib)/lib -L$(brew –prefix bzip2)/lib” pyenv install –patch 3.7.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

コマンドの実行結果は以下の通り。

~/D/g ❯❯❯ CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.7.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.3.tar.xz...
-> https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
Installing Python-3.7.3...
patching file Misc/NEWS.d/next/macOS/2020-06-24-13-51-57.bpo-41100.mcHdc5.rst
patching file configure
Hunk #1 succeeded at 3387 (offset -39 lines).
patching file configure.ac
Hunk #1 succeeded at 490 (offset -20 lines).
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use ncurses from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.3 to /Users/yutasasaki/.pyenv/versions/3.7.3

 

 

以下の記事を参考にしました。

 

2, 3年前にも同じことをやろうとして、同じところで詰まった気がするので、忘れないようにここに書き記しておきます。