The site of the transfer is: http://www.cnblogs.com/fengyv/archive/2012/08/10/2631313.html
The type of file under Linux is not dependent on the suffix, but generally:
. O, which is the target file, equivalent to the. obj file in Windows
. So is a shared object, which is used for dynamically connected, similar to DLLs
. A is a static library and is a good number of. O Together for static connection
. LA for Libtool auto-generated some shared libraries, vi edit view, mainly recorded some configuration information. You can use the following command to view
*.la file Formats $file *.la
*.la:ascii 中文版 Text
So you can use VI to view its contents.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Create the. A library file and the. O Library file:
[Email protected] perl_c2]$ pwd
/home/yufei/perl_c2
[email protected] perl_c2]$ cat MYLIB.C
#include <stdio.h>
#include <string.h>
void Hello () {
printf ("Success call from Perl to C library\n");
}
[email protected] perl_c2]$ cat Mylib.h
extern void Hello ();
[Email protected] perl_c2]$ gcc-c MYLIB.C
[Email protected] perl_c2]$ dir
MYLIB.C Mylib.h MYLIB.O
[Email protected] perl_c2]$ ar-r MYLIB.A MYLIB.O
AR: Creating MYLIB.A
[Email protected] perl_c2]$ dir
MYLIB.A MYLIB.C mylib.h MYLIB.O
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111
Compilation and use of dynamic link library *.so--
Dynamic library *.so in Linux under the C and C + + programming often encounter, recently found several articles on the Web site to introduce the compilation and chain of dynamic library
Then, finally understand this has been not very familiar with the east, here to make a note, but also for other dynamic library linked to the library and distressed brother
Provide a little help.
1, the compilation of dynamic library
Here's an example of how to build a dynamic library. Here is a header file: So_test.h, three. c Files:
TEST_A.C, TEST_B.C, TEST_C.C, we compile these files into a dynamic library: libtest.so.
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 was in test_a...\n");
}
TEST_B.C:
#include "So_test.h"
void Test_b ()
{
printf ("This was in test_b...\n");
}
TEST_C.C:
#include "So_test.h"
void Test_c ()
{
printf ("This was in test_c...\n");
}
Compile these files into a dynamic library: libtest.so
$ gcc test_a.c test_b.c test_c.c-fpic-shared-o libtest.so
2, the dynamic library link
In 1, we have successfully generated a dynamic link library of our own libtest.so, below we have a program to call this
A function in a library. The source file for the program is: test.c.
TEST.C:
#include "So_test.h"
int main ()
{
Test_a ();
Test_b ();
Test_c ();
return 0;
}
L deliver test.c and dynamic library libtest.so to execute file test:
$ gcc test.c-l.-ltest-o Test
L Test whether the connection is dynamic, if the libtest.so is listed, then it should be connected properly.
$ LDD Test
L Execute test and you can see how it calls functions in the dynamic library.
3. Compilation parameter parsing
The main thing is an option for the GCC command line:
-shared This option specifies to generate a dynamic connection library (let the connector generate an export symbol table of type T, and sometimes generate
Weak connection w type of the export symbol), do not use this flag external program cannot connect. Equivalent to an executable file
L-fpic: A code that is compiled to be location-independent, without this option, the compiled code is location-dependent, so move
State loading is done by means of code copies to meet the needs of different processes, rather than to achieve the purpose of true code segment sharing.
L-l.: Indicates the library to be connected in the current directory
L-ltest: There is an implicit naming convention when the compiler looks for a dynamic connection library, which is to precede the given name with Lib, and then
Add. So to determine the name of the library
L Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.
L Of course, if you have root privileges, you can modify the/etc/ld.so.conf file, and then call/sbin/ldconfig to
Achieve the same goal, but if there is no root authority, then only the output Ld_library_path method is used.
4. Attention
When calling a dynamic library, there are a few problems that often come across, and sometimes the directory where the header file of the library is located through the "-I
"Include comes in, the library file is booted with the"-L "parameter and the library name"-L "is specified, but when viewed through the LDD command,
It's just that you can't find the so file for your specified link, and you're going to do this by modifying Ld_library_path or
The/etc/ld.so.conf file to specify the directory of the Dynamic library. This is usually done to solve the problem where the library cannot be linked.
Makefile inside how to compile and connect the build. So library file, and then again in the makefile of other programs how to compile
and connect to call this library file's function????
For:
You need to tell the dynamic linker, loader ld.so where to find this shared library, you can set the environment variables to put the library's
The path is added to the library directory/lib and/usr/lib,ld_library_path=$ (PWD), a method that uses the command line method is not very convenient, a
Generation method
^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ the ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^ Comments ^ ^^ ^^ ^^ The ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^
Ld_library_path can be set in/etc/profile or ~/.profile or./bash_profile, or. BASHRC
,
Run Source/etc/profile or after the change is complete. /etc/profile
A better approach is to add/etc/ld.so.conf and then execute/sbin/ldconfig
^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ the ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^ Comments ^ ^^ ^^ ^^ The ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^ ^
is to add the library path to/etc/ld.so.conf and then run as root ldconfig
You can also specify the file path and name-i-l at the time of connection.
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
Comments
************************************************
Ln-s is used to create a soft link, which is equivalent to a shortcut in Windows, to create a file in the current directory in the top level directory
The TTT named Ttt2 soft-Link command is ln-s. /TTT Ttt2, if the original file is the TTT file deletion, Ttt2 also become
Empty file.
Ln-d is used to create a hard link, which is equivalent to a copy of a file in Windows, and when the original file is deleted, it does not affect the "copy
"Content.
When compiling the target file, use the GCC-fpic option to generate location-independent code and can be loaded to any address:
Gcc–fpic–g–c Liberr.c–o LIBERR.O
Use GCC's-shared and-soname options;
Use GCC's-wl option to pass parameters to the connector ld;
The connection C library is displayed using the-l option of GCC to ensure that the required startup code is available, thus avoiding the program
Execution cannot be started on a system with a different, potentially incompatible version of the C library.
Gcc–g–shared–wl,-soname,liberr.so–o liberr.so.1.0.0 LIBERR.O–LC
Establish the appropriate symbolic connection:
Ln–s liberr.so.1.0.0 liberr.so.1;
Ln–s liberr.so.1.0.0 liberr.so;
In the makefile:
[Email protected]
Represents a set of target files in a rule. In a pattern rule, if there are multiple targets, then "[email protected]" is matched to the pattern in the target
The collection of definitions.
$%
Represents the target member name in a rule only if the target is in a function library file. For example, if a target is "foo.a (BAR.O)",
Then, "$%" is "BAR.O", "[email protected]" is "FOO.A". If the target is not a function library file (under UNIX is [. A],windows is
[. lib]), then its value is null.
$<
The first target name in a dependent target. If the dependent target is defined in a pattern (that is, "%"), then "$<" will be compliant with modulo
A series of file sets of the type. Note that it is taken out one by one.
$?
A collection of all new dependent targets than the target. separated by a space.
$^
A collection of all dependent targets. separated by a space. If there are multiple duplicates in the dependent target, that variable will remove the heavy
Target, and only one copy is retained.
Comments
***********************************************************************
TEST:TEST.O Libfunc
$ (GCC)-O test test.o-l.-lfunc
%.o:%.c
$ (GCC)-C $ (CFLAGS)-o [email protected] $<
Clean
RM-FR *.O
RM-FR *.so*
RM-FR Test
To generate the. so file, CC takes the-shared parameter; to invoke the. So file, such as libfunc.so, you can add the CC command at the end of the
-lfunc, also depending on the situation plus-l/usr/xxx point libfunc.so path, so that in the source file you want to compile can call
Libfunc.so The function of this library file.
The front of all said almost, finally reminded that it is best to provide an oral document
Dynamic loading, with Dlopen,dlclose,dlsym
Dynamic link library. So file static link library. A file creation and use