Linux Learning Notes--Makefile Add custom shared libraries, for example __linux

Source: Internet
Author: User
0. The preface starts from the study C language to begin slowly to contact Makefile, has consulted many makefile's data but the total feeling does not have the real mastery makefile, if oneself writes a Makefile always feels very laborious.     Therefore, the use of blog to summarize the relevant knowledge of makefile, through examples to illustrate the specific use of makefile.     For example, makefile is divided into the following sections, for more information please refer to "Example said Makefile Index Bowen" 1. Only a single C file 2 contains multiple C file 3. Need to include header file path 4. Add macro definition 5. Increase System Share Library 6. Add custom Shared Library 7. A practical example
The Code warehouse--makefile-example code warehouse is located in BitBucket, using TORTOISEHG (GUI tools) to clone code or download a zip package directly from a Web page. "This example illustrates" this example is divided into two procedures, first making a shared library, and then using the shared library.      Shared libraries are dynamically linked, with a suffix of. So.     1.GCC review "1" GCC directives use a shared library using the prefix-l, the shared library lookup directory uses the-L. "2" in general Dlibs to join the shared library, and Ldflags to join the shared library lookup directory.
2. Shared library source files and build shared libraries are located in the Libtest directory, and at the end of makefile the shared library is replicated to the Lib directory of the sibling.     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_H
int 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-b;
}

3. Makefile of shared libraries replace [tab] with the makefile file in your code warehouse.
# directive compiler and option
CC = gcc
cflags =-wall-std=gnu99

# target file
= libtest.so
# c file
SRCs = test-a DD.C TEST-SUB.C
# target file
Objs = $ (srcs:.c=.o)

# link to executable
$ (target): $ (OBJS)
[tab]$ (CC)-shared -O $@ $^
[TAB]MV $ (TARGET). /lib clean

:
[TAB]RM-RF $ (target) $ (OBJS)

# Compilation rule $@ represents the target file $< represents the first dependent file
%.o:%.c
[tab]$ (CC) $ (cflags)-O $@-fpic-c $<

"description" "1" target is libtest.so, be sure to start with Lib. So, the system automatically removes Lib and. So when using the shared library, written as a-ltest when used.     "2" Most content is the same as multiple files in a learning note.     "3" adds the-shared parameter to the link process, meaning the destination file for the shared form.     "4" adds the-fpic parameter to the compilation process, meaning the generated and address-independent target file.     "5" After the link completes, moves the libtest.so through the MV instruction to the sibling's Lib directory. "6" if necessary, the libtest.so can be copied to the/usr/lib directory, so it will be more convenient to use.
Authentication
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)

Compile and execute 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 to the "2" link process.
The "3" shared library is libtest.so.
"4" Finally, the shared library is moved to the Lib directory at the previous level.
4. The source file invokes 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.makefile Replace [tab] with the makefile file in the code warehouse.
# directive compiler and option
CC = gcc
cflags =-wall-std=gnu99

# target file
= Test
# c file
SRCs = test.c
# header File Lookup path
INC =-I.
# library file and library lookup path
dlibs =-ltest
ldflags =-l./lib
# Specifies runtime library file path
rpath =-wl,-rpath=./lib

# destination file
OBJS = $ (srcs:.c=.o)

# link to executable
$ (TARGET): $ (OBJS)
[tab]$ (CC)-$@ $^ $ (ldflags) $ (dlibs) $ (rpath) Clean

:
[Tab]rm-rf $ (target) $ (OBJS)

# Continuous action, clear the Recompile link, and then execute
Exec:clean $ (target)
[tab] @echo begin execution
[tab]./$ (TARGET)
[tab] @echo execution End

# compilation rule $@ represents the target file $< represents the first dependent file
%.o:%.c
[tab]$ (CC) $ (cflags) $ (INC)-O $@-C $<

6. Specify the "1" dlibs =-ltest Specifies a shared library, note that the name of the shared library is libtest.so, and the-l parameter takes only the test section and removes the prefix lib and suffix. So.     "2" ldflags =-l./lib Specifies a shared library path, note that the shared library has been replicated to the Lib directory in the previous step.     The "3" INC =-i./lib Specifies the libtest.h directory, or it can copy libtest.h to the directory where test.c resides.     The "4" $ (CC)-O $@ $^ $ (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 link process, and the shared library path needs to be specified through-wl,-rpath=<path> during execution. Compile and link 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 can also see if libtest link was successful by LDD test prior to execution .../test
A=3
b=2
A+b=5
A-b=1 the output from the console shows that the shared library is running normally.
7. The summary "1"-l specifies the shared library name,-l specifies the shared library path.
8. Reference "1" gcc-wl,rpath=<your_lib_dir> option "2" Makefile Learning Experience (iii)----compiling a dynamic library file (mode i)

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.