Obtain the include path of a symbol from the pre-compilation result.

Source: Internet
Author: User

When there are many header files and the reference relationship is complex, how do you know the path through which the compiler finds the declaration of a symbol (variable, function, class) during compilation. this can be obtained by viewing the pre-compiled output results. Example: A three-headed file, a CPP file.

Simple1.h

#ifndef Project1_Simple1_h#define Project1_Simple1_hclass Circle{public:Circle(int r);double Area(void);private:Circle(void);int m_radius;};#endif // Project1_Simple1_h

Simple2.h

#ifndef Project1_Simple2_h#define Project1_Simple2_h#include "Simple1.h"void FunctionInS2();#endif // Project1_Simple2_h

Simple3.h

#ifndef Project1_Simple3_h#define Project1_Simple3_h#include "Simple2.h"void FunctionInS3();#endif // Project1_Simple3_h

Main. cpp

#include "Simple3.h"double AddArea(int a, int b){Circle ca(a);Circle cb(b);return ca.Area() + cb.Area();}

Pre-compile main. cpp. The output result is as follows:

// Preprocessed output for main.cpp// Generated at 2:29:57 PM on Saturday, April 7, 2012// Using Debug configuration, x86_64 architecture for Project1 target of Project1# 1 "/Users/gavinwang/MyProject/main.cpp"# 1 "/Users/gavinwang/MyProject/main.cpp" 1# 1 "<built-in>" 1# 1 "<built-in>" 3# 149 "<built-in>" 3# 1 "<command line>" 1# 1 "<built-in>" 2# 1 "/Users/gavinwang/MyProject/main.cpp" 2# 1 "/Users/gavinwang/MyProject/Simple3.h" 1# 1 "/Users/gavinwang/MyProject/Simple2.h" 1# 1 "/Users/gavinwang/MyProject/Simple1.h" 1class Circle{public: Circle(int r); double Area(void);private: Circle(void); int m_radius;};# 5 "/Users/gavinwang/MyProject/Simple2.h" 2void FunctionInS2();# 5 "/Users/gavinwang/MyProject/Simple3.h" 2void FunctionInS3();# 2 "/Users/gavinwang/MyProject/main.cpp" 2double add(int a, int b){ Circle ca(a); Circle cb(b); return ca.Area() + cb.Area();}

As you can see, the pre-compiled output result consists of fixed format segments. Each section starts with "# line number header file name", followed by the function declaration in this file. Use simple1.h as an example (line 17 ~ 27) from row 1st of simple1.h, It is the declaration of class circle. The sequence of header files is the include sequence during compilation.

If we want to find the path where a symbol in the source code is included, such as area

1. Find the referenced position in the source code (line 38) from the pre-compiled output ),

2. Find the first header file (line 30, simple3.h) from the beginning of the file)

3. Check the declaration in the header file. If the symbol exists, find the path. Otherwise, go back to Step 1 and continue searching for the beginning of the file from the current position.

The header file order of the path is the include path of the symbol.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.