After class C ++, Mr. Yang gave his students a question about the memory structure of C ++ function calls. Refer to "Programmer self-cultivation"

Source: Internet
Author: User

This article describes how to develop or generate c/c ++ in Linux. o. a. so files in the intermediate library status (it may be that you write a lib for others to call or provide. c /. cpp files are embedded into others' Makefile projects ). How to view some basic information about these library files. Sometimes many errors, such as "undefine reference", occur when you compile a program (specifically when you are using a linker link), are found because they are not found. o. a. so library file, resulting in link failure.

-------------------------------------------------------------------------------

1. Linux Library File

2. Usage of library files

3. Use tar/nm to view the Library File Information

-------------------------------------------------------------------------------

1. Definition of library files is not cumbersome here, so you are interested in Google. To put it bluntly, we have written some corresponding information. h and. c (. cpp) file, and then generate intermediate code for others' use through compiler compilation. Others only need to include your intermediate code into their own programs. Note that it takes several steps for the compiler to compile the final executable file, which can be divided into: Text parsing-> Syntax Parsing-> This method analysis-> preprocessing analysis-> compilation-> connection. There is no link stage for generating the intermediate library. in Linux Gcc, you can use the-C parameter to specify that only compilation is not linked. Therefore, if you write one. the c file uses external calls such as pthread_create and does not use-lpthread during Gcc-C compilation because no link is required at this time.

2. (skip this step if you are familiar with library files) on each system platform, the format and format of library files are different. Windows is like xxx. dll or xxx. lib, * inux is xxx. so or xxx. a. The two types correspond to the static library and the dynamic library respectively. The static library enters the program as part of the program together with the compiler compilation link, the advantage is that as a part of the program, you do not need to load it every time you run it (the disadvantage is that many processes may use this database but each process has one copy. If the dynamic library has only one copy in the memory, by redirecting), and does not cause the running to fail due to missing libraries. The disadvantage is that the executable file is too large. The dynamic library is dynamically loaded into the process when the program is running, and can be shared by multiple processes. It facilitates software updates and replaces the old library directly.

Whether a static or dynamic library is used depends on the context environment of the program. Generally, the third-party Library provides two versions. The system library is generally a dynamic link library, because the libraries in the same system are the same. Use the Linux Gcc for example:

1. Write A. h Statement to declare a foo () function, and then implement the foo () function in. c or. cpp.

Ii. gcc (g ++)-c-o foo. o foo. c (note that the. h header file is not required here. The header file only describes the external interface of the Library)

3. Generate a static library: ar-r libfoo. a foo. o (static library. a is actually the compressed package of the. o file. Note that. a is not supported here.)

Generate dynamic library: gcc foo. c-fPIC-shared-o libfoo. so (-fPIC means to generate location-independent code. Because the dynamic library is loaded at runtime, code must be redirected. You can Google it if you are not sure)

4. Write a file containing the main function and call the foo () function: gcc (g ++)-o test. c-lfoo. Here-lfoo indicates to find a file starting with lib. so or. file a, which is preferred by default. so dynamic library. Www.2cto.com

Note that if cpp references the c library or. c, the header file should use the externc "C" keyword to specify to read data in c mode (basically because the function signatures of c and c ++ are inconsistent, because c ++ supports heavy load, therefore, c ++ cannot find c functions with the same name ). When a dynamic library is used, the program goes back to some predefined places to find the. so file. For example, in/usr/lib/, modify the/etc/ld. so. conf file if you need to specify it yourself. Use ldconfig to refresh the cache.

 

3. If you need to view the information of your own database, you can use nm to view the information, such as the functions in the database, global variables, and dependent items of other databases, the following is an example:

[Cpp]
# Include <stdio. h>
 
Int g1;
Int g2 = 0;
 
Static int g3;
Static int g4 = 0;
 
Const int g5 = 0;
 
Static const int g6 = 0;
 
Int main (int argc, char * argv [])
{
Static int st = 0;
 
Int t1;
Int t2 = 0;
 
Const int t3 = 0;
 
Printf ("printf-function ");
 
Return 0;
}
 
Void foo1 (){}
Static void foo2 (){}
[Cpp] view plaincopyprint?
Void overload (int I ){}
[Cpp] view plaincopyprint?
Void overload (float I ){}

The linux nm command can list the symbols in a file and list the compiled by the above Code Gcc-c. o (. a. so) You can use the nm command to view the symbol information:

[Cpp]
0000000000000000 TB
0000000000000000 d
0000000000000000 B
0000000000000000 r
0000000000000000 r
0000000000000000 n
0000000000000000 n
0000000000000000 B g1
0000000000000004 B g2
0000000000000008 B g3
000000000000000c B g4
000000000000001c r g5
0000000000000020 r g6
U _ gxx_personality_v0
0000000000000000 T main
0000000000000000 a nm. cpp
U printf
000000000000003e T _ Z4foo1v
0000000000000044 t _ Z4foo2v
0000000000000054 T _ Z8overloadf
2017100000000004a T _ Z8overloadi
0000000000000010 B _ ZZ4mainE2st
The first column on the left is the address value of the symbol. The corresponding source code shows the increasing rule. The second column is the type of the symbol, and the third column is the name of the symbol (such as the function name and variable name ):

Symbol type: describes several of the most commonly used symbols. If you encounter other symbols, Google:

B --- the symbol of the global non-initialized data segment (BBS segment). Its value indicates the offset of this symbol in the bss segment, such as g1

B --- the Global static symbol, such as g3

R --- readonly)

N --- symbols used for debug

T --- symbol located in the code area, such as the main foo function in this file

T --- the symbol in the code area. It is generally a static function.

U --- Call function or variable symbol located outside the current file, such as the system's printf () function

Note that g ++ is used for compiling, so it is compiled in the c ++ function style that supports heavy load. You can see that all functions contain prefixes and suffixes, the prefix indicates the name of the class, and the suffix indicates the abbreviation of the type of the parameter list, because the overload must distinguish the parameter type. Here we can also see why different functions with different return values are not overloaded, because there is no returned record in the symbol table.

For example, the suffixes of the two overload functions are f and I, respectively, which indicate float and int (v-> void ).

The nm command is very useful for debugging multi-module programs. In most cases, it can solve the "undefined reference" problem. If you find that the alternative usage of nm is good, you can leave a message !!!

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.