去掉cpp生成的注释

Sof*_*mur 5 gcc ocaml c-preprocessor

我使用#include ".../frontend/tokens.mll"in lexer.mll,然后cpp -C -P frontend/lexer.mll -o frontend/lexer_new.mll生成lexer_new.mll.

这一直有效,直到我昨天将我的 ubuntu 从 12.04 升级到 14.04。

编译报错:

ocamllex frontend/lexer_new.mll
File "frontend/lexer_new.mll", line 1, character 1: illegal character /.
make: *** [frontend/lexer_new.ml] Error 3
Run Code Online (Sandbox Code Playgroud)

那是因为在lexer_new.mll开始处插入了几行 C 注释:

/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   ... */
Run Code Online (Sandbox Code Playgroud)

我不记得在升级之前是否生成了相同的评论。

有谁知道如何摆脱这些评论?

PS:gcc 版本是: gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

Kei*_*son 3

更新: pcu 的答案可能比这个更正确。

您可以cpp与 flag 一起使用-nostdinc,但不要使用 flag -C

我会把这个答案留在这里。


省略该-C选项似乎会禁止插入版权信息。

文档中:

'-C'
不要丢弃注释。所有注释都会传递到输出文件,但已处理指令中的注释除外,这些注释会与指令一起删除。

使用“-C”时,您应该做好副作用的准备;它导致预处理器将注释视为其本身的标记。例如,出现在指令行开头的注释具有将该行转换为普通源代码行的效果,因为该行上的第一个标记不再是“#”。

默认情况下,源代码中的注释会被丢弃。该-C选项使它们被传递。显然在最近的版本中它还插入了版权信息。

可能会产生其他影响,无论好坏。如果-C之前为您工作,可能是您的 OCaml 代码中一些看起来像 C 注释的东西正在从 lexer.mllto传递lexer_new.mll;省略-C将导致它们被删除。如果这是一个问题,您可能需要保留该-C选项并在预处理器之后添加一个过滤器,以删除添加的注释。(编写这样的过滤器留作练习。)

更多信息:跑步

cpp -C /dev/null
Run Code Online (Sandbox Code Playgroud)

表明版权评论来自/usr/include/stdc-predef.h

$ cpp -C /dev/null
# 1 "/dev/null"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
/* Copyright (C) 1991-2014 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
[39 lines deleted]
# 1 "/dev/null"
$
Run Code Online (Sandbox Code Playgroud)

(该-P选项禁止#指示文本来源的指令。)显然,在预处理 C 源代码时,默认情况下包含该文件。它定义了一些特定于 C 的预定义宏,例如__STDC_IEC_559____STDC_ISO_10646__

为什么使用-C非 C 代码?