File. Ko,. O,. So,. A,. La in Linux

Source: Internet
Author: User

In Linux, the file type does not depend on its suffix, but generally speaking:

  • . Ko is the suffix of the dynamic connection file used by the Linux 2.6 kernel, that is, the module File, used to load the kernel module when the Linux system starts.
  • . O is the target file, which is equivalent to the. OBJ file in windows.
  • . So is a shared library and is a shared object, used for dynamic connection, similar to DLL
  • . A is a static database and multiple. O files are combined for static connections.
  • . LA is a shared library automatically generated by libtool. It is edited and viewed by VI and mainly records some configuration information. You can run the following command to View File *. La to view the file type:

Create the. A library file and. O library file:
$ Gcc-C mylib. c
$ Ar-r mylib. A mylib. o

Dynamic Link Library *. So compilation and use

1. Dynamic library Compilation
The following example shows how to generate a dynamic library. There is a header file, three. c files:

So_test.h

Test_a.c

Test_ B .c

Test_c.c

We will compile these files into a dynamic library: libtest. So.

 

Code

 so_test.h:
#include <stdio.h>
#include <stdlib.h>
void test_a();
void test_b();
void test_c();

test_a.c:
#include \"so_test.h\"
void test_a()
{
printf(\"this is in test_a...\\n\");
}

test_b.c:
#include \"so_test.h\"
void test_b()
{
printf(\"this is in test_b...\\n\");
}

test_c.c:
#include \"so_test.h\"
void test_c()
{
printf(\"this is in test_c...\\n\");
}

$ GCC test_a.c test_ B .c test_c.c-FPIC-shared-O libtest. So

2. Dynamic library links
In 1, we have successfully generated a dynamic link library libtest. So. Below we call this through a program
Library functions. The source file of the program is test. C.

#include \"so_test.h\"
int main()
{
test_a();
test_b();
test_c();
return 0;
}

 
Link test. C to the dynamic library libtest. So to generate the execution file test:
$ GCC test. C-L.-ltest-o Test
Test whether to dynamically connect. If libtest. So is listed, the connection is normal.
$ LDD Test

At this time, it should be reported that libtest. So cannot be found. Here we will execute:

$ Sudo CP libtest. So/usr/lib

Copy this library to the default library path of the system. This is only for temporary testing. For more reasonable methods, refer to the introduction below.

Run test to see how it calls functions in the dynamic library.

3. Compile Parameter Parsing
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, sometimes also generate
Weak Link W type export symbol). External programs cannot connect without this symbol. Equivalent to an executable file
-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related.
The code is copied to meet the needs of different processes, but cannot achieve the purpose of sharing real code segments.
-L.: indicates that the database to be connected is in the current directory.
-Ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.
LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.
If you have the root permission, you can modify the/etc/lD. So. conf file and call/sbin/ldconfig
Achieve the same purpose, but if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.

4. Note
When calling a dynamic library, there are several problems that may occur frequently. Sometimes, it is clear that the directory where the header file of the library is located has passed the "-I
"Include in. The file where the library is located is guided by the"-l "parameter and the database name of"-l "is specified. However,
You cannot find the so file with the specified link. In this case, you need to modify the LD_LIBRARY_PATH or
/Etc/lD. So. conf file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.
In makefile, how to correctly compile and connect to generate the. So library file, and then how to compile it in makefile of other programs?
And connection to call the function of this library file ????
A:
You need to tell the dynamic linker and the Loader where LD. So can be found. You can set environment variables
Path to the library directory/lib and/usr/lib, LD_LIBRARY_PATH = $ (PWD). This method is not convenient using the command line method.
Generation Method
^ ^
LD_LIBRARY_PATH can be in/etc/profile or ~ /. Profile or./bash_profile or. bashrc
,
Run source/etc/profile or./etc/profile after modification.
A better way is to add/etc/lD. So. conf and then execute/sbin/ldconfig
^ ^
Is to add the library path to/etc/lD. So. conf, and then run ldconfig as root
You can also specify the file path and name-I-l.
GCC = gcc
Cflags =-wall-ggdb-FPIC
# Cflags =
ALL: libfunc Test
Libfunc: func. O func1.o
$ (GCC)-shared-wl,-soname, libfunc. so.1-O libfunc. so.1.1 $ <
Ln-SF libfunc. so.1.1 libfunc. so.1
Ln-SF libfunc. so.1 libfunc. So
**************************************** * ****** Comment
**************************************** ********
Ln-S is used to create soft links, which is equivalent to a shortcut in windows. In the current directory, create files in the upper-level directory
The command named ttt2 soft link in TTT is ln-S ../TTT ttt2. If the original file is deleted, ttt2 becomes
Empty file.
Ln-D is used to create hard links, which is equivalent to a copy of files in windows. When the original file is deleted
.
When compiling the target file, use the-FPIC option of GCC to generate location-independent code and load it to any address:
Gcc-FPIC-g-C liberr. C-o liberr. o
Use the-shared and-soname options of GCC;
Use the-wl option of GCC to pass the parameter to the connector LD;
Use the-L option of GCC to display the connection to the C library to ensure that the required startup code can be obtained.
Different versions may not be compatible with the C library.
Gcc-g-shared-wl,-soname, liberr. So-O liberr. so.1.0.0 liberr. O-lC
Create a symbolic connection:
Ln-s liberr. so.1.0.0 liberr. so.1;
Ln-s liberr. so.1.0.0 liberr. So;
In makefile:
$ @
Indicates the target file set in the rule. In a pattern rule, if multiple targets exist, \ "$ @ \" matches the target pattern.
Set.
$ %
Only when the target is in the function library file, it indicates the target member name in the rule. For example, if a target is \ "foo. A (bar. O )\",
Then, \ "$ % \" is \ "bar. O \", \ "$ @ \" is \ "foo. \". If the target file is not a function library file ([. A] in UNIX
[. Lib]). The value is null.
$ <
The first target name in the dependency target. If the dependency target is defined in the mode (\ "% \"), \ "$ <\" will be the compliance Model
A series of file sets. Note that it is obtained one by one.
$?
A set of new dependent targets. Separated by spaces.
$ ^
A set of all dependent targets. Separated by spaces. If there are multiple duplicates in the dependency target, the variable will remove duplicates.
Only one copy is retained.
**************************************** * ***** Comment
**************************************** *******************************
Test: Test. O libfunc
$ (GCC)-O test. O-L.-lfunc
%. O: %. c
$ (GCC)-C $ (cflags)-o $ @ $ <
Clean:
Rm-Fr *. o
Rm-Fr *. So *
Rm-fr Test
To generate the. So file, CC must contain the-shared parameter; To call the. So file, such as libfunc. So, you can add it at the end of the CC command
-Lfunc: Add-L/usr/xxx to indicate the path of libfunc. So that you can call
The function of the library file libfunc. So.
I mentioned above, but I 'd like to remind you that it is best to provide an interface header file.
Dynamic Loading, with dlopen, dlclose, dlsym

Related Article

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.