Makefile Learning (2) [version 2] And makefile learning Version 2
Complex instances
# Example 1: add an executable file based on the previous example 03 test [the modification has been marked in red]
. PHONY: clean all
CC = gcc
CFLAGS =-Wall-g
BIN = 01 test 02 test 03 test
SOURCES = $ (BIN: =. c)
OBJECTS = $ (BIN: =. o)
All: $ (BIN)
01 test: 01test. o
02 test: 02test. o
03 test: 03test. o
. C. o:
$ (CC) $ (CFLAGS)-c $ <-o $ @
Clean:
-Rm-rf $ (BIN) $ (OBJECTS)
# Example 2: Add a self-compiled header file pub. h for 03test in Example 1 [the modification has been marked in red]
. PHONY: clean all
CC = gcc
CFLAGS =-Wall-g
BIN = 01 test 02 test 03 test
SOURCES = $ (BIN: =. c)
OBJECTS = $ (BIN: =. o)
All: $ (BIN)
01 test: 01test. o
02 test: 02test. o
03 test: 03test. o pub. o
. C. o:
$ (CC) $ (CFLAGS)-c $ <-o $ @
Clean:
-Rm-rf $ (BIN) $ (OBJECTS)
A complex example
//1 pub.cppint pub(int a,char *ch,int b){ char c = ch[0]; switch(c) { case '+': return a+b; break; case '-': return a-b; break; case '*': return a*b; break; case '/': if (b != 0) { return a/b; } else { return -1; } break; default: return -1; break; } return 0;}
//2 pub.hint pub(int a,char *ch,int b);
//3 main.cpp#include <iostream>#include <stdlib.h>#include "pub.h"using namespace std;int main(int argc,char *argv[]){ if (argc < 4) { cerr << "error" << endl; return -1; } cout << argv[1] << ' ' << argv[2] << ' ' << argv[3] << " = " << pub(atoi(argv[1]),argv[2],atoi(argv[3])) << endl; return 0;}
#4 MacFile
. SUFFIXES:. cpp. o
CC = g ++
SOURCE = main. cpp pub. cpp
OBJC = $ (SOURCE:. cpp =. o) # Replace all. cpp files with. o files.
EXEC = mytest
All: $ (OBJC)
$ (CC)-o $ (EXEC) $ (OBJC)
. Cpp. o:
$ (CC)-o $ @-c $ <
Clean:
Rm-rf $ (OBJC)
Appendix-if a statement does not want to be printed out when running the MacFile file, add the @ symbol before it.
Professional Makefile Compilation
# Example 1
All: myapp
# Which compiler
CC = g ++
# Where are include files kept
INCLUDE =.
# Options for development
FLAGS =-g-Wall-ansi
# Options for release
# CFLAGS =-O-Wall-ansi
. SUFFIXES:. cpp. o
SOURCE = main. cpp pub. cpp
OBJC = $ (SOURCE:. cpp =. o)
EXEC = myapp
$ (EXEC): $ {OBJC}
$ (CC)-o $ (EXEC) $ (OBJC)
. Cpp. o:
$ (CC)-o $ @-I $ (INCLUDE) $ (FLAGS)-c $ <
Clean:
Rm-rf $ (OBJC) $ (EXEC)
# Example 2-added the install option
All: myapp
# Which compiler
CC = g ++
# Where to install
INSTDIR =/usr/local/bin/
# Where are inlude files kept
INCLUDE =.
# Options for development
CFLAGS =-g-Wall-ansi
# Options for release
# CFLAGS =-O-Wall-ansi
. SUFFIXES:. cpp. o
SOURCE = main. cpp pub. cpp
OBJECT = $ (SOURCE:. cpp =. o)
EXEC = myapp
$ (EXEC): $ (OBJECT)
$ (CC)-o $ (EXEC) $ (OBJECT)
. Cpp. o:
$ (CC)-I $ (INCLUDE) $ (CFLAGS)-o $ @-c $ <
Clean:
-Rm-rf $ (OBJECT)
Install: $ (EXEC)
@ If [-d $ (INSTDIR)]; then \
Cp $ (EXEC) $ (INSTDIR );\
Chmod a + x $ (INSTDIR)/$ (EXEC );\
Chmod og-w $ (INSTDIR)/$ (EXEC );\
Echo "Installed in $ (INSTDIR )";\
Else \
Echo "Sory, $ (INSTDIR) does not exist .";\
Fi
Is it necessary to learn to compile makefile files at the beginning to learn about arm?
My experience is
I. First, I learned how to install and uninstall software on linux.
Ii. You need to compile Makefile (that is, you need to write and debug the program in linux)
This is required
Then, we need to transplant and drive the learning of writing.
What is the make project manager? How to compile makefile
Copyleft_X answered well. I think this is the case. before writing makefile, I should first find a good makefile and learn about it. Then I am familiar with how makefile works, after a certain degree of conceptual termination, it would be nice to write the makefile thing with me. Remember, never write it very complicated. It would be enough.