Makefile common universal templates (including static link library, dynamic link library, executable file)

Source: Internet
Author: User

This paper divides makefile into three parts: generate makefile of executable file, generate makefile of static link library, generate makefile of dynamic link library.

These makefile are simple, they are usually used at a glance, and are easy to use, just copy them to the same directory as your code and then use make to generate the target files.

Here is the source code for the three makefile:

1. Makefile to generate executable files
##############################################################################source File#source files, automatically find all. C and. cpp files, and define the target as the same name. o FileSOURCE: = $ (wildcard *.c) $ (wildcard *. cpp) OBJS:= $ (Patsubst%.c,%.o,$ (Patsubst%.cpp,%. o,$ (SOURCE))) #target can change test to what do you want#target file name, enter any executable file name you wantTARGET: =Test#compile and lib parameter#Compile ParametersCC: =Gcclibs:=Ldflags:=defines:=INCLUDE:= -I.cflags:=-g-wall-O3 $ (defines) $ (INCLUDE) Cxxflags:= $ (CFLAGS)-Dhave_config_h#I think you should does anything here#The following basically does not need to make any changes.. Phony:everything Objs Clean Veryclean rebuild everything: $ (target) All: $ (target) OBJS: $ (OBJS) rebuild:verycle An everything clean:rm-FR *. So RM-FR *. O Veryclean:clean rm-FR $ (target) $ (target): $ (OBJS) $ (CC) $ (cxxflags)-O [email protected] $ (OBJS) $ (ldflags) $ (LIBS)

2, generate static link library makefile
##############################################################################  #target can change test to what do you want#Shared library file name, lib*.aTARGET: =libtest.a#compile and lib parameter#Compile ParametersCC: =Gccar=Arranlib=Ranliblibs:=Ldflags:=defines:=INCLUDE:= -I.cflags:=-g-wall-O3 $ (defines) $ (INCLUDE) Cxxflags:= $ (CFLAGS)-Dhave_config_h#I think you should does anything here#The following basically does not need to make any changes.  #source File#source files, automatically find all. C and. cpp files, and define the target as the same name. o FileSOURCE: = $ (wildcard *.c) $ (wildcard *. cpp) OBJS:= $ (Patsubst%.c,%.o,$ (Patsubst%.cpp,%. o,$ (SOURCE))) . Phony:everything Objs Clean Veryclean rebuild everything: $ (target) All: $ (target) OBJS: $ (OBJS) rebuild:verycle An everything clean:rm-FR *. O Veryclean:clean rm-Fr $ (target) $ (target) : $ (OBJS) $ (AR) Cru $ (target) $ (OBJS) $ (ranlib) $ (target)

3. Generate Makefile for dynamic link library
##############################################################################  #target can change test to what do you want#Shared library file name, lib*.soTARGET: =libtest.so#compile and lib parameter#Compile ParametersCC: =Gcclibs:=Ldflags:=defines:=INCLUDE:= -I.cflags:=-g-wall-O3 $ (defines) $ (INCLUDE) Cxxflags:= $ (CFLAGS)-Dhave_config_hshare:=-fpic-shared-o#I think you should does anything here#The following basically does not need to make any changes.  #source File#source files, automatically find all. C and. cpp files, and define the target as the same name. o FileSOURCE: = $ (wildcard *.c) $ (wildcard *. cpp) OBJS:= $ (Patsubst%.c,%.o,$ (Patsubst%.cpp,%. o,$ (SOURCE))) . Phony:everything Objs Clean Veryclean rebuild everything: $ (target) All: $ (target) OBJS: $ (OBJS) rebuild:verycle An everything clean:rm-FR *. O Veryclean:clean rm-FR $ (target) $ (target): $ (OBJS) $ (CC) $ (cxxflags) $ (SHARE) [email protected] $ (OBJS) $ (ldflags) $ (LIBS)

Makefile common universal templates (including static link library, dynamic link library, executable file)

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.