Mat*_*att 14 c gcc c-preprocessor
我在程序中定义了许多调试语句,我希望能够在没有这些语句的情况下复制源代码.
为了做到这一点,我首先看了GCC的-E命令行参数,它只运行预处理器,但这比我想要的要多得多,扩展包含的文件并添加#line语句.
例如:
#include <stdio.h>
#ifdef DEBUG
#define debug( s ) puts ( s );
#else
#define debug( s )
#endif
int main( int argc, char* argv[] )
{
debug( "Foo" )
puts( "Hello, World!" );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望将其处理为:
#include <stdio.h>
int main( int argc, char* argv[] )
{
puts( "Hello, World!" );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我可以用astyle这样的东西来整理它,并且不需要手动工作来获得我想要的东西.
GCC是否缺少指令,或者是否有能够执行此操作的工具?
如果-E没有帮助,请尝试使用-fdump-tree-all,如果您没有看到您想要的内容,则 is not-available-in (or) not-provided-by GCC。
OTOH,这个问题已在 SO 中讨论如下,请参考下面的内容以获得一些想法。
希望能帮助到你!
嗨马特,
我看到你对@nos 的评论。但我手边有一个这样的脚本,因此与您分享。您可以尝试在这里阅读我对类似问题的回答
将以下代码复制到文件中,例如convert.sh. 为该文件分配执行权限,chmod +x convert.sh然后运行它,如下所示:
$./convert.sh <filename>.c
$cat filename.c.done
Run Code Online (Sandbox Code Playgroud)
那里<filename>.c.done会有你需要的东西!
#!/bin/bash
if [[ $# -ne 1 || ! -f $1 ]] ; then
echo "Invalid args / Check file "
exit
fi
file_name=$1
grep '^\s*#\s*include' $file_name > /tmp/include.c
grep -Pv '^\s*#\s*include\b' $file_name > /tmp/code.c
gcc -E /tmp/code.c | grep -v ^# > /tmp/preprocessed.c
cat /tmp/include.c > $file_name.done
cat /tmp/preprocessed.c >> $file_name.done
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
gcc -E -nostdinc test.c产生
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
# 9 "test.c"
int main( int argc, char* argv[] )
{
puts( "Hello, World!" );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以及 stderr 的错误
test.c:1:19: error: no include path in which to search for stdio.h
Run Code Online (Sandbox Code Playgroud)
您可以轻松过滤掉 # 行...并重新添加包含内容。