在C语言中调用Python API执行脚本失败:PyRun_SimpleFile触发未定义引用链接错误
__imp_PyRun_SimpleFileExFlags When Using Python C API Hey there, let's get that linker error sorted out for your C code calling the Python API. That undefined reference error means your compiler knows what PyRun_SimpleFile is (thanks to including Python.h), but the linker can't find the actual implementation of the function in any of the libraries it's checking.
Why This Happens
PyRun_SimpleFile is actually a preprocessor macro that expands to PyRun_SimpleFileExFlags under the hood. The implementation of this function lives in Python's import library (either .lib for MSVC or .a for MinGW), and you haven't told your linker to include that library during compilation.
Step-by-Step Fixes
1. Verify Python Development Files Are Installed
Make sure your Python installation includes the development headers and libraries:
- If you used the official Python installer, ensure you checked the "Add Python to PATH" option and installed the "Debug Binaries" (if needed).
- Locate your Python installation directory (e.g.,
C:\Python39for Python 3.9). You should see anincludefolder (withPython.h) and alibsfolder (with the import library, likepython39.liborlibpython39.a).
2. Update Your Compilation Command
When compiling with GCC (MinGW), you need to:
- Point the compiler to Python's header files with
-I - Point the linker to Python's library directory with
-L - Link against the Python library with
-l(followed by the library name, e.g.,python39for Python 3.9)
Here's an example command (replace C:\Python39 with your actual Python path):
gcc pyboot.c -o pyboot.exe -IC:\Python39\include -LC:\Python39\libs -lpython39
3. Match Architecture (32-bit vs 64-bit)
Ensure your GCC version matches your Python architecture:
- You're using a 64-bit GCC (
x86_64-w64-mingw32), so you must use a 64-bit Python installation. Mixing 32-bit and 64-bit will cause linker errors or runtime crashes.
4. Tweak Your Code for Robustness
A small improvement to your code: check if the file opens successfully before passing it to the Python API, and use read-only mode (r) since you don't need to write to the script file. Also, don't forget to close the file after execution:
#define PY_SSIZE_T_CLEAN #include <stdio.h> #include <conio.h> #include "Python.h" #include "fileapi.h" #include "fileobject.h" int main(){ FILE *file = fopen("test.py", "r"); if (!file) { printf("Error: Could not open test.py\n"); return 1; } PyRun_SimpleFile(file, "test.py"); fclose(file); return 0; }
Final Notes
If you still run into issues, double-check that the library name matches your Python version (e.g., python310 for Python 3.10) and that the paths to include and libs are correct. Sometimes MinGW might need the .a version of the library—if the libs folder only has .lib files, you can generate the .a file using gendef and dlltool tools that come with MinGW, but usually the official Python installer includes the necessary files for MinGW these days.
内容的提问来源于stack exchange,提问作者SecurityRaven




