Linux Build Dynamic library system
One, explain
The dynamic library file under Linux has the extension ". So" (Shared Object).In accordance with the Convention, all dynamic library file names are in the form of libname.so (which may be added in the name).
Such The thread function library is called libthread.so.
The file name form of the static library is libname.a. The file name form of the shared archive is libname.sa. Shared archive is just a transitional form that helps people transform from static libraries to dynamic libraries.
  II, library file and test file code library file and test file folder:/home/test/program/ 1, library file name: MYLIBSRC.C
/*
Filename:mylibsrc.c
*/
#include <stdio.h>
#include "myLibInclude.h"
Mylibsrcfun () {
printf ("There is mylibsrcfun () \ n");
return 0;
}
2. test file: main.c
/*
Filename:main.c
*/
#include <stdio.h>
#include "myLibInclude.h"
int main (INTARGC, char** argv) {
Calling functions in the loaded dynamic library
return 0;
}
3. header file: MyLibInclude.h
/*
Filename:mylibinclude.h
*/
Intmylibsrcfun ();//Declare the function
Three, the compilation method of dynamic library The Compile library file MYLIBSRC.C commands such as the following:
$ gcc mylibsrc.c-shared-o libmylib.so or: $ gcc mylibsrc.c-fpic-shared-g-ddebug-o libmylib.so
Assuming the compilation succeeds, the dynamic library file is generated under folder/home/test/program/: libmylib.so
Here are two points to add: A, for Linux operations, generally recommended in ordinary user mode, assuming that the power of the superuser, you can use sudo or Su root, enter the root user password switch.Since individual learning is used, it is necessary to use the root user at the same time for very many operations, so it is compiled directly under the root user.
B. The meaning of the reference when compiling a dynamic library-fpic: Enables the output object module to reposition address mode.
-shared: Specifies that the corresponding source file is generated for the corresponding dynamic link library file. Four, the dynamic library test method compiled test file: main.c
$GCC –o App Main.c/home/test/program/libmylib.so
Executed after successful compilation./app:
Main function! there is mylibsrcfun () Note: 1. Commands compiled above $GCC –o app main.c/home/test/program/libmylib.so The last parameter of the is the absolute path that specifies the specific connected library file. The absolute path to the library file in this example is/home/test/program/libmylib.so of course, false assumptions from the system's library file path (usually the system function library is located/ Usr/lib folder), it is possible to copy the generated library files to the/usr/lib before linking them to a dynamic library. Then on the link: $CP libmylib.so /usr/lib/libmylib.so $gcc –o app main.c -lmylib
Here, a simple explanation for the link method: –lmylib for the last parameter in the Gcc–o app Main.c-lmylib. The command-line parameters of the compiler passed to C do not refer to the full path of the library. Not even mention the full name of the file in the Library folder!In fact, the compiler is told to link to the corresponding library of functions based on the option-lmylib (/usr/lib), the library name is libmylib.so, that is. The "Lib" section and the file extension are omitted. But I added an ' l ' to the front.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Linux Build Dynamic library system