Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

c++ - how to find the source of some macros

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A simple, quick way to find out where a macro has been defined is to redefine the macro and check compiler's Warning/Error message.

#include <windows.h>
#define min(a,b) nonsense

mintest.cpp(3) : warning C4005: 'min' : macro redefinition
    C:ProgrammeMicrosoft SDKsWindowsv6.0Aincludewindef.h(194) : see previous definition of 'min'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...