在python中访问autoconf定义的符号
我正在开发一个用 C++ 和 python 编写的项目。我的configure.ac中有以下行:
AC_INIT(MILHOUSE, 0.3.6)
这意味着在运行configure生成的config.h中,我有以下定义行:
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.3.6"
我只是想知道是否有一个现有的模块用于解析这样的配置符号或在至少有一种在 python 中访问这些定义的标准方法。
I'm working on a project that is written in both C++ and python. I have the following line in my configure.ac:
AC_INIT(MILHOUSE, 0.3.6)
which means that in the config.h generated by running configure, i have the following define line:
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.3.6"
I just wanted to know if there was an existing module for parsing configure symbols like this or at least a standard way of accessing these defines in python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AC_INIT
不仅定义了预处理器符号,还定义了输出变量。当您列出一个文件时,我们将其称为somefile
,在您的AC_CONFIG_FILES
宏中,您的configure
脚本会查找名为somefile 的文件。 in
,并将@符号之间的任何输出变量的名称替换为其值,调用结果somefile
。因此,要在 Python 文件
somescript.py
中访问这些定义,请在configure.ac
中添加如下内容:然后将您的 Python 文件命名为
somescript.py。 in
并访问 PACKAGE_VERSION 输出变量,如下所示:三引号可能是明智的,因为您永远不知道输出变量何时可能包含引号。
AC_INIT
not only defines preprocessor symbols, it also defines output variables. When you list a file, let's call itsomefile
, in yourAC_CONFIG_FILES
macro, yourconfigure
script looks for a file calledsomefile.in
, and replaces the names of any output variables between @-signs with their values, calling the resultsomefile
.So, to access these definitions in a Python file
somescript.py
, put something like this in yourconfigure.ac
:Then name your Python file
somescript.py.in
and access the PACKAGE_VERSION output variable like this:The triple quotes are probably wise, because you never know when an output variable might contain a quote.
pyparsing wiki 的示例页面包括 此宏扩展器示例。以下是它处理的示例代码:
因此它还将处理根据其他宏定义的宏。将“#def”更改为“#define”应该不难。
The examples page of the pyparsing wiki includes this example of a macro expander. Here is the sample code that it processes:
So it will also handle macros that are defined in terms of other macros. Should not be difficult to change '#def' to '#define'.
添加到接受的答案:如果您对自定义定义的变量感兴趣,请务必使用
AC_SUBST 除了
AC_DEFINE[_UNQUOTED]
以外,您的配置文件中不会替换任何内容。使用这个其他答案中的提示,我将其添加到我的configure.ac
中,因此在< code>config.h 我得到
并且
config.py.in
从转换为
config.py
Adding to the accepted answer: if you are interested in a custom defined variable, be sure to use
AC_SUBST
in addition toAC_DEFINE[_UNQUOTED]
else nothing is replaced in your config files. Using the hints from this other answer, I added this to myconfigure.ac
so in
config.h
I getand
config.py.in
is converted fromto
config.py