Home > Operation and Maintenance > Linux Operation and Maintenance > What is the connection between libraries and header files in Linux?

What is the connection between libraries and header files in Linux?

青灯夜游
Release: 2023-03-16 10:27:23
Original
1457 people have browsed it

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.

What is the connection between libraries and header files in Linux?

#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.

1. 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

2. Library file

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

##2.1 Static libraryThe static library ends with .a, also called an archive file. When generating an executable program, the static library file is linked in, and the generated executable The file is larger and can be run without the static library file.

2.1.1 Generation methodFirst create two source files fred.c and bill.c

What is the connection between libraries and header files in Linux?

What is the connection between libraries and header files in Linux?Then use

gcc -c

to compile and generate two target files

What is the connection between libraries and header files in Linux?Then for these two Write a header file for each function, make a function declaration

What is the connection between libraries and header files in Linux?Then include the header file in the main function and call one of the library functions

What is the connection between libraries and header files in Linux?Then compile the main function and link main.o with the previously generated bill.o, which can be executed correctly

What is the connection between libraries and header files in Linux?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.

What is the connection between libraries and header files in Linux?Then we link the library file with the previously generated program.o file and the execution is successful

What is the connection between libraries and header files in Linux?Also Use

-l

to specify function libraries, and -L to specify non-standard locations.

What is the connection between libraries and header files in Linux?

We can use the

nm command to view which functions are included in the target file, function library or executable file, 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.

What is the connection between libraries and header files in Linux?

1What is the connection between libraries and header files in Linux?

Comparison of static libraries between Linux and Windows

1What is the connection between libraries and header files in Linux?

2.2 Shared library

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.
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

Use the intermediate file bill.o fred.o to generate the shared library libfoo.so gcc -shared -fpic -o libfoo.so bill.o fred .o

1What is the connection between libraries and header files in Linux?

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 /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

1What is the connection between libraries and header files in Linux?

Then specify the search path

1What is the connection between libraries and header files in Linux?

Link to generate executable filegcc -o main program.o -lfoo -L.

1What is the connection between libraries and header files in Linux?

lddTools can be used for analysis Shared libraries required to run the executable file

1What is the connection between libraries and header files in Linux?

You can also specify the search path of the .so library when compiling the target code, through the gcc parameter-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 ":".

1What is the connection between libraries and header files in Linux?

Related recommendations: "Linux Video Tutorial"

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template