Simple and intelligent searching for makefile of source files

Source: Internet
Author: User

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

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.