C macros __LINE__, __FILE__ and __func__
2015-01-27 10:46
330 查看
It is possible for a C program to print the currently executing line of source code, the file of the source code, and the name of the current function. The currently executing line is available in a preprocessor variable called
This prints out
To get the name of the file, use the preprocessor variable
This prints out
C99 (the most recent version of the C language, at the time of writing) introduced another macro called
This prints out
You can change the values used for
which tells the C compiler that the next line after the line directive is line 20 of
This prints out
This line directive is used by facilities like "lex" and "yacc" which output C code so that error messages are related to the input file rather than the output C file.
GCC (the "Gnu Compiler Collection") also defines
__LINE__:
<span class="preprocessor">#include <<a target=_blank href="http://www.nxmnpg.com/3/stdio" style="color: rgb(51, 102, 153); text-decoration: none;">stdio</a>.h> </span> <span class="type">int</span> <span class="function-name">main</span> () { <span class="function-name"><a target=_blank href="http://www.nxmnpg.com/3/printf" style="color: rgb(51, 102, 153); text-decoration: none;">printf</a></span> (<span class="string">"This is line %d.\n"</span>, <span class="variable-name">__LINE__</span>); <span class="keyword">return</span> <span class="number">0</span>; }
This prints out
This is line 5.
To get the name of the file, use the preprocessor variable
__FILE__:
<span class="preprocessor">#include <<a target=_blank href="http://www.nxmnpg.com/3/stdio" style="color: rgb(51, 102, 153); text-decoration: none;">stdio</a>.h> </span> <span class="type">int</span> <span class="function-name">main</span> () { <span class="function-name"><a target=_blank href="http://www.nxmnpg.com/3/printf" style="color: rgb(51, 102, 153); text-decoration: none;">printf</a></span> (<span class="string">"This is line %d of file \"%s\".\n"</span>, <span class="variable-name">__LINE__</span>, <span class="variable-name">__FILE__</span>); <span class="keyword">return</span> <span class="number">0</span>; }
This prints out
This is line 6 of file "/share/websites/www.lemoda.net/c/line-file-func/file.c".
C99 (the most recent version of the C language, at the time of writing) introduced another macro called
__func__which also names the function in use:
<span class="preprocessor">#include <<a target=_blank href="http://www.nxmnpg.com/3/stdio" style="color: rgb(51, 102, 153); text-decoration: none;">stdio</a>.h> </span> <span class="preprocessor">#define BLURT <a target=_blank href="http://www.nxmnpg.com/3/printf" style="color: rgb(51, 102, 153); text-decoration: none;">printf</a> ("This is line %d of file %s (function %s)\n",\ __LINE__, __FILE__, __func__) </span> <span class="type">void</span> <span class="function-name">silly_function</span> () { <span class="variable-name">BLURT</span>; } <span class="type">int</span> <span class="function-name">main</span> () { <span class="variable-name">BLURT</span>; <span class="function-name">silly_function</span> (); <span class="keyword">return</span> <span class="number">0</span>; }
This prints out
This is line 13 of file /share/websites/www.lemoda.net/c/line-file-func/func.c (function main) This is line 8 of file /share/websites/www.lemoda.net/c/line-file-func/func.c (function silly_function)
You can change the values used for
__FILE__and
__LINE__using a line directive, which is a preprocessor command of the form
#line 20 "some-file"
which tells the C compiler that the next line after the line directive is line 20 of
some-file.
<span class="preprocessor">#include <<a target=_blank href="http://www.nxmnpg.com/3/stdio" style="color: rgb(51, 102, 153); text-decoration: none;">stdio</a>.h> </span> <span class="preprocessor">#define BLURT <a target=_blank href="http://www.nxmnpg.com/3/printf" style="color: rgb(51, 102, 153); text-decoration: none;">printf</a> ("This is line %d of file \"%s\" (function <%s>)\n",\ __LINE__, __FILE__, __func__) </span> <span class="preprocessor">#line 99 "grody-to-the-max" </span><span class="type">void</span> <span class="function-name">silly_function</span> () { <span class="variable-name">BLURT</span>; } <span class="preprocessor">#line 999 "bassetts-liquorice-allsorts" </span><span class="type">int</span> <span class="function-name">main</span> () { <span class="variable-name">BLURT</span>; <span class="function-name">silly_function</span> (); <span class="keyword">return</span> <span class="number">0</span>; }
This prints out
This is line 1001 of file "bassetts-liquorice-allsorts" (function <main>) This is line 101 of file "grody-to-the-max" (function <silly_function>)
This line directive is used by facilities like "lex" and "yacc" which output C code so that error messages are related to the input file rather than the output C file.
GCC (the "Gnu Compiler Collection") also defines
__FUNCTION__and
__PRETTY_FUNCTION__. These are both the same as
__func__for C. For C++,
__PRETTY_FUNCTION__is something different.
相关文章推荐
- C macros __LINE__, __FILE__ and __func__
- C macros __LINE__, __FILE__ and __func__
- C macros __LINE__, __FILE__ and __func__
- Reading and modify OS X plist file by command line
- __FILE__,__LINE__,__func__ 真好用,DEBUG利器啊!
- C语言中的__FILE__、__LINE__和__func__等预定义跟踪调试
- FAQ: Why is file and line information available for some warnings in FxCop but not for others?
- MySQL import SQL file using comand line
- Open and read file line by line tcl tk
- 宏 __FILE__,__LINE__,#line,__func__
- how to get the caller's module name, file name , function name and line number?
- <Java> 获取当前调用函数, 如同C++中的__FUNC__, __FILE__, __LINE__
- Using __FILE__ and __LINE__ to Report Errors
- gcc中用于预编译的宏(如__cplusplus , __func__ , __FILE__ , __LINE__ 等)
- Windows and Linux File Line End
- JAVA中实现C语言的_FILE_,_LINE_,_FUNC_,_TIME_
- # and #line and __FILE__ and __LINE__
- python 中如何实现类似C语言中 __FILE__, __LINE__, __FUNC__
- C++ '__FILE__' and '__LINE__
- add cool toolbar 256corler is easy,only two file and onCreate func is needed also the MainFrame.toolbar must resize to what real need and also the load width is same to it.