如何找到一些宏之源 [英] how to find the source of some macros

查看:132
本文介绍了如何找到一些宏之源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有定义一个macro.When在我们自己的项目由我们宏定义许多地方,很容易找到他们的定义位置。
但是当我尝试去学习一些著名的开源项目,我经常被这个问题纠缠:在哪里可以找到宏的来源,如果我不能得到它的定义,我会不明白其中的一些人(例如一些可以通过他们的名字被猜出)。
例如,Apache的一些语句:

 #如果定义(__ osf__)及和放大器;定义(__阿尔法)#elif指令定义(__ NSIG)

对于我的知识,我知道有一个宏观的一些可能的始发地点:


  1. 从这个项目本身,在一些源文件(这是最简单的,因为我们可以通过一些工具找到它)

  2. 一些3 LIB的一些头文件
  3. ,我们可以grep它

  4. 从C / C ++标准头文件(其中,他们是在Linux?)

  5. 从OS
  6. (其中有他们在Linux?)

  7. 将配置工具自动生成的(这是痛苦的,我不知道)

  8. 从海湾合作委员会一样/ G ++,或者在Makefile中,我们可以定义一些宏编译工具

我有一些问题请教:


  1. 如何区分它们之间的OS定义和gcc / g ++的定义和配置工具生成的宏?他们分别有一定的特点呢?

  2. 如何找到那些操作系统或标准的C编译器或定义的来源?例如,使用的grep 找到应用

  3. 这是什么意思,如果一个宏,如 __奇怪___ 无法通过梳理整个机器被找到( CD /; grep的__strange___ -r )?

谢谢你告诉的原理和方法来区分他们,找到他们的源泉!


解决方案

  

      
  1. 如何区分它们之间的OS定义和gcc / g ++的定义和配置工具生成的宏?他们分别有一定的特点呢?

  2.   

绝大多数都是在一些头文件中定义的某个地方。 GCC -dN -E 可帮助在这里。注意事项:如果你使用这种方法,你需要调用 GCC -dN -E 与同包括路径,相同的 -D<名称> 命令行选项,同样的环境变量,例如 CPATH ,......,因为你在编译源文件的对象做。


  

      
  1. 如何找到那些操作系统或标准的C编译器或定义的来源?例如,使用grep或找到应用

  2.   

RTFM。阅读精细的手工。


  

      
  1. 这是什么意思,如果一个宏,如 __奇怪__ 无法通过梳理整个机器被找到(CD /; grep的__strange___ -r)

  2.   

这可能只是意味着该符号是不是在您的计算机上定义。假设有问题的code是一些开源软件包,针对不同的系统,不同的编译器,其中一些不符合C ++标准兼容相当一船。典型的做法是在code的关键部位使用 #IFDEF __some_bizarre_os __ 。该符号只能在运行离​​奇OS的机器来定义 - 不会对你的

不幸的是,这不是唯一的案例。即使你的grep不能在任何地方找到它的标志很可能会被定义。 makefile文件可以连接两个字符串, -D__str 安格__ 来形成一个单一的命令行参数编译器。选项​​ -D__strange __ 可能在众目睽睽下由makefile文件使用环境变量中的一个藏身。在〜/ .tcshrc文件的文件,有些项目任务可以非常令人费解。

更新结果
GCC -dM -E 显示宏的定义,但它们被定义不是哪里。一个更好的选择是使用 GCC -dN -E ,然后过滤掉不符合最初的开始行

There are many places for defining a macro.When the macro is defined in our own project by us,the are easy to find the definition position for them. But when i try to learn some famous open source project,i am frequently pestered by the question:where to find the source of the macros,if i can not get it's definition,i won't understand some of them (e.g. some of them can be guessed by their name). for example,some statement from apache:

#if defined(__osf__) && defined(__alpha),

#elif defined(__NSIG)

as for my knowledge,i know there are some possible originating place for a macro:

  1. from this project itself,in some source file(this is the easiest,because we can find it by some tool)
  2. from some header file of some 3rd lib ,we can grep it
  3. from c/c++ standard header file(where are they in linux?)
  4. from the os (where are they in linux?)
  5. automatically generated by the configure tool(it is bitter,i have no idea)
  6. from the compiler tool like gcc/g++,or in the makefile we can define some macro

I have some question to consult:

  1. how to differentiate them between os defined and gcc/g++ defined and configure tool generated macros? do they have some characteristic respectively?
  2. how to find the source of those defined by os or by standard C or compiler? e.g.,using grep or find utilities
  3. what does it mean if one macro such as __strange___ can not be find by combing the whole machine (cd /;grep __strange___ -r)?

Thanks for telling the principle and the method to distinguish them and ,to find the source of them!

解决方案

  1. how to differentiate them between os defined and gcc/g++ defined and configure tool generated macros? do they have some characteristic respectively?

The vast majority are defined in some header file somewhere. gcc -dN -E can be of help here. Caveat: If you use this approach, you need to invoke gcc -dN -E with the same include paths, the same -D<name> command line options, the same environment variables such as CPATH, ..., as you do when you compile your source to object files.

  1. how to find the source of those defined by os or by standard C or compiler? e.g.,using grep or find utilities

RTFM. Read the fine manual.

  1. what does it mean if one macro such as __strange__ can not be find by combing the whole machine (cd /;grep __strange___ -r)?

It could just mean that that symbol isn't defined on your computer. Suppose the code in question is from some open source package that targets a boatload of different systems, different compilers, some of which are not quite compliant with the C++ standard. The typical approach is to use #ifdef __some_bizarre_os__ in key parts of the code. That symbol will only be defined on machines running the Bizarre OS -- not on yours.

Unfortunately, that's not the only case. The symbol might well be defined even if your grep can't find it anywhere. The makefile could concatenate two strings, -D__str and ange__ to form a single command line argument to the compiler. The option -D__strange__ might be hiding in plain sight in one of your environment variables used by the makefile. The ~/.tcshrc files that some projects mandate can be incredibly convoluted.

Update
gcc -dM -E shows the definitions of the macros, but not where they were defined. A much better options is to use gcc -dN -E and then filter out lines that don't start with an initial #.

这篇关于如何找到一些宏之源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆