Linux Learning Notes--an example of makefile adding custom shared libraries

Source: Internet
Author: User

0. PrefaceFrom the beginning of learning C language began to contact Makefile, consulted a lot of makefile information but always feel no real master makefile, if you write a makefile always feel very laborious.    So deliberately using the blog to summarize the relevant knowledge of makefile, through examples to illustrate the specific use of makefile. The example says that makefile roughly divides into 4 parts 1. Only a single C file 2. Contains multiple C files 3. Need to include header file path 4. Add a macro definition 5. Add a system Shared library 6. Add a custom shared Library 7. A practical example of "code warehouse"--    The Makefile-example code warehouse is located in BitBucket and can be cloned with TORTOISEHG (GUI tools) or downloaded directly from a Web page. In this example, this example is divided into two processes, first making a shared library, and then using the shared library. Shared libraries are dynamically linked, with the suffix. So.1.GCC ReviewThe "1" GCC directive uses the shared library to use the prefix-l, and the shared library lookup directory uses-L. "2" generally joins the shared library in Dlibs, while Ldflags joins the shared library to find the directory.
2. Shared library source FilesThe files related to generating the shared library are located in the Libtest directory, and the shared library is finally copied to the Lib directory of the sibling in makefile.    Please refer to the Code warehouse and copy the ZIP package for details. "Libtest.h" Specifies the interface, giving the corresponding declaration
#ifndef __libtest_h#define __libtest_hint Sub (int a, int b); int add (int a, int b); #endif
"TEST-ADD.C"
int add (int a, int b) {    return a+b;}
"TEST-SUB.C"
int sub (int a, int b) {return a-a    ;}

3. Makefile of shared librariesReplace [tab] with the makefile file in the code warehouse.
# directive compiler and Option CC = gcccflags =-wall-std=gnu99# destination file target = libtest.so# c file SRCs = test-add.c test-sub.c# destination File Objs = $ (srcs:. C=.O) # Link as executable $ (target): $ (OBJS) [tab]$ (CC)-shared-o [email protected] $^[TAB]MV $ (target). /LIBCLEAN:[TAB]RM-RF $ (target) $ (OBJS) # compilation rule [email protected] on behalf of the destination file $< represents the first dependent file%.o:%.c[tab]$ (CC) $ (CFLAGS)-O [Emai L protected]-fpic-c $<

"description"The "1" target is libtest.so, be sure to start with Lib. So end, when using the shared library, the system will automaticallyRemoveLib and. So, written when used-ltest。    "2" Most content is the same as multiple files in the learning notes. "3" in the link process to add-sharedparameter, which means the target file in the shared form. "4" is added during the compilation process.-fpicparameter, which means the generated and address-independent target files.    "5" After the link is completed, the libtest.so is moved to the same Lib directory by the MV instruction. "6" If necessary, you can copy the libtest.so to the/usr/lib directory, which makes it more convenient to use.
"Validate"
LDD test
Linux-vdso.so.1 = (0x00007fffde960000)
libm.so.6 =/lib/x86_64-linux-gnu/libm.so.6 (0x00007ffe55b18000)
libc.so.6 =/lib/x86_64-linux-gnu/libc.so.6 (0x00007ffe55750000)
/lib64/ld-linux-x86-64.so.2 (0x00007ffe55e38000)

"Compiling and executing"Make clean && make
"Console Output"RM-RF libtest.so TEST-ADD.O TEST-SUB.O
Gcc-wall-std=gnu99-o TEST-ADD.O-fpic-C TEST-ADD.C
Gcc-wall-std=gnu99-o TEST-SUB.O-fpic-C TEST-SUB.C
Gcc-shared-O libtest.so test-add.o test-sub.o
MV Libtest.so. /lib
The-fpic option was added to the "1" compilation process.
The-shared option is added during the "2" link.
The "3" shared library is libtest.so.
"4" Finally, the shared library is moved to the previous level of the Lib directory.
4. Source FilesInvokes the interface specified by the shared library, add and sub functions.
#include <stdio.h> #include <libtest.h>int main (void) {    int a = 3;    int b = 2;           printf ("a=%d\n", a);    printf ("b=%d\n", b);       printf ("a+b=%d\n", add (A, b));    printf ("a-b=%d\n", Sub (A, b));    return 0;}
5.makefileReplace [tab] with the makefile file in the code warehouse.
# directive compiler and Options cc = gcccflags =-wall-std=gnu99# destination file target = test# c file SRCs = test.c# header File Lookup Path inc =-i.# library file and library lookup path dlibs =-ltes Tldflags =-l./lib# Specifies the runtime's library file path rpath =-wl,-rpath=./lib# destination File Objs = $ (SRCS:.C=.O) # link as executable $ (TARGET): $ (OBJS) [tab]$ (CC)- o [email protected] $^ $ (ldflags) $ (dlibs) $ (RPATH) CLEAN:[TAB]RM-RF $ (TARGET) $ (OBJS) # Continuous action, please clear the recompile link and finally execute Exec:clean $ (TA rget) [tab] @echo start [tab]./$ [Target] [tab] @echo execution End # compilation rule [email protected] represents the target file $< represents the first dependent file%.o:%.c[tab]$ (CC) $ ( CFLAGS) $ (INC)-o [email protected]-C $<

6. Specific Instructions"1" dlibs =-ltestSpecify a shared library, note that the name of the shared library is calledlibtest.so, and the-l parameter takes only the test part, removing the prefix lib and suffix.    "2" ldflags =-l./lib Specifies the shared library path, note that the shared library has been copied to the Lib directory in the previous step.    "3" INC =-i./lib Specifies the libtest.h directory, or you can copy the libtest.h to the directory where the TEST.C resides.    "4" $ (CC)-o [email protected] $^ $ (ldflags) $ (dlibs) Link procedure specifies the shared library lookup path, specifying the shared library name. The "5", "1" and "2" points are only valid during the linking process and need to specify the shared library path through-wl,-rpath=<path> during execution. "Compiling and linking"Make clean && make"Console Output"RM-RF Test TEST.O
Gcc-wall-std=gnu99-i./lib-o test.o-c test.c
Gcc-o Test TEST.O-l./lib-ltest-wl,-rpath=./lib
"Execute"You can also see if the Libtest link succeeded by LDD test before executing:/test
A=3
b=2
A+b=5
A-b=1 the output from the console shows that the shared library is functioning properly.
7. Summary"1"-l specifies the shared library name,-l specifies the shared library path.
8. References-wl,rpath=<your_lib_dir> option "2" in "1" GCC Makefile Learning Experience (iii)----compile to generate a dynamic library file (mode one)

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.