如何编译.C code从用Cython用gcc [英] How to compile .c code from Cython with gcc

查看:797
本文介绍了如何编译.C code从用Cython用gcc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我已经成功地在Windows 7上安装用Cython,我尝试编译使用用Cython一些用Cython code,但GCC让我的生活努力。

  CDEF无效say_hello(名称):
    打印你好%的名字%

使用gcc来编译code抛出几十未定义的引用 -erros,我pretty确保 libpython.a 可(如安装教程说的未定义的引用的-errors是,如果这个文件丢失抛出)。

  $用Cython ctest.pyx
$ GCC ctest.c -IC:\\ Python27 \\包括

  C:\\用户\\尼克拉斯\\应用程序数据\\本地的\\ Temp \\ cckThGrF.o:ctest.c :(文字+ 0x1038):未定义的参考`_imp__PyString_FromStringAndSize
C:\\用户\\尼克拉斯\\应用程序数据\\本地的\\ Temp \\ cckThGrF.o:ctest.c :(文字+ 0x1075):未定义的参考`_imp___Py_TrueStruct
C:\\用户\\尼克拉斯\\应用程序数据\\本地的\\ Temp \\ cckThGrF.o:ctest.c :(文字+ 0x1086):未定义的参考`_imp___Py_ZeroStruct
C:\\用户\\尼克拉斯\\应用程序数据\\本地的\\ Temp \\ cckThGrF.o:ctest.c :(文字+ 0x1099):未定义的参考`_imp___Py_NoneStruct
C:\\用户\\尼克拉斯\\应用程序数据\\本地的\\ Temp \\ cckThGrF.o:ctest.c :(文字+ 0x10b8):未定义的参考`_imp__PyObject_IsTrue
C:/ Program Files文件/ MinGW的/ bin中/../ lib中/ GCC / mingw32的/ 4.5.2 /../../../ libmingw32.a(main.o中):main.c中:(文字+ 0xd2。 ):未定义的引用`WinMain函数@ 16'
collect2:劳工处返回1退出状态

奇怪的是,使用 pyximport *或设置 -script工作pretty罚款,但这既是不是很方便当一个模块上仍在工作。



如何编译使用gcc用Cython产生的 .C 文件?

或任何其他的编译器,重要的是,它会工作!


* pyximport :这是正常的,只有蟒蛇原生函数和类都包含导入的模块中,而不是CDEF函数和类?
这样的:

 #文件名:cython_test.pyx
CDEF c_foo():
    打印c_foo!
高清富():
    打印富!
    c_foo()

 进口pyximport为p; p.install()
进口cython_test
cython_test.foo()
#富!\\ nc_foo!
cython_test.c_foo()
#AttributeError的,模块对象有没有属性c_foo


更新

调用 $ GCC ctest.cC:\\ Python27 \\库\\ libpython27.a杀死在未定义的引用 -erros,但这一个:

  C:/ Program Files文件/ MinGW的/ bin中/../ lib中/ GCC / mingw32的/ 4.5.2 /../../../ libmingw32.a(主。 O):main.c中:(文字+ 0xd2):未定义的引用`WinMain函数@ 16'


解决方案

尝试:

  

GCC -c -IC:\\ Python27 \\包括-o ctest.o ctest.c
GCC -shared -LC:\\ Python27 \\库-o ctest.pyd ctest.o -lpython27

-shared 创建一个共享库。 -lpython27 通过导入库C链接:\\ Python27 \\库\\ libpython27.a

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.

这篇关于如何编译.C code从用Cython用gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆