Relatively simple generic Makefile for C + + program__c++

Source: Internet
Author: User
relatively simple generic Makefile for C + + program

Reference to the online data written by a simple can compile C + + mixed engineering Makefile "online most of the general makefile very long and no explanation, so write this document"

Have not encountered problems, I am not very proficient in the document without the actual use of temper, if you have questions please contact:liyanbin0027@163.com Wall crack welcome correction and exchange.

This makefie needs the reader to have a certain makefile basic knowledge

This makefile can realize the multi-level catalog compilation, the user needs to set up according to own project, the main setting variable is as follows:
# cflags/cxxflags is used to set compiler parameters, header file path "including dynamic library header file path if used to"
# ldflags is used to set the path of the dynamic library file if used
# Srcdirs is used to set the folder where the source code resides
# srcexts is used to set the type of file used in the project, such as. c. cxx -Results Demo

First set up the file as follows:

The contents of the documents are as follows:

hello.c

/* File name:hello.c
* C source file */
#include "hello.h"
#include "stdio.h"
void Print_hell O ()
{
	printf ("Hello, world!\n");
}

Hello.h

/* File name:hello.h
* C header file */
#ifndef hello_h
#define HELLO_H
#ifdef _ _cplusplus
extern "C" {
#endif
void Print_hello ();
#ifdef __cplusplus
}
#endif
#endif

Main.cxx

/* File naem:main.cxx
* C + + source file *
/#include "hello.h"
int main ()
{
	Print_hello ();
	return 0;
}

Makefile

############################################################################# # # Generic Makefile for C + + program #

# author:yanbin Lee # date:2017/07/05 #============================================================================
# The C + + program compiler. CC=GCC cxx=g++ # compiler parameter settings at compile time, include header file path settings cflags:=-wall-o2-g cflags+=-i $ (shell pwd)/include cxxflags:=-wall-o2-g CXXFL
Ags+=-i $ (shell pwd)/include # library file add ldflags:= ldflags+= # Specifies where the source program is stored srcdirs:=. The Srcdirs+=dir # Setup program uses the file type srcexts:=.c. CXX # Set Run program name Program:=hello #t1 =$ (Addprefix $ (srcdirs)/*,$ (srcexts)) #t2 =$ (for Each d,$ (srcdirs), echo $ (d)) #t3 =$ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts))) #all: # @echo $ (T3) S ources=$ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts))) objs=$ (foreach x,$ (srcexts), $ (Patsubst%$ ( x),%.o,$ (filter%$ (x), $ (SOURCES))). Phony:all Clean Distclean Install%.O:%.C $ (CC)-C $ (cflags)-O $@ $<%.o:%.cxx $ (CXX)-C $ (cxxflags)-O $@ $& Lt


$ (Program): $ (OBJS) ifeq ($ (Strip $ (srcexts)),. c) $ (CC)-O $ (program) $ (OBJS) $ (ldflags) Else $ (CXX)-O $ (program) $ ( OBJS) $ (ldflags) endif install:install-m 755-d-P $ (program)./bin clean:rm-f $ (Shell find-name "*.O") rm-f $ (program) Distclean:rm-f $ (Shell find-name "*.O") Rm-f $ (Shell find-name "*.D") rm-f $ [Program] All: @echo $ (







 OBJS)


This sample engineering code can be downloaded here: Click on the Open link


Get root permission, enter make to get the executable file Hello, and then run:

1. Partial makefile function

This section describes some of the functions that are used, and a complete description of the function can refer to:

"Makefile Common function Summary" http://blog.csdn.net/ustc_dylan/article/details/6963248

filter: Filtering functions

Sources: = foo.c bar.c baz.s ugh.h

$ (Filter%.c%.s,$ (sources))

Number return value is "foo.c bar.c baz.s"

patsubst: pattern substitution function

$ (patsubst%.c,%.o,x.c.c bar.c)

#把字串 "X.C.C bar.c" to replace the words ending with. C with characters ending with. O. The return result of the function is "X.C.O bar.o"

wildcard: pattern matching filename

In makefile, it is expanded to a list of all files that already exist, separated by spaces, that match this pattern

foreach: Loop function

$ (foreach Var,list,text)

When executed, the word in the "list" that is separated by a space is assigned to the variable "VAR" and then the "TEXT" expression is executed.

A.O B.O

string: Go to the space function, remove the null characters from the beginning and end of the <string> string

$ (Strip a B c)

The string "abc" goes to the opening and ending spaces, and the result is "ABC".

$ (findstring <find>,<in>)

addprefix : Add prefix function

$ (Addprefix src/,foo bar) return value is "Src/foosrc/bar"

2. Difficult explanation

It may be more difficult to understand in this makefile that "SOURCES = $ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts)))" It is more difficult to understand purely from a grammatical understanding, can be gradually disassembled

Test 1

New File under Folder

root@ubuntu:/home/share/task002# ls

HELLO.C hello.h Makefile Mian.cxx

Makefile:

srcdirs:=.

Srcexts:=.c. cxx

t1=$ (Addprefix $ (srcdirs)/*,$ (srcexts))

All

@echo $ (T1)

Output:./hello.c./mian.cxx(first thought the output would be./*.c,./*cxx)

Test 2

Add a dir folder based on test 1

srcdirs:=.

Srcdirs+=dir

t2=$ (foreach d,$ (srcdirs), echo $ (d))

All

@echo $ (T2)

Output: Echo. echo dir "Note that the results are not obtained by all, but by a foreach rule."

Test 3

Add a A.C file based on test 2

srcdirs:=.

Srcdirs+=dir

Srcexts:=.c. cxx

t3=$ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts)))

All

@echo $ (T3)

Output:./hello.c./mian.cxx dir/a.c ( simple to understand difficult, or test yourself more reasonable )

Test 4

similarly to objs=$ (foreach x,$ (srcexts), $ (Patsubst%$ (x),%.o,$ (filter%$ (x), $ (SOURCES))) is processed, and a new file is created in the dir directory a.o

Final output:./hello.o dir/a.o./MIAN.O

3. Matters needing attention

L Note the space when editing in Windows, pay attention to the TAB key, note the line break, these are pits

L time is limited, makefile files are not tested when adding dynamic libraries

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.