gcc可以编译python程序吗_如何编译.C code从用Cython用gcc

博主在Windows 7上安装Cython后,用gcc编译Cython代码时遇到大量未定义引用错误,虽确认libpython.a文件存在仍无法解决。使用pyximport或setup - script可行但不方便。最后给出解决方案,通过特定命令用gcc编译生成共享库并链接相关库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Now that I've successfully installed Cython on Windows 7, I try to compile some Cython code using Cython, but gcc makes my life hard.

cdef void say_hello(name):

print "Hello %s" % name

Using gcc to compile the code throws dozens of undefined reference to -erros, and I'm pretty sure the libpython.a is available (as the installation tutorial said, undefined reference to -errors are thrown if this file is missing).

$ cython ctest.pyx

$ gcc ctest.c -I"C:\Python27\include"

C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1038): undefined reference to `_imp__PyString_FromStringAndSize'

C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1075): undefined reference to `_imp___Py_TrueStruct'

C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1086): undefined reference to `_imp___Py_ZeroStruct'

C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x1099): undefined reference to `_imp___Py_NoneStruct'

C:\Users\niklas\AppData\Local\Temp\cckThGrF.o:ctest.c:(.text+0x10b8): undefined reference to `_imp__PyObject_IsTrue'

c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16'

collect2: ld returned 1 exit status

The weird thing is, using pyximport* or a setup-script works pretty fine, but it's both not very handy when still working on a module.

How to compile those .c files generated with Cython using gcc ?

or any other compiler, important is that it will work !

*pyximport: Is it normal that only python-native functions and classes are contained in the imported module and not cdef-functions and classes ?

like:

# filename: cython_test.pyx

cdef c_foo():

print "c_foo !"

def foo():

print "foo !"

c_foo()

import pyximport as p; p.install()

import cython_test

cython_test.foo()

# foo !\nc_foo !

cython_test.c_foo()

# AttributeError, module object has no attribute c_foo

UPDATE

Calling $ gcc ctest.c "C:\Python27\libs\libpython27.a" kills the undefined reference to -erros, but this one:

c:/program files/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../libmingw32.a(main.o):main.c:(.text+0xd2): undefined reference to `WinMain@16'

解决方案

Try:

gcc -c -IC:\Python27\include -o ctest.o ctest.c

gcc -shared -LC:\Python27\libs -o ctest.pyd ctest.o -lpython27

-shared creates a shared library. -lpython27 links with the import library C:\Python27\libs\libpython27.a.

python3.10 mingw64 编译pyd文件,报错D:\gitlab\cython_asn1>python setup.py running build_ext building 'v2x_decoder' extension gcc -mdll -O -Wall -I./includes -ID:\programs\python310\include -ID:\programs\python310\Include -c spdu_decoder.cpp -o build\temp.win-amd64-cpython-310\Release\spdu_decoder.o -std=c++11 -O3 -Wall -m64 -DMS_WIN64 -static-libgcc -static-libstdc++ spdu_decoder.cpp: In function 'const char* spdu_oer_asn1_decode(const unsigned char*, int, int*)': spdu_decoder.cpp:75:9: warning: unused variable 'ret' [-Wunused-variable] int ret = 0; ^~~ gcc -mdll -O -Wall -I./includes -ID:\programs\python310\include -ID:\programs\python310\Include -c v2x_decoder.cpp -o build\temp.win-amd64-cpython-310\Release\v2x_decoder.o -std=c++11 -O3 -Wall -m64 -DMS_WIN64 -static-libgcc -static-libstdc++ writing build\temp.win-amd64-cpython-310\Release\v2x_decoder.cp310-win_amd64.def g++ -O -Wall -shared -s build\temp.win-amd64-cpython-310\Release\spdu_decoder.o build\temp.win-amd64-cpython-310\Release\v2x_decoder.o build\temp.win-amd64-cpython-310\Release\v2x_decoder.cp310-win_amd64.def -LD:\programs\python310\libs -LD:\programs\python310 -LD:\programs\python310\PCbuild\amd64 -lpython310 -lucrt -lvcruntime140 -o build\lib.win-amd64-cpython-310\v2x_decoder.cp310-win_amd64.pyd -L./libs -lasncodec -m64 D:/programs/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lucrt collect2.exe: error: ld returned 1 exit status error: command 'D:\\programs\\mingw64\\bin\\g++.exe' failed with exit code 1
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值