从Python中返回包含和运行时lib目录 [英] Return the include and runtime lib directories from within Python

查看:386
本文介绍了从Python中返回包含和运行时lib目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想从命令行使用 gcc 来编译Python的C扩展。我会构造这样的调用:

  gcc -o applesauce.pyd -IC:/ Python35 / include -LC: / Python35 / libs -l python35 applesauce.c 

我注意到 - 我, -L -l <​​/ code>选项是绝对必要的,否则您会得到一个类似这个的错误。这些命令告诉gcc在哪里查找头文件( -I ),在哪里寻找静态库( -L )以及实际使用哪个静态库( python35 ,其实际转换为 libpython35.a )。 p>

现在,显然很容易得到 libs include gcc 的程序,其他人将使用。发生此调用的行如下所示:

 来自子进程import call 
import sys
filename = applesauce.c
include_directory = os.path.join(sys.exec_prefix,'include')
libs_directory = os.path.join(sys.exec_prefix,'libs')

call(['gcc',...,'-I',include_direcory,'-L',libs_directory,...])



但是,其他平台会有不同的平台和不同的Python安装结构,因此只需加入路径并不总是有效。



相反,我需要一个来自Python内部的解决方案,它可以可靠地返回 include libs 目录。



编辑:



我查看了模块 distutils.ccompiler ,并且找到了许多有用的函数,这些函数部分使用distutils,但为了让我的编译器完全跨越平台,可以对它进行定制。唯一的一点是,我需要将它传递给包含和运行时库......



编辑2:



我查看了 distutils.sysconfig ,我能够可靠地返回包含所有头文件的'include'目录。我仍然不知道如何获得运行时库。


$ b $ < distutils.ccompiler
文档是 here


需要此功能的程序被命名为 Cyther

解决方案

如果您查看 distutils 中的 rel =nofollow> build_ext.py finalize_options 你会发现用于定位库的不同平台的代码。


Lets say I want to use gcc from the command line in order to compile a C extension of Python. I'd structure the call something like this:

gcc -o applesauce.pyd -I C:/Python35/include -L C:/Python35/libs -l python35 applesauce.c

I noticed that the -I, -L, and -l options are absolutely necessary, or else you will get an error that looks something like this. These commands tell gcc where to look for the headers (-I), where to look for the static libraries (-L), and which static library to actually use (python35, which actually translates to libpython35.a).

Now, this is obviously really easy to get the libs and include directories if its your machine, as they never change if you don't want them to. However, I was writing a program that calls gcc from the command line, that other people will be using. The line where this call occurs looks something like this:

from subprocess import call
import sys
filename = applesauce.c
include_directory = os.path.join(sys.exec_prefix, 'include')
libs_directory = os.path.join(sys.exec_prefix, 'libs')

call(['gcc', ..., '-I', include_direcory, '-L', libs_directory, ...])

However, others will have different platforms and different Python installation structures, so just joining the paths won't always work.

Instead, I need a solution from within Python that will reliably return the include and libs directories.

Edit:

I looked at the module distutils.ccompiler, and found many useful functions that would in part use distutils, but make it customizable for me to make my compiler entirely cross platform. The only thing is, I need to pass it the include and runtime libraries...

Edit 2:

I looked at distutils.sysconfig an I am able to reliably return the 'include' directory including all the header files. I still have no idea how to get the runtime library.

The distutils.ccompiler docs are here

The program that needs this functionality is named Cyther

解决方案

If you look at the source of build_ext.py from distutils in the method finalize_options you will find code for different platforms used to locate libs.

这篇关于从Python中返回包含和运行时lib目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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