如何对一个文件进行两阶段预处理 [英] how to do two-stage preprocessing of one file
问题描述
我要做的是让预处理器解析一个文件两次。
文件有三个部分,每个部分都依赖于前一个。
示例(文件名为foo.c):
#ifndef ONCE / *第一部分* /
#define D #define
#define ONCE
#include" foo.c"
#endif
#ifdef ONCE / * second part * /
D x int
#ifndef TWICE
#define TWICE
#include" foo.c"
#endif
#endif
#ifdef TWICE / *第三部分* /
xy;
#endif
问题是预处理器只是将第三部分添加到
第二部分,但是在这个过程中没有预处理第三部分。 />
我如何制作我的#includes和#ifedef,以便第三部分将被预处理为
?
在文章< 11 ********************* @ k79g2000hse.googl egroups。 com>,
andreyvul< an ******** @ gmail.comwrote:
>我是什么我试图做的是让预处理器解析一个文件两次。
你不能使用C预处理器定义的工具来做到这一点。
您的特定实现 - 可能 - 提供了一种方式
预处理文件。例如,在我在
时刻使用的系统上,序列看起来像是,
cc -P myfile.c
MV myfile.i myfile_preprocessed.c
CC -o输出myfile_preprocessed.c
但这也不会是少见Unix系统上序列
看起来更接近
cc -E myfile.c | egrep的-v'^#线'myfile_preprocessed.c
CC -o outfile中myfile_preprocessed.c
许多UNIX C编译器提供了一些选项,如第二个例子,
预处理并将结果发送到标准输出;它可能在您的工具链中可能没有必要,或者在您可以将文件推回之前从预处理的输出中删除#line指令
通过C解析器。
如果您使用的是其中一个Windows编译器...可能有一些类似的选项,但可能没有命名-P或-E。
-
有什么事可以说,看,这是新的吗?它已经过去了,这已经是我们面前的旧时代了。 - 传道书
块引用>
在11月12日下午7时11分,罗伯特... @ ibd.nrc-cnrc.gc.ca(沃尔特·罗伯逊)
写道:
文章< 1194910305.752743.81 ... @ k79g2000hse.googlegroups。 com>,
andreyvul< andrey .... @ gmail.comwrote:
我在尝试什么要做的是让预处理器解析一个文件两次。
你不能使用C预处理器定义的工具来做到这一点。
哦,我的意思是_recursively_预处理文件两次。
这就是为什么我问的是正确的#include / #ifdef结构,所以
它预处理如下:
#block 1
| - > #block 2(通过递归包含)
| - > #block 3(通过递归包含)
然而,它没有达到| - > #block 3
andreyvul写道:
11月12日晚上7:11,罗伯... @ ibd.nrc-cnrc.gc.ca(沃尔特罗伯逊)
写道:
>在冠词LT; 1194910305.752743.81。 .. @ k79g2000hse.googlegroups。 com>,
andreyvul< andrey .... @ gmail.comwrote:
>>我在尝试什么要做的是让预处理器解析一个文件两次。
你不能使用C预处理器定义的工具来做到这一点。
哦,我的意思是_recursively_预处理文件两次。
这就是为什么我问的是正确的#include / #ifdef结构,所以
它预处理如下:
#block 1
| - > #block 2(通过递归包含)
| - > #block 3(通过递归包含)
然而,它没有达到| - > #block 3
我没有尝试过,但这些内容可能会这样做:
#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/ *这是肉* /
#if REC_COUNT == 2
/ *第一次迭代* /
#elif REC_COUNT == 1
/ *第二次迭代* /
#else
/ *第三次和最后一次迭代* /
#如果REC_COUNT!= 0
#include __FILE__
#endif
-
方舟
What I''m trying to do is have the preprocessor parse one file twice.
The file has three parts, and each is dependent on the previous.
Example (file name is foo.c):
#ifndef ONCE /* first part */
#define D #define
#define ONCE
#include "foo.c"
#endif
#ifdef ONCE /* second part */
D x int
#ifndef TWICE
#define TWICE
#include "foo.c"
#endif
#endif
#ifdef TWICE /* third part */
x y;
#endif
The problem is that the preprocessor just adds the third part to the
second part, but doesn''t preprocess the third part in the process.
How do I make my #includes and #ifedefs so that the third part will be
preprocessed?
解决方案In article <11*********************@k79g2000hse.googlegroups. com>,
andreyvul <an********@gmail.comwrote:>What I''m trying to do is have the preprocessor parse one file twice.You cannot do that using the facilities defined by the C preprocessor.
Your particular implementation -probably- offers a way to
preprocess files. For example, on the system I am using at the
moment, the sequence would look something like,
cc -P myfile.c
mv myfile.i myfile_preprocessed.c
cc -o outputfile myfile_preprocessed.c
But it would also not be uncommon on a Unix system for the sequence
to look something closer to
cc -E myfile.c | egrep -v ''^#line'' myfile_preprocessed.c
cc -o outfile myfile_preprocessed.c
Many unix C compilers offer some option as in the second example,
to preprocess and send the result to standard output; it may
or may not be necessary in your toolchain to remove #line directives
from the preprocessed output before you can push the file back
through the C parseer.
If you are using one of the Windows compilers... there is probably
some similar option, but probably not named -P or -E .
--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes
On Nov 12, 7:11 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:In article <1194910305.752743.81...@k79g2000hse.googlegroups. com>,
andreyvul <andrey....@gmail.comwrote:What I''m trying to do is have the preprocessor parse one file twice.
You cannot do that using the facilities defined by the C preprocessor.Oh, I meant _recursively_ preprocess the file twice.
That''s why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:
#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)
however, it doesn''t get to |->#block 3
andreyvul wrote:On Nov 12, 7:11 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:>In article <1194910305.752743.81...@k79g2000hse.googlegroups. com>,
andreyvul <andrey....@gmail.comwrote:>>What I''m trying to do is have the preprocessor parse one file twice.You cannot do that using the facilities defined by the C preprocessor.
Oh, I meant _recursively_ preprocess the file twice.
That''s why I was asking about proper #include/#ifdef structure, so
that it preprocesses like this:
#block 1
|->#block 2 (via recursive include)
|->#block 3 (via recursive include)
however, it doesn''t get to |->#block 3
I didn''t try it but something along these lines might do it:
#ifndef REC_COUNT
#define REC_COUNT 2
#elif REC_COUNT == 2
#undef REC_COUNT
#define REC_COUNT 1
#elif REC_COUNT == 1
#undef REC_COUNT
#define REC_COUNT 0
#endif
/* Here is the meat */
#if REC_COUNT == 2
/* first iteration */
#elif REC_COUNT == 1
/* second iteration */
#else
/* third and last iteration */
#if REC_COUNT != 0
#include __FILE__
#endif
--
Ark
这篇关于如何对一个文件进行两阶段预处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!