Storehouse
Environment is CentOS 7
Library file : file does not contain the function main function (which has .c and .h files)
Since the file is a header file .h .c file, pre-loading process directly into the .c, so when generating the library, the process only needs to .c file
At the presentation of the library file is main.c generated dynamic library files are libchild.so generated static library files are libchild.a
Library file naming convention
Dynamic library files |
Static library files |
lib + filename + .so |
lib + filename + .a |
Build the library
Generate dynamic library |
Generate static library |
gcc -c main.c -o main.o [only when the file is compiled into machine language program, the link to the next step of the operation] |
|
gcc --share main.o -o llibchild.so |
ar -cr libchild.a main.o |
Use the library
Library search path when generating an executable program
|
Examples of command | Command interpreter |
1, when generating an executable program, as required third-party libraries, third party libraries need to be placed in the specified path / lib64 / usr / lib64 |
gcc main.c -o mian -lmychild |
To a main.c file to generate an executable program of a main, -o is specified to generate an executable program name. -l followed by library name (library file name is [lib name .so]), where the child is the library name |
2, you can specify an environment variable expert LIBRARY_PATH =. To specify the search path for library files |
expert LIBRARY_PATH = path |
|
3, may also be a link path -L option to specify the library file gcc |
gcc main.c -o mian -L . -lmychild |
The following pay more to the search path] [-L |
Executing the executable search path for library file (using the executable program generated a static library does not need to load the library file)
1, the program is running, you need to specify the directory under dynamic loading / lib64 / usr / lib64 ······ |
The library files placed in the / lib64 / usr / lib64 ······ |
2, you can also use expert LD_LIBRARY_PATH =. To set the environment variable to declare the library load path when the program runs |
expert LD_LIBRARY_PATH = path environment variable added |