【问题标题】:Remove the comments generated by cpp删除cpp生成的评论
【发布时间】:2014-08-14 18:07:33
【问题描述】:

我在lexer.mll中使用#include ".../frontend/tokens.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 cmets:

/* 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.
   ... */

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

有谁知道如何搭这些 cmets?

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

【问题讨论】:

    标签: gcc ocaml c-preprocessor


    【解决方案1】:

    更新pcu's answer 可能比这个更正确。

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

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


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

    来自documentation

    '-C'
    不要丢弃 cmets。所有的 cmets 都通过 输出文件,已处理指令中的 cmets 除外,它们是 与指令一起删除。

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

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

    可能有其他影响,好的或坏的。如果 -C 以前为您工作,可能是您的 OCaml 代码中的某些看起来像 C cmets 的东西是从 lexer.mlllexer_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"
    $
    

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

    您为什么将 -C 用于非 C 代码?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 在这里添加 -nostdinc 是正确的答案。仍然可以使用 -C。
      【解决方案3】:

      我最近遇到了这个问题(其实比较复杂,但是归结为这些问题),在CPP/GPP in Fortran variadic macro (plus Fortran // concatenation)中描述了它及其解决方案

      简而言之,解决方法是安装较旧版本的 cpp,我了解到无需使用以前版本的编译器即可轻松完成。

      在 ubuntu 16.04 上

      sudo apt-get install cpp-4.7
      

      然后使用

      /usr/bin/cpp-4.7 -C -P myfile.F90 > myfile.f90
      

      【讨论】:

        猜你喜欢
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-01
        • 1970-01-01
        • 2011-12-18
        相关资源
        最近更新 更多