Article Title: compile your own library file practices in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
In linux, the file type does not depend on its suffix, but generally speaking:
. 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 mainly records some configuration information.
1. create static. o library files and. a library files
[Root @ localhost study] # mkdir libtest
[Root @ localhost study] # ls
Cc. c hello hello1 hello2 libtest
[Root @ localhost study] # cd libtest/
[Root @ localhost libtest] # pwd
/Home/a/study/libtest
[Root @ localhost libtest] # vim mylib. c
[Root @ localhost libtest] # vim mylib. h
[Root @ localhost libtest] # ls
Mylib. c mylib. h
[Root @ localhost libtest] # cat mylib. c
# Include
Void hello ()
{
Printf ("This is my lib. \ n ");
}
[Root @ localhost libtest] # cat mylib. h
Extern void hello ();
[Root @ localhost libtest] # vim test. c
[Root @ localhost libtest] # ls
Mylib. c mylib. h test. c
[Root @ localhost libtest] # cat test. c
# Include "mylib. h"
Int main ()
{
Hello ();
Return 0;
}
[Root @ localhost libtest] # gcc-Wall-g-c-o mylib. o mylib. c
[Root @ localhost libtest] # ls
Mylib. c mylib. h mylib. o test. c
[Root @ localhost libtest] # ar rcs mylib. a mylib. o
[Root @ localhost libtest] # gcc-Wall-g-c test. c-o test. o
[Root @ localhost libtest] # ls
Mylib. a mylib. c mylib. h mylib. o test. c test. o
[Root @ localhost libtest] # gcc-g-o test. o-L.-lmylib
/Usr/bin/ld: cannot find-lmylib
Collect2: ld returns 1
[Root @ localhost libtest] # ls
Mylib. a mylib. c mylib. h mylib. o test. c test. o
The name must start with lib, so ,:-)
[Root @ localhost libtest] # mv mylib. a libmylib.
[Root @ localhost libtest] # ls
Libmylib. a mylib. c mylib. h mylib. o test. c test. o
[Root @ localhost libtest] # gcc-g-o test. o-L.-lmylib
[Root @ localhost libtest] # ls
Libmylib. a mylib. c mylib. h mylib. o test. c test. o
[Root @ localhost libtest] #./test
This is my lib.
[Root @ localhost libtest] #
2. Dynamic Link Library *. so file
(1) dynamic library Compilation
[Root @ localhost study] # ls
Cc. c hello hello1 hello2 libtest
[Root @ localhost study] # mkdir sharelib
[Root @ localhost study] # ls
Cc. c hello hello1 hello2 libtest sharelib
[Root @ localhost study] # cd sharelib/
[Root @ localhost sharelib] # vim so_test.h
[Root @ localhost sharelib] # cat so_test.h
Extern void test_a ();
Extern void test_ B ();
Extern void test_c ();
[Root @ localhost sharelib] # vim test_a.c
[Root @ localhost sharelib] # vim test_ B .c
[Root @ localhost sharelib] # vim test_c.c
[Root @ localhost sharelib] # cat test_a.c
# Include
# Include "so_test.h"
Void test_a ()
{
Printf ("This is in test_a... \ n ");
}
[1] [2] [3] Next page