CC for python distutils 构建c

Posted

技术标签:

【中文标题】CC for python distutils 构建c【英文标题】:CC for python distutils to build c 【发布时间】:2018-02-12 10:29:57 【问题描述】:

我试图通过 python distutils 构建 c。我想用 gcc 替换 CC 并关注这个page

CC=gcc python setup.py build

然后我得到了

gcc -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat - Werror=format-security -fPIC -I/usr/include/python2.7 -c hello.c -o build/temp.linux-x86_64-2.7/hello.o

创建 build/lib.linux-x86_64-2.7

x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-严格别名 -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl ,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/hello.o -o build/lib.linux- x86_64-2.7/hello.so

第一阶段CC只改成gcc,会变成原来的x86_64-linux-gnu-gcc,不知道有没有遗漏,谢谢。

【问题讨论】:

用LD定义链接器怎么样? 没用,不过我找到了答案,是需要定义LDSHARED 【参考方案1】:

你必须用

设置“CC”

os.environ["CC"] = "GCC"

但是,这有时在 Windows 中不起作用,并且涉及到配置文件中的一些更改。

    创建文件“C:\Python27\Lib\distutils\distutils.cfg”并将其写入其中

然后插入以下代码:

[build] compiler = gcc

    从文件“C:\Python27\Lib\distutils\cygwinccompiler.py”中删除“-mno-cygwin”gcc 选项的所有实例:

    在同一个文件中转换这个:

    self.set_executables(compiler='gcc -mno-cygwin -O -Wall', compiler_so='gcc -mno-cygwin -mdll -O -Wall', compiler_cxx='g++ -mno-cygwin -O -Wall', linker_exe='gcc -mno-cygwin', linker_so='%s -mno-cygwin %s %s' % (self.linker_dll, shared_option, entry_point))

    到这里:

    self.set_executables(compiler='gcc -O -Wall', compiler_so='gcc -mdll -O -Wall', compiler_cxx='g++ -O -Wall', linker_exe='gcc', linker_so='%s %s %s' % (self.linker_dll, shared_option, entry_point))

【讨论】:

【参考方案2】:

我找到了答案here,LDSHARED 是我需要的变量。

完整的命令是这样的

CC=gcc LDSHARED=gcc -pthread -shared python setup.py build

【讨论】:

以上是关于CC for python distutils 构建c的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 distutils 安装 Python 扩展模块?

使 Python 中的 distutils 自动查找包

使用 distutils / setuptools 执行 Python 脚本安装后

使用 distutils / setuptools 执行 Python 脚本安装后

python distutils 基本打包与发布

python错误:distutils.errors.DistutilsError 解决方法