0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

mingw32でpython3.dllを動的リンクしたいけどundefined referenceって言われるとき

Last updated at Posted at 2025-09-13

記事の安定性は保証しません。

このメッセージはpython3.dllをリンクしようとすると発生します。
代わりにpython313.dllをリンクしてください

/usr/bin/x86_64-w64-mingw32-ld: python/py-gdb-readline.o:py-gdb-readline.c:(.text+0xf): undefined reference to __imp_PyRun_SimpleStringFlags' 
/usr/bin/x86_64-w64-mingw32-ld: python/py-gdb-readline.o:py-gdb-readline.c:(.text+0x1a): undefined reference to __imp_PyOS_ReadlineFunctionPointer' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x207c): undefined reference to __imp_Py_CompileStringExFlags' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x391b): undefined reference to __imp_PyRun_InteractiveLoopFlags' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x3f7d): undefined reference to __imp_PyImport_ExtendInittab' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x4105): undefined reference to __imp_PyConfig_InitPythonConfig' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x411c): undefined reference to __imp_PyConfig_SetString' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x4135): undefined reference to __imp_PyStatus_Exception' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x417a): undefined reference to __imp_PyConfig_Read' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x41a6): undefined reference to __imp_Py_InitializeFromConfig' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x41af): undefined reference to __imp_PyConfig_Clear' 
/usr/bin/x86_64-w64-mingw32-ld: python/python.o:python.c:(.text+0x41ef): undefined reference to __imp_PyStatus_IsError' 
/usr/bin/x86_64-w64-mingw32-ld: varobj.o:varobj.c:(.text+0x19f7): undefined reference to __imp_PyRun_StringFlags' collect2:
error: ld returned 1 exit status

おまけ

/usr/bin/x86_64-w64-mingw32でpython313.dllをリンクするコードの断片(Docker Desktop)


#権限降格
RUN useradd --create-home --home /home/nonroot --shell /bin/bash nonroot
USER nonroot

#pythonフルエディオンをミラーから取得
RUN curl -L -o Python313.tar.gz "https://github.com/DaisukeDaisuke/empty/releases/download/1.0.0/Python313.tar.gz"
RUN mkdir -p /tmp/install/Python313
RUN tar xvf "Python313.tar.gz" --directory /tmp/install

# python portable runtimeをダウンロード
RUN curl -L -o python-3.13.7-embed-amd64.zip https://www.python.org/ftp/python/3.13.7/python-3.13.7-embed-amd64.zip
RUN unzip python-3.13.7-embed-amd64.zip -d /tmp/install/python-3.13.7-embed-amd64 \ 
&& rm -f python-3.13.7-embed-amd64.zip

#ディレクトリ生成
RUN cp -r /tmp/install/Python313/include/ /tmp/install/python-3.13.7-embed-amd64/include
RUN cp -r /tmp/install/Python313/libs/ /tmp/install/python-3.13.7-embed-amd64/libs

# cdする
WORKDIR /tmp/install/python-3.13.7-embed-amd64

#  libpython313.a & python313.defを生成
RUN gendef python313.dll
# インポートライブラリを期待される名前で生成
RUN x86_64-w64-mingw32-dlltool --def python313.def --dllname python313.dll --output-lib libpython313.a
# libpython313.aを生成してライブラリを lib ディレクトリへ
RUN mkdir -p /tmp/install/python-3.13.7-embed-amd64/lib && mv libpython313.a /tmp/install/python-3.13.7-embed-amd64/lib/libpython313.a

#cd /
WORKDIR /

# 2) configure が呼ぶ「python 実行」をエミュレートする shim を作成
# 必要に応じて本物のpythonにリダイレクトする(未実装)
RUN cat > /tmp/install/python_shim <<'EOF' && chmod +x /tmp/install/python_shim
#!/bin/sh
# shim: $1 == path/to/python-config.py, $2 == option
case "$2" in
  --includes)
    # ヘッダが include と include/python3.13 にある可能性をカバー
    echo "-I/tmp/install/python-3.13.7-embed-amd64/include -I/tmp/install/python-3.13.7-embed-amd64/"
    exit 0
    ;;
  --ldflags)
    # ライブラリ・パスと、configure が期待する -l 名に合わせる
    echo "-L/tmp/install/python-3.13.7-embed-amd64/lib -lpython313"
    exit 0
    ;;
  --exec-prefix)
    echo "/tmp/install/python-3.13.7-embed-amd64"
    exit 0
    ;;
  --cflags)
    echo "-I/tmp/install/python-3.13.7-embed-amd64/include -I/tmp/install/python-3.13.7-embed-amd64/"
    exit 0
    ;;
  --help|--version)
    echo "python-shim (fake)"; exit 0
    ;;
  *)
    exit 1
    ;;
esac

EOF

# 改行コードをCRLEからLEに変換
RUN sed -i 's/\r$//' /tmp/install/python_shim

#なんかいい感じにする
#ENV PKG_CONFIG_PATH=""
#ENV CPPFLAGS="-I/tmp/install/gmp/include -I/tmp/install/mpfr/include -I/tmp/install/mpc/include"
#ENV LDFLAGS="-L/tmp/install/gmp/lib -L/tmp/install/mpfr/lib -L/tmp/install/mpc/lib -static-libgcc -static-libstdc++. -Wl,--gc-sections -Wl,--allow-multiple-definition"


# configureに偽pythonと生成したlibを渡す
# --with-python=/tmp/install/python_shim \
# --with-python-libdir=/tmp/install/python-3.13.7-embed-amd64/lib \

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?