运行GCC预处理程序非C文件 [英] Run GCC preprocessor non-C files

查看:111
本文介绍了运行GCC预处理程序非C文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用专有的开发环境,该环境可以编译用 C 编写的代码以及IEC 61131语言.对于 C 编译,它使用带有以下构建选项的GCC 4.1.2:

I'm using a proprietary development environment that compiles code written in C, as well as the IEC 61131 languages. For the C compilation, it uses GCC 4.1.2 with these build options:

-fPIC -O0 -g -nostartfiles -Wall -trigraphs -fno-asm

该编译是通过使用Cygwin在Windows上运行的程序完成的.

The compilation is done by a program running on windows utilizing Cygwin.

我的问题是,IEC语言预处理器没什么用(根本不支持#define),我想使用宏!我不知道为什么GCC预处理器会真正关心它正在处理的语言(我的目标语言是结构化文本),所以我在寻找是否有人知道一种获取它的方式来处理不同文件类型的文件然后不再进行进一步的编译(我只是在通过IEC编译器运行文件之前寻找宏扩展).我对编译器选项和环境一无所知,因为我从来不需要处理它们,我只写了 C 代码,它神奇地编译并转移到我的目标系统上运行.

My issue is, IEC language preprocessor is not that useful (doesn't support #define at all) and I want to use macros! I don't see why the GCC preprocessor would really care what language it is processing (my target language is Structured Text), so I'm looking to see if anyone might know a way to get it to process files of different file types that then are not compiled further (I'm just looking for macro expansion before the file is run through the IEC compiler). I'm very ignorant of compiler options and environments since I've never had to deal with them, I just write C code and it magically compiles and transfers to my target system to run.

我唯一能做的就是添加构建选项并在执行任何操作之前执行一个批处理文件.我认为我的最大希望在于使用批处理文件来处理某个扩展名的所有文件,但我什至不知道要使用gnuinst文件夹中的哪个可执行文件,更不用说要使用哪些标志来运行这些文件了. >

The only things I can really do are add build options and execute a batch file before anything is executed. I think my best hope lies in using a batch file to process all files of a certain extension, but I don't even know what executable in the gnuinst folder to use, let alone what flags to use to run through the files.

推荐答案

关于任何C预处理器,包括gcc的cpp,都将假定其输入是有效的C代码.它必须遵循C(或C ++或Objective-C)规则对输入进行标记化,因为它必须将其输入解析为标记(更准确地说是预处理标记).令牌级别以上的构造应该不是问题.

Just about any C preprocessor, including gcc's cpp, is going to assume that its input is valid C code. It has to tokenize the input following C (or C++, or Objective-C) rules, because it had to resolve its input into tokens (more precisely preprocessing tokens). Constructs above the token level shouldn't be an issue.

您当然可以使用cppgcc -E预处理不是C源代码的文本,但是某些输入构造会导致问题.

You certainly can use cpp or gcc -E to preprocess text that isn't C source code, but some input constructs will cause problems.

以评论为例:

$ cat foo.txt
#define ADDTHEM(x, y) ((x) + (y))
ADDTHEM(2, 3)
$ gcc -E - < foo.txt
# 1 "<stdin>"
# 1 "<command-line>"
# 1 "<stdin>"

((2) + (3))

请注意,我必须使用gcc -E - < foo.txt而不是gcc -E foo.txt,因为默认情况下,gcc将.txt文件视为链接器输入文件.

Note that I had to use gcc -E - < foo.txt rather than gcc -E foo.txt, because gcc treats a .txt file as a linker input file by default.

但是,如果您向foo.txt添加了一些不包含有效C预处理程序令牌的内容,则可能会出现问题:

But if you add some content to foo.txt that doesn't consist of valid C preprocessor tokens, you can have problems:

$ cat foo.txt 
#define ADDTHEM(x, y) ((x) + (y))
ADDTHEM(2, 3)
ADDTHEM('c, "s)
$ gcc -E - < foo.txt
# 1 "<stdin>"
# 1 "<command-line>"
# 1 "<stdin>"

((2) + (3))
<stdin>:3:9: warning: missing terminating ' character [enabled by default]
<stdin>:3:0: error: unterminated argument list invoking macro "ADDTHEM"
ADDTHEM

(由于Ada将隔离的撇号'字符用作其属性语法,因此尝试将Ada源代码提供给C预处理程序时遇到了这种问题.)

(Attempts to feed Ada source code to a C preprocessor have run into this kind of problem, since Ada uses isolated apostrophe ' characters for its attribute syntax.)

因此,如果输入语言没有使用不是有效的C预处理器标记的东西,您就可以做到 .

So you can do it if the input language doesn't use things that aren't valid C preprocessor tokens.

请参见 N1570草案 C标准的6.4节,以获取有关预处理令牌的更多信息.

See the N1570 draft of the C standard, section 6.4, for more information about preprocessing tokens.

在检查 GNU cpp手册之前,我实际上写了以上内容,其中说:

I actually wrote the above before I checked the GNU cpp manual, which says:

C预处理程序仅可用于C,C ++和 Objective-C源代码.过去,它已被普遍滥用 文字处理器.它将在不遵守C的词法的输入上窒息 规则.例如,撇号将被解释为 字符常量,并导致错误.而且,你不能依靠它 保留对输入不重要的输入特性 C系列语言.如果对Makefile进行了预处理,则所有硬选项卡 将被删除,Makefile将不起作用.

The C preprocessor is intended to be used only with C, C++, and Objective-C source code. In the past, it has been abused as a general text processor. It will choke on input which does not obey C's lexical rules. For example, apostrophes will be interpreted as the beginning of character constants, and cause errors. Also, you cannot rely on it preserving characteristics of the input which are not significant to C-family languages. If a Makefile is preprocessed, all the hard tabs will be removed, and the Makefile will not work.

话虽如此,您通常可以在事情上使用cpp 并非C.其他使用Algol的编程语言通常是安全的 (Pascal,Ada等.)装配时也要格外小心. `-traditional-cpp' 模式保留更多空白,否则允许更多.许多 通过编写C或C ++样式注释可以避免这些问题 而不是使用本地语言注释,并保持宏简单.

Having said that, you can often get away with using cpp on things which are not C. Other Algol-ish programming languages are often safe (Pascal, Ada, etc.) So is assembly, with caution. `-traditional-cpp' mode preserves more white space, and is otherwise more permissive. Many of the problems can be avoided by writing C or C++ style comments instead of native language comments, and keeping macros simple.

在任何可能的情况下,您都应使用适合于 您正在使用的语言.现代版本的GNU汇编器具有 宏观设施.大多数高级编程语言都有自己的 有条件的编译和包含机制.如果其他所有方法都失败, 尝试使用真正的通用文本处理器,例如GNU M4.

Wherever possible, you should use a preprocessor geared to the language you are writing in. Modern versions of the GNU assembler have macro facilities. Most high level programming languages have their own conditional compilation and inclusion mechanism. If all else fails, try a true general text processor, such as GNU M4.

(该手册的作者显然错过了Ada属性语法的问题.)

(The authors of that manual apparently missed the problem with Ada's attribute syntax.)

这篇关于运行GCC预处理程序非C文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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