If you are looking for a makefile for a simple and intelligent source file, you may see the following demo:
For example, if you have a directory where only a binary file is generated and the source code is all here, you can tryThe first version
Makefile
# Makefile, 2012-06-15 T1738# TODO# ----------------------------------NAME = AppDemoVERSION = 1.0.0RELEASE = 01# ----------------------------------BIN = $(NAME)OBJS = $(patsubst %.cpp,%.o,$(wildcard $(SRC_DIR)/*.cpp))# TODO# BEGIN ----------------------------------# FoldersSRC_DIR = .# FlagsCXXFLAGS = -WallCXXFLAGS += -I$(SRC_DIR)CPPFLAGS = -D_DEBUGLDFLAGS = -L$(SRC_DIR)LIBS = -lpthread# END ----------------------------------.PHONY: all cleanall: $(BIN)$(BIN): $(OBJS)$(CXX) $(CPPFLAGS) $(LDFLAGS) $^ -o $@ $(LIBS)%.o: %.cpp$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $^ -o $@clean:$(RM) $(OBJS) $(BIN)
The Directory should include:
$ls --colormain.cpp makefile
Here, Main. cpp is a hello World applet, which is compiled and run below
$makeg++ -Wall -I. -D_DEBUG -c main.cpp -o main.og++ -D_DEBUG -L. main.o -o AppDemo -lpthread$ls --colorAppDemo main.cpp main.o makefile$./AppDemoHello World!
Okay. You can also add a source code without modifying makefile. For example, add an interface file helloworld. cpp to encapsulate the sayhelloworld () method.
Helloworld. h
// HelloWorld.h#ifndef _HELLOWORLD_H#define _HELLOWORLD_Hvoid sayHelloWorld();#endif
Helloworld. cpp
// HelloWorld.cpp#include "HelloWorld.h"#include <iostream>void sayHelloWorld() { std::cout << "Hello World!" << std::endl;}
And the modified main. cpp
// main.cpp#include "HelloWorld.h"int main(int argc, char **argv) { sayHelloWorld(); return 0;}
Clean up the site and compile and run again:
$make cleanrm -f ./HelloWorld.o ./main.o AppDemo$makeg++ -Wall -I. -D_DEBUG -c HelloWorld.cpp -o HelloWorld.og++ -Wall -I. -D_DEBUG -c main.cpp -o main.og++ -D_DEBUG -L. HelloWorld.o main.o -o AppDemo -lpthread$./AppDemoHello World!
Very nice. Next time I will study its upgraded version ~~~ (Different requirements, different versions, but the cornerstone of unification !)
Download-http://download.csdn.net/detail/wxqee/4375731