去掉cpp生成的注释

2024-02-19

I use #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

那是因为在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.
   ... */

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

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

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


UPDATE: 有可能是这样pcu的回答 https://stackoverflow.com/a/51203412/827263比这个更正确。

您可以使用cpp有旗帜-nostdinc并且不使用标志-C.

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


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

来自文档 https://gcc.gnu.org/onlinedocs/cpp/:

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

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

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

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

更多信息:跑步

cpp -C /dev/null

表明版权评论来自/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"
$

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

你为什么使用-C对于非 C 代码?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

去掉cpp生成的注释 的相关文章

随机推荐