Using the Makefile compiling multiple source method c

This is the folder structure in my app directory, each directory such as debug, debug / bin, debug / obj and so need to manually create, and the best give enough rwx permissions, here Makefile does not automatically create the directory. 
.
| - Makefile | - Debug | | - Makefile | | - bin | | `- App |` - obj | | - main.o | `- test_file_stat.o | - main.c | - the Test | | - Makefile | | - test_file_stat.c | `- test_file_stat.h ` - named text.txt in the

1. the following is a Makefile file in the root directory of the app directory:

Set # compiler
CC = gcc

#debug folder in the makefile needs to be executed last, so the subdirectory where you need to perform to exclude debug folder, where the use awk exclude the debug folder, read the rest of the folder
SUBDIRS = $ (shell ls -l | grep ^ d | awk '{if (! $$ 9 = "debug") print $$ 9}')

# No need to comment next line of code, because we already know debug in the makefile is executed last, so I finally go directly to debug the makefile execution specified directory on the line, there are the following specific comments
#DEBUG = $ (shell ls -l | grep D ^ | awk '{IF ($$. 9 == "Debug")}. 9 Print $$')
# remember the path to the root directory of the current project
ROOT_DIR = $ (shell pwd)

# Ultimate bin name of the file, you can change their needs
BIN = app

Catalog # target file is located
OBJS_DIR = debug / obj

#bin file directory where
BIN_DIR = debug / bin

C # Get the current directory in the file set, in the variable CUR_SOURCE
CUR_SOURCE = $ {wildcard * .c}

C # corresponding file name into file on the following o CUR_OBJS variable
CUR_OBJS = $ {patsubst% .c, % .o, $ (CUR_SOURCE)}

# The following variables are exported to the sub-shell, this is equivalent to the export makefile in a subdirectory
export CC BIN OBJS_DIR BIN_DIR ROOT_DIR

# Note that the order, you need to perform the last to be SUBDIRS DEBUG
All: $ (SUBDIRS) $ (CUR_OBJS) DEBUG
  # the make-successful ...

# Recursively makefile files in subdirectories, which is the key recursively executed
$ (SUBDIRS): ECHO
  the make -C $ @

DEBUG:ECHO
  make -C debug

ECHO:
  @echo $(SUBDIRS)

# Compile the c file is o file and placed in the specified directory object file that is OBJS_DIR
$ (CUR_OBJS):% o:..% C
  $ (CC) -c $ ^ -o $ (ROOT_DIR) / $ ( OBJS_DIR) / $ @

clean:
  . @rm $ (OBJS_DIR) / * o
  @rm -rf $ (BIN_DIR) / *

  2. The following is the Makefile subdirectory test test directory:

# Subdirectory Makefile directly read line its sub
SUBDIRS = $ (shell ls -l | grep ^ d | awk '{print $$ 9}')

# The following codes with the same root directory of the makefile explanation
CUR_SOURCE = $ {wildcard * .c}

CUR_OBJS=${patsubst %.c, %.o, $(CUR_SOURCE)}

all:$(SUBDIRS) $(CUR_OBJS)

$(SUBDIRS):ECHO
  make -C $@

$ (CUR_OBJS):%. O:%. C
  $ (CC) -c $ ^ -o $ (ROOT_DIR) / $ (OBJS_DIR) / $ @

ECHO:
  @echo $(SUBDIRS)

  3. The following is the Makefile in the target debug directory:

OBJS = *. The

O = obj

$ (Is root_k) / $ (the BIN_) / $ (B) $ (O) / $ (OBJS)
  $ (CC) -o $ @ $ ^

  4. written, in the long run make app directory, to generate an executable file in the debug / bin directory of app.

  5. Do ./debug/bin/app to run the executable file app.




 

Guess you like

Origin www.cnblogs.com/yongfengnice/p/11790115.html