Makefile

korkhg15·2022년 5월 25일
0

Ubuntu

목록 보기
7/9

tree
ㄴ main.cpp
ㄴ a.h
ㄴ b.h
ㄴ Makefile

Example of like upper struct

CC = g++
CFLAGS = -g -Wall
OBJS = main.o

#the name of execute file you want
TARGET = main 

all : main

#the first gab front of '$(CC)' should be 'tab' not 'space'
$(TARGET): $(OBJS)
	$(CC) -o $@ $(OBJS) 

#the first gab front of 'rm' should be 'tab' not 'space'
clean :
	rm -f *.o
    rm -f $(TARGET)
    
#object filename must be the same as source file
main.o: main.cpp 

tree
ㄴ src
\ \ main.cpp
\ \ a.cpp
\ \ b.cpp
ㄴ include
\ \ a.h
\ \ b.h
ㄴ objs
ㄴ Makefile

Example of like upper struct

CC = g++
CXXFLAGS = -Wall -O2
LDFLAGS =
INCLUDE = -Iinclude/
# source file directory
SRC_DIR = ./src
# object file directory (x.o files will be made in this directory)
OBJ_DIR = ./obj

#the name of execute file you want
TARGET = main

#extract .cpp file in src using wildcard and extract name from file using notdir
SRCS = $(notdir $(wildcard $(SRC_DIR)/*.cpp))

OBJS = $(SRCS:.cpp=.o)
# append dirname for object files
OBJECTS = $(patsubst %.o,$(OBJ_DIR)/%.o,$(OBJS))
DEPS = $(OBJECTS:.o=.d)

all: main

#the first gab front of '$(CC)' should be 'tab' not 'space'
$(OBJ_DIR)/%.o : $(SRC_DIR)/%.cpp
	$(CC) $(CXXFLAGS) -c $< -o $@ -MD $(LDFLAGS)

#the first gab front of '$(CC)' should be 'tab' not 'space'
$(TARGET) : $(OBJECTS)
	$(CC) $(CXXFLAGS) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

#the first gab front of 'rm' should be 'tab' not 'space'
.PHONY: clean all
clean:
	rm -f $(OBJECTS) $(DEPS) $(TARGET)

-include $(DEPS)

The upper makefile is almost perfect makefile
If you have some trouble when using the file, plz comment or send mail to me
(korkhg15@g.cbnu.ac.kr)

0개의 댓글