Remove the comments generated by cpp

做~自己de王妃 提交于 2019-12-07 10:23:27

问题


I use #include ".../frontend/tokens.mll" in lexer.mll, and then cpp -C -P frontend/lexer.mll -o frontend/lexer_new.mll to generate lexer_new.mll.

That worked until I upgraded my ubuntu from 12.04 to 14.04 yesterday.

The compilation gives an error:

ocamllex frontend/lexer_new.mll
File "frontend/lexer_new.mll", line 1, character 1: illegal character /.
make: *** [frontend/lexer_new.ml] Error 3

That is because in lexer_new.mll several lines of C comments have been inserted in the beginning:

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

I don't remember if same comments were generated before upgrading.

Does anyone know how to get ride of these comments?

PS: the gcc version is : gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)


回答1:


Omitting the -C option appears to inhibit the inserted copyright message.

From the documentation:

'-C'
Do not discard comments. All comments are passed through to the output file, except for comments in processed directives, which are deleted along with the directive.

You should be prepared for side effects when using '-C'; it causes the preprocessor to treat comments as tokens in their own right. For example, comments appearing at the start of what would be a directive line have the effect of turning that line into an ordinary source line, since the first token on the line is no longer a '#'.

Comments in the source code are discarded by default. The -C option causes them to be passed through. Apparently in recent versions it also inserts that copyright message.

This might have other effects, good or bad. If -C was working for you before, it may be that some things that looked like C comments in your OCaml code were being passed through from lexer.mll to lexer_new.mll; omitting -C would cause them to be removed. If that's an issue, you might want to keep the -C option and add a filter after the preprocessor that removes the added comment. (Writing such a filter is left as an exercise.)

More information: running

cpp -C /dev/null

indicates that the copyright comment comes from /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 option was inhibiting the # directives that indicate where the text came from.) Apparently that file is included by default when preprocessing C source. It defines a few C-specific predefined macros such as __STDC_IEC_559__ and __STDC_ISO_10646__.

Why were you using -C for non-C code?




回答2:


I ran into this problem recently (it was actually more complex, but boiled down to these issues), and described it and its solution in CPP/GPP in Fortran variadic macro (plus Fortran // concatenation)

In short, the fix was to install an older version of cpp, which I learned can be done easily without having to use a previous version of the compiler.

On ubuntu 16.04

sudo apt-get install cpp-4.7

and then use

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



回答3:


You can use cpp with flag -nostdinc and do not use flag -C.



来源:https://stackoverflow.com/questions/25314725/remove-the-comments-generated-by-cpp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!