#define _ROOT_PATH 中_ROOT_PATH的值是什么

奔跑的路 2014-09-16 10:03:24
#define _ROOT_PATH 中_ROOT_PATH的值是什么,如果有,是什么类型的值,怎么验证?
...全文
569 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
buyong 2014-09-17
  • 打赏
  • 举报
回复
gcc -E 查看预处理后代码,你就明白了。就是空值
奔跑的路 2014-09-16
  • 打赏
  • 举报
回复
引用 1 楼 micropentium6 的回复:
if it's like #define _ROOT_PATH then it is nothing but a switch for cpp... u probably could find something like #ifdef _ROOT_PATH code here #else code here #endif
这往往作为一个开关(switch)的用法是知道的,它能够控制编译量和提高移植性,但是如你所说“it is nothing”,那么里面应该是没有值了吧?还是赋有空白、空格或其他字符? 宏定义在预编译的时候进行处理,那么应该是没有数据类型的, 如果 _ROOT_PATH当做一个开关来使用,_ROOT_PATH没有值(nothing)会不会占内存空间呢,还是存放在预编译里面的一个字符数组中?这个字符串是将会当做一个变量来看待,还是单纯的当做一个标签贴在哪里? 又怎么来验证呢?比如 printf("%s",_ROOT_PATH);是编译不通过的。 资料说预编译做些代码文本的替换工作,_ROOT_PATH后面只有空格和换行符,不会用_ROOT_PATH来替换这些吧? 有这么多疑问我想主要是自己对编译原理不怎么了解,如果能讲讲清楚,将加40分,谢谢!!
  • 打赏
  • 举报
回复
if it's like #define _ROOT_PATH then it is nothing but a switch for cpp... u probably could find something like #ifdef _ROOT_PATH code here #else code here #endif
  • 打赏
  • 举报
回复
It doesn't have a type that you may think it should have for all variables you defined in your c/c++ codes. It was processed during the preprocessing stage of compilation. When the output is fed to the scanner/parser for further process, this macro is gone... Here is a copy/paste from <Professional Liunx Kernel Architecture> ❑ Preprocessing —All pre-processor actions are performed in this phase. Depending on the compiler version, this phase is supported by an external utility (cpp) or by special library functions— both are initiated automatically by the compiler. On completion of preprocessing, there is only one (large) input file generated from the source file and all header files are included using the #include directive. The compiler itself is then no longer required to take account of the distributed structure of C programs over several files. ❑ Scanning and Parsing — The syntax of a programming language can be described by means of grammatical rules that are similar to those of a natural language such as English but that must understandably be much more restrictive. (Although the existence of several alternatives to represent one and the same fact contribute greatly to the appeal and subtlety of a language, ambiguity must be avoided at all costs in programming languages.) This phase usually comprises two closely linked tasks. The scanner analyzes the source text character-by-character and looks for keywords of the programming language. The parser takes the input stream supplied by the scanner and already abstracted from source text representation and checks that the structures it detects are correct in terms of the grammar rules of the language. It also creates data structures in computer memory that are a further abstraction of the source code and are designed for processing by a computer (in contrast to the actual source code of the program that should be as easy as possible to read and manipulate by human beings). ❑ Intermediate Code Generation — A further step along the path toward the final machine code converts the parse tree (i.e., the data structure created in memory) set up by the scanner and parser into another language known as the register transfer language (RTL). This is a kind of assembly language for a hypothetical machine. This language can be optimized—independently of the target processor for the most part. However, this does not mean that the RTL code generated in this phase of the compilation process is the same for all target processors. Depending on the architecture, a range of assembler statements are available— and this fact must be taken into account during RTL generation. The individual statements of the RTL are already on a very low level and are a step away from the high-level C language on the path to the assembly language. Their main task is to manipulate register values to support execution of the compiled program. There are, of course, also conditional statements and other mechanisms to control program flow. However, this intermediate code still includes various elements and structures common to higher-level programming languages (these are not specific to a particular language such as C, Pascal, etc.) that do not appear in a pure assembly language. ❑ Optimization— The most compute-intensive phase of program compilation is optimization of the intermediate code in the RTL language. The reasons why programs are optimized are clear. But how is this done by the compiler? Because the mechanisms used are generally not only complex but also sophisticated and even devious (subtle details must always be taken intoaccount), it would not be difficult to write a long tome on optimization techniques alone, and a further one on their usage in the GCC. Nevertheless, this appendix illustrates at least some of the techniques employed. All optimization options are based on ideas that initially appear to be relatively simple. However, in practice (and in theory) they are difficult to implement. Such options include, above all, the simplification of arithmetic expressions (algebraic rewriting of terms into expressions that can be computed more efficiently and/or with a less-intensive use of memory), elimination of dead code (parts of code that cannot be reached by the program flow),merging of repeated expressions and items of code in a program, rewriting of program flow into a more efficient form, and so on—these are covered as individual topics in this appendix. ❑ Code generation— The last phase is concerned exclusively with the generation of the actual assembler code for the target processor. However, this does not yet produce an executable binary file, but instead, it produces a text file with assembler instructions that is converted into binary machine code by further external programs (assemblers and possibly linkers). In principle, the assembler code has the same form as the code of the final program but can still be read by humans (not by machines) even if the power of the individual commands has reached machine level.
zhxianbin 2014-09-16
  • 打赏
  • 举报
回复
你都知道宏是在预处理阶段字面替换了,printf("%s",_ROOT_PATH); 替换后 printf("%s", ); 当然不能编译通过,为什么楼主觉得这里不替换呢,所用的宏在预处理阶段都被替换

23,223

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧