I'd like to strip the comments from a c++ file. This post provides the following solution for this:
gcc -fpreprocessed -dD -E test.c
However, this command appears to collapse long multiline comments. This causes behavior differences if the macro __LINE__
is used.
Can the gcc solution be salvaged to not change __LINE__
-dependent behavior? Or, a non-gcc solution would work fine too.
Example test.c:
int main() {
/*
*
*
*
*
* comment 1
*/
// comment 2
return 0;
}
Output using gcc 4.9.2:
$ gcc -fpreprocessed -dD -E test.c
# 1 "test.c"
int main() {
# 10 "test.c"
return 0;
}
If we delete // comment 2
, then we instead get the desired output without comment collapsing:
$ gcc -fpreprocessed -dD -E test.c
# 1 "test.c"
int main() {
return 0;
}