python - Distutils 和 shell 扩展

标签 python bash distutils

我有一个 Python 扩展模块,需要 GTK。编译 GTK 程序通常需要大量链接,因为 GTK 依赖于许多其他库(Glib、Cairo、Pango 等)。因此,通常我只是使用 shell 扩展(反引号)使用 pkg-config 的输出进行编译,例如:

gcc -p-O2 -Wall myprogram.c -o myprogram `pkg-config --cflags gtk+-3.0` `pkg-config --cflag `pkg-config --libs gtk+-3.0`

但由于某种原因,当我在 distutils 中传递给 module.extra_compile_args 的参数中使用 shell 扩展时,我收到错误,因为 BASH 实际上并未扩展表达式:

module = Extension('mymodule',
        sources = ['mymodule.c'])

module.extra_compile_args = ['`pkg-config --cflags gtk+-3.0`', 
        '`pkg-config --libs gtk+-3.0`'];

这只会导致如下错误:

gcc: error: `pkg-config --cflags gtk+-3.0`: No such file or directory

那么,有什么办法可以实现这一点吗?我是否需要通过将 pkg-config 的输出作为 Python 字符串获取,然后将其添加为 module.extra_compile_args 中的元素来手动进行 shell 扩展?

最佳答案

module.extra_compile_args = [
  subprocess.check_output(["pkg-config", "--cflags", "gtk+-3.0"]),
  subprocess.check_output(["pkg-config", "--libs", "gtk+-3.0"])
];

http://docs.python.org/library/subprocess.html#subprocess.check_output

关于python - Distutils 和 shell 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11871713/

相关文章:

python - 验证字符串作为 Python 字典中的键或值存在吗?

Python pandas 更改数据框值

python - 如何使用其变更集知道分支是否已关闭?

bash - 找不到subprocess.Popen返回命令

bash - 过滤 `tree` 命令的输出

python - DistutilsOptionError : must supply either home or prefix/exec-prefix -- not both

python - 索引错误: list index out of range in Python Script

Bash <read> 打印新行,如何防止

python - 在 Anaconda 中安装 Python 模块开发版本的最佳实践是什么?

python - Homebrew 软件/Python : Convince distutils to link against a specific library on OS X?