卸下C / C ++ code评论 [英] Remove comments from C/C++ code
问题描述
有一个简单的方法来消除从C / C ++源文件的注释没有做任何preprocessing。 (也就是说,我想你可以用gcc -E但是这将扩大宏)我只想源$ C $ c。与意见剥离,其他什么都应该有所改变。
Is there an easy way to remove comments from a C/C++ source file without doing any preprocessing. (ie, I think you can use gcc -E but this will expand macros.) I just want the source code with comments stripped, nothing else should be changed.
编辑:
对现有的工具preference。我不希望有这种用正则表达式写我自己,我预见到code太多的惊喜。
Preference towards an existing tool. I don't want to have to write this myself with regexes, I foresee too many surprises in the code.
推荐答案
在源文件运行以下命令:
Run the following command on your source file:
gcc -fpreprocessed -dD -E test.c
感谢KennyTM为寻找合适的标志。下面是完整的结果:
Thanks to KennyTM for finding the right flags. Here’s the result for completeness:
test.c的:
#define foo bar
foo foo foo
#ifdef foo
#undef foo
#define foo baz
#endif
foo foo
/* comments? comments. */
// c++ style comments
GCC -f preprocessed -dD -E test.c的:
gcc -fpreprocessed -dD -E test.c:
#define foo bar
foo foo foo
#ifdef foo
#undef foo
#define foo baz
#endif
foo foo
这篇关于卸下C / C ++ code评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!