在微信公众号文章中看到它,一个c文件就能解析python,感到很神奇,到github存储库https://github.com/pocketpy/pocketpy,下载了2.1.8版头文件(https://github.com/pocketpy/pocketpy/releases/download/v2.1.8/pocketpy.h)和源代码(https://github.com/pocketpy/pocketpy/releases/download/v2.1.8/pocketpy.h), 以及2.1.6版CLI的源码(https://github.com/pocketpy/pocketpy/releases/download/v2.1.6/main.c)。

主页上说windows只支持msvc, 我先试试mingw会怎么样

C:\d>ss

C:\d>set path=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Users\lt\AppData\Local\Programs\Python\Python313\Scripts\;C:\Users\lt\AppData\Local\Programs\Python\Python313\;C:\Users\lt\AppData\Local\Microsoft\WindowsApps;;C:\Users\lt\AppData\Local\Programs\Ollama;C:\Users\lt\.dotnet\tools;C:\d\mingw64\bin;C:\d\pypy


C:\d>gcc mainpo.c -o mainpo.exe
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x1e7): undefined reference to `py_initialize'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x1f5): undefined reference to `py_sys_setargv'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x29f): undefined reference to `py_replinput'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x2f4): undefined reference to `py_peek'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x31d): undefined reference to `py_exec'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x32d): undefined reference to `py_printexc'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x339): undefined reference to `py_clearexc'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x349): undefined reference to `py_profiler_begin'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x363): undefined reference to `py_debugger_waitforattach'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x39a): undefined reference to `py_exec'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x3a6): undefined reference to `py_printexc'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x3b1): undefined reference to `py_profiler_report'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x412): undefined reference to `py_checkexc'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x41d): undefined reference to `py_finalize'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccgGXndD.o:mainpo.c:(.text+0x42d): undefined reference to `py_debugger_exit'
collect2.exe: error: ld returned 1 exit status

C:\d>gcc mainpo.c pocketpy.c -o mainpo.exe
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x181fb): undefined reference to `__imp_WSAStartup'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x18246): undefined reference to `__imp_socket'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x18278): undefined reference to `__imp_htons'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x1829c): undefined reference to `__imp_inet_pton'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x182b6): undefined reference to `__imp_bind'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x182ef): undefined reference to `__imp_listen'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x18330): undefined reference to `__imp_accept'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x18361): undefined reference to `__imp_inet_ntop'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x1837a): undefined reference to `__imp_ntohs'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x183b7): undefined reference to `__imp_htons'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x183db): undefined reference to `__imp_inet_pton'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x183f5): undefined reference to `__imp_connect'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x18440): undefined reference to `__imp_recv'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x1847a): undefined reference to `__imp_send'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x1849c): undefined reference to `__imp_closesocket'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x184da): undefined reference to `__imp_ioctlsocket'
C:/d/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/14.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\lt\AppData\Local\Temp\ccWiTR5n.o:pocketpy.c:(.text+0x184fe): undefined reference to `__imp_WSAGetLastError'
collect2.exe: error: ld returned 1 exit status

一开始漏写pocketpy.c,报了一堆链接错误,后来写了,还是一堆链接错误,只不过引用的函数变了。
在网上搜索答案。

解决 __imp_WSAStartup 未定义引用错误及 WSAStartup …

在 Windows 下进行 Socket 编程 时,如果编译时报错 undefined reference to ‘__imp_WSAStartup’,通常是因为没有正确链接 Winsock 库。 例如:

(.text+0x24): undefined reference to `__imp_WSAStartup'
collect2.exe: error: ld returned 1 exit status

这个错误意味着编译器找不到 WSAStartup 的实现,需要显式链接 ws2_32.lib 或 -lwsock32。

常见解决方法

示例:使用 #pragma comment 链接库

#include <winsock2.h>
#pragma comment(lib, “ws2_32.lib”)

int main() {
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
WSACleanup();
return 0;
}

这种方式适合 Visual Studio 等支持 #pragma comment 的环境。

命令行编译方式

gcc main.c -lwsock32

通过 -lwsock32 参数手动链接 Winsock 库。

IDE 工程设置 在 Dev-C++、VS 等 IDE 中,将 ws2_32.lib 添加到项目的链接器输入项。

WSAStartup 函数简介

WSAStartup 用于初始化 Winsock DLL,是调用任何其他 Winsock API 前必须执行的步骤。 调用格式:

int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);

其中 wVersionRequested 通常使用 MAKEWORD(2, 2) 请求 Winsock 2.2 版本,lpWSAData 用于接收实现信息。成功返回 0,失败返回错误码。

完整示例

#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, “ws2_32.lib”)

int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf(“WSAStartup failed\n”);
return 1;
}
printf(“Winsock initialized\n”);
WSACleanup();
return 0;
}

在使用完网络功能后,必须调用 WSACleanup 来释放资源,否则可能导致内存泄漏或句柄未释放。

查看mingw的目录中不存在ws2_32.lib,只能放弃在windows中编译。改用wsl的gcc容器。

C:\d>wsl
root@DESKTOP-59T6U68:/mnt/c/d# docker start gcc
gcc
root@DESKTOP-59T6U68:/mnt/c/d# docker exec -it gcc bash
root@6ae32a5ffcde:/# cd /par
root@6ae32a5ffcde:/par# gcc mainpo.c pocketpy.c -o mainpo
root@6ae32a5ffcde:/par# ./mainpo
pocketpy 2.1.8 (May  4 2026, 10:52:43) [64 bit] on linux (DEBUG)
https://github.com/pocketpy/pocketpy
Type "exit()" to exit.
>>> 1+1
2
>>> def a(x):return 2*x
>>> print(a(2))
4
>>>
root@6ae32a5ffcde:/par# gcc mainpo.c pocketpy.c -o mainpo -O3
root@6ae32a5ffcde:/par# time ./mainpo primes.py

real    0m4.124s
user    0m3.129s
sys     0m0.523s
root@6ae32a5ffcde:/par# time python3 primes.py

real    0m2.225s
user    0m2.009s
sys     0m0.228
root@6ae32a5ffcde:/par# python3 --version
Python 3.11.2

root@6ae32a5ffcde:/par# gcc mainpo1.c pocketpy.c -o mainpo1 -O3
root@6ae32a5ffcde:/par# ./mainpo1
pocketpy 2.1.8 (May  4 2026, 11:05:58) [64 bit] on linux
https://github.com/pocketpy/pocketpy
Type "exit()" to exit.
>>>
root@6ae32a5ffcde:/par# time ./mainpo1 primes.py

real    0m3.454s
user    0m3.039s
sys     0m0.474s

编译很顺利,交互界面有模有样的。用源代码benchmark目录中的primes.py脚本测试。默认参数编译出的是DEBUG版本,主页也说性能较差,在main.c加了#define NDEBUG, 重新编译,就没有 (DEBUG)标记了,但是效率还是不如cpython 3.11。

root@6ae32a5ffcde:/par# time lua-5.4.8/src/lua primes.lua

real    0m1.619s
user    0m1.306s
sys     0m0.317s

也不如lua 5.4.8。

后记:

参考 https://blog.csdn.net/qq570437459/article/details/118488957 ,mingw用如下命令行可以编译成功

gcc mainpo1.c pocketpy.c -o mainpo1.exe --std=c11 -Lc:\d\gcc161\mingw64\x86_64-w64-mingw32\lib -lws2_32

但是,不同crt版本的mingw编译器行为不同

https://github.com/brechtsanders/winlibs_mingw/releases/download/16.1.0posix-14.0.0-msvcrt-r1/winlibs-x86_64-posix-seh-gcc-16.1.0-mingw-w64msvcrt-14.0.0-r1.7z

编译成功且能执行,但效率不高

gcc mainpo1.c pocketpy.c -o mainpo218.exe --std=c11 -Lc:\d\gcc161\mingw64\x86_64-w64-mingw32\lib -lws2_32 -O3

mainpo218
pocketpy 2.1.8 (May  5 2026, 12:48:09) [64 bit] on win32
https://github.com/pocketpy/pocketpy
Type "exit()" to exit.
>>> exit()
C:\d>timer64 mainpo218 primes.py


Kernel  Time =     0.125 =    1%
User    Time =     9.265 =   98%
Process Time =     9.390 =   99%    Virtual  Memory =    517 MB
Global  Time =     9.445 =  100%    Physical Memory =    486 MB

C:\d>timer64 python311 primes.py


Kernel  Time =     0.078 =    3%
User    Time =     2.156 =   94%
Process Time =     2.234 =   97%    Virtual  Memory =    191 MB
Global  Time =     2.293 =  100%    Physical Memory =    196 MB

https://github.com/brechtsanders/winlibs_mingw/releases/download/14.2.0posix-12.0.0-ucrt-r3/winlibs-x86_64-posix-seh-gcc-14.2.0-llvm-19.1.7-mingw-w64ucrt-12.0.0-r3.7z

虽然编译出了exe文件,但执行立刻退出了。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐