Home >Operation and Maintenance >Linux Operation and Maintenance >What is the connection between libraries and header files in Linux?
The relationship between libraries and header files in Linux: there are declarations of functions in header files, and library files implement the definition of functions; each function in the library needs to be declared in the header file. When writing a program, you need to use header files to provide definitions of constants and declarations of calls to system functions and library functions; and a library is a collection of compiled functions, that is, a collection of target files ".o". It is written according to the principle of reusability and generally consists of a set of interrelated functions to perform a common task.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
In general application source code, there are function declarations in the header file, and library files implement the definition of the function; each function in the library needs to be declared in the header file.
When writing a program, we need to use header files to provide definitions of constants and system functions and libraries Declarations of function calls. These header files are located in /usr/include
. These header files are located in directories that can be automatically searched by the compiler, such as /usr/include/X11 of the X window system and /usr/include /c of GNU C.
When calling gcc, you can use the flag -I
to include header files saved in subdirectories or other non-standard locations, such as gcc -I/usr/openwin/ include fred.c
The library is a collection of compiled functions, that is, the target file A collection of .o is written according to the principle of reusability and generally consists of a set of interrelated functions to perform a common task.
Standard Linux system library files are generally stored in the /lib
and /usr/lib
directories. By default, the GCC linker only searches the standard C language library, which is a historical issue. We must conform the library file name to a specific convention and specify it on the command line.
Library files always start with lib, and then indicate what library it is. The last part .a represents the static function library, and .so represents the shared function library (that is, the DLL dynamic link library under Windows).
Such as gcc -o fred fred.c /usr/lib/libm.a
or gcc -o fred fred.c -lm
# The ##-L flag increases the library search path for the linker, such as
gcc -o x11fred -L /usr/openwin/lib x11fred.c -1X11
Then use
gcc -c to compile and generate two target files
Then for these two Write a header file for each function, make a function declaration
Then include the header file in the main function and call one of the library functions
Then compile the main function and link main.o with the previously generated bill.o, which can be executed correctly
Next we generate the library file and use ar to archive the program Create an archive file and add target files to it, which combines several individual files into one large file.
ar crv libfoo.a bill.o fred.o After that we need to use the ranlib
command to generate a content table for the function library.
Then we link the library file with the previously generated program.o file and the execution is successful
Also Use
-l to specify function libraries, and -L
to specify non-standard locations.
nm command to view which functions are included in the target file, function library or executable file Comparison of static libraries between Linux and Windows When many applications are running at the same time and they all use functions from the same function library, there will be many copies of the same function in the memory, which wastes memory. And external storage! ! When a program uses a function library, it no longer contains function code when linking, but refers to shared code accessible at runtime. When the compiled program is loaded into memory for execution, the function reference is parsed and A call is made to the shared library, and if necessary, the shared library is loaded into memory. That is, the system can keep only one copy of the shared library in memory for many applications to call, or it can keep only one copy in external memory. Moreover, shared libraries can be updated independently of the applications that rely on them, and the applications do not need to be recompiled. Use the intermediate file bill.o fred.o to generate the shared library libfoo.so After the shared library is generated, it must be placed in the standard search directory. Additional locations for searching shared libraries can be configured in the file Then specify the search path Link to generate executable file lddTools can be used for analysis Shared libraries required to run the executable file You can also specify the search path of the .so library when compiling the target code, through the gcc parameter Related recommendations: "Linux Video Tutorial", available For disassembly analysis. When a program is created, it only contains the functions it actually needs in the function library. Although there are declarations of all functions in the header file, the linker will not link them all in.
2.2 Shared library
In a Linux system, the program (dynamic loader) responsible for loading shared libraries and parsing functions referenced by client programs is ld.so
2.2.1 Generation method
gcc -shared -fpic -o libfoo.so bill.o fred .o
/etc/ld.so.conf
. After modifying this file, execute the command ldconfig
to process it. The content of /etc/ld.so.conf
is include /etc/ld.so.conf.d/*.conf
, create your own dynamic in this directory Link library path configuration filemylib.conf
gcc -o main program.o -lfoo -L.
-Wl,- rpath
is specified, such as gcc main.cpp -lfoo -L. -Wl,-rpath=.
-Wl, which means that the following parameters will be passed to the link program ld (because gcc will automatically call ld) When specifying multiple dynamic library search paths, separate the paths with colons ":".
The above is the detailed content of What is the connection between libraries and header files in Linux?. For more information, please follow other related articles on the PHP Chinese website!