Error analysis of symbol table during C++ compilation and linking

      This is the case. When I was studying Teacher Zheng Li's multi-file structure and compilation preprocessing command chapters, I saw such a picture in the book. The description is as follows: The function of the #include instruction is to embed the specified file into the current source file #include The location of the command.

Then I thought that the main program 5_10.cpp can directly include point.cpp (because point.h is included in point.cpp, so there are both declarations and definitions). Yes, the book continues to describe that the embedded file can be a .h file or a .cpp file. But when I verified in the eclipse for c++ environment, I was slapped in the face, which made me wonder if I couldn't #include .cpp.

In eclipse, an error is reported at the step of connection, as follows:

I didn't read the error report in eclipse seriously, and the result of an operation verification in vim obviously can be imported into cpp. The upper part is the test of include .h, and the lower part is the test of include .cpp. It can be seen that there is nothing wrong with generating the .o file, that is, compiling and generating the target code, but the error is reported when the .h is introduced in the connection step. , the introduction of .cpp is normal (this result is just the opposite of that in eclipse), which makes me think, oh, I found out, in the command line, I use the command to specify which cpp file to compile, and when I introduce the test of .h In the .cpp test, I only compiled test but did not compile and did not connect point.cpp, so it is normal that the function address cannot be found when connecting (you can notice that the error reported by it is undefined, and the report in eclipse is duplicate, That's the difference...)

Go back to eclipse, look up the error, and see that eclipse seems to have compiled all the cpps under my project. It looks like...emmm make all. In fact, just compile and compile, it doesn't matter. Finally, don't connect those that I don't use, but you can see that this is really a linker all..


Since I included Point.cpp of 6 folders in main.cpp, this is equivalent to compiling Point.cpp in 6 folders twice (two symbol tables about point.cpp are generated, about what is The symbol table is a data structure that associates the names of identifiers in the program with their addresses in each segment, see the figure below). Then when linking, the object files of each compilation unit and the units called in the runtime library are merged. After merging, the code segments and data segments of different compilation units are merged together. At the same time, each object file is merged. The symbol tables can also be synthesized, and each entry in the final symbol table must have a definite address. However, when the eclipse connection error is reported, the error is reported in the function address of the symbol table. The Point.cpp referenced in main and the Point.cpp in the 6 folder are one thing, but the Point class in the symbol table is generated when the .o file is generated. Each function has its own address.


A prerequisite for the correct synthesis of the symbol table is that the same symbol is defined in exactly one compilation unit and undefined in other compilation units. The reason for this requirement is that the address of each symbol in the merged symbol table needs to be determined according to the relative address of the symbol in the defined compilation unit. If the same symbol has a defined address in multiple compilation units, its address will be at a loss, and a link error of symbol definition conflict will occur. So you can see that the error message given by eclipse is "duplicate symbol".

The name in the form of __ZN5... is the function name. In the symbol table, the function is not only named after its name in the source program. The name of the function in the symbol table includes at least the function name and parameter table type information of the source program. Because functions can be overloaded, and because there is no special type information in the symbol table, the parameter table information can only be reflected in the name. Otherwise, functions with the same function name and different parameters cannot be distinguished in the object file. look, where the move function is a member function defined in my point class, and the rest are constructors (the ones defined by me + other default constructions in the class) or destructors. Therefore, it seems that if you want to refer to the cpp file in eclipse, you must rewrite the make file in eclipse yourself.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215918&siteId=291194637