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

Source: Internet
Author: User
Tags sin

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. "This example explains"
This example shows how to add a shared library to the makefile file.

1.GCC ReviewTo increase the shared library using the-l prefix, note that this must be lowercase. "1" Under certain circumstances, the system default search library directory is located in/usr/lib and Lib "2" if the shared library is not in the system default library, you can specify the directory through the-l parameter. Note that the parameter is valid only during the link process.
2. Source FilesThe source file is very simple and prints the result of sin (30).    Because of using the math library, we need to introduce M.A shared libraries. Note that the sin and COS functions use radians, and the passed arguments are of type double, and the LF prefix is required for printf.
#include <stdio.h> #include <math.h> #define PI 3.14159265int Main (void) {    double angle = 30.00;     printf ("Sin (%.2lf) =%.2lf\n", Angle, sin (angle * pi/180));    return 0;}

3.makefileReplace [tab] with the makefile file in the code warehouse as the main
# directive compiler and Option CC = Gcccflags =-wall-std=gnu99# executable file target=test# c file SRCs = test.c# destination File Objs = $ (SRCS:.C=.O) # library file Dlibs =-lm# Link as executable $ (TARGET): $ (OBJS) # @echo Target:[email protected]# @echo objects:$^[tab]$ (CC)-o [email protected] $^ $ (dlibs) CLEAN:[TAB]RM-RF $ (target) $ (OBJS) # Continuous action, clear the recompile link, and finally execute Exec:clean $ (target) [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)-o [email protected]-C $<

4. Specific Instructions"1" dlibs =-lm adds a shared library. Please note that the actual library full name islibm.so。 "2" $ (CC)-o [email protected] $^ $ (dlibs) Add shared library when compiling.    Note that the shared library is added during the link process and does not need to be added during the compilation process. "3" Because libm.so is a system library, you do not need to specify a path. The default lookup paths for shared libraries in Linux systems are/lib and/usr/lib
5. Execution Process"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)
From the console output can be seen, successfully linked to the system shared library libm.so"Compiling and linking"Make clean && make"Console Output"RM-RF Test TEST.O
Gcc-wall-std=gnu99-o test.o-c test.c
Gcc-o Test TEST.O-LM
From the console output, you can see that the library file was added during the link."Execute"./test
Sin (30.00) =0.50 The results of the execution are normal and match the expected effect.
6. Summary"1" uses the prefix-l when adding system shared libraries. The "2" system shared library does not need to specify a path.

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.