您好我通過修改這個例子中寫道這個Makefile:https://sites.google.com/site/michaelsafyan/software-engineering/how-to-write-a-makefile1.4.3 OBJ文件OBJ目錄中的Makefile
program_NAME := bin/myprogram
SRC_DIR := src
#
#srces
#
program_C_SRCS := $(wildcard $(SRC_DIR)/*.c)
program_CXX_SRCS := $(wildcard $(SRC_DIR)/*.cpp)
#
#obj files
#
program_C_OBJS := ${program_C_SRCS:.c=.o}
program_CXX_OBJS := ${program_CXX_SRCS:.cpp=.o}
program_OBJS := $(program_C_OBJS) $(program_CXX_OBJS)
#
# include and library dirs; also libraries
#
program_INCLUDE_DIRS := inc
program_LIBRARY_DIRS :=
program_LIBRARIES :=
# flags
CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))
#
# targets
#
.PHONY: all clean distclean
all: $(program_NAME)
$(program_NAME): $(program_OBJS)
$(LINK.cc) $(program_OBJS) -o $(program_NAME)
clean:
@- $(RM) $(program_NAME)
@- $(RM) $(program_OBJS)
distclean: clean
它可以通過以下方式。下面的類被編譯成一個可執行文件「myprogram」,它在bin
目錄中輸出。唯一的問題是目標文件是在src
文件夾中創建的,而不是obj
文件夾。我如何修改這個makefile,在obj
文件夾中創建obj
文件?謝謝。
/project
Makefile
/src
Class1.cpp
Class2.cpp
main.cpp
/obj
/bin
myProgram
/inc
Class1.h
Class2.h
嗨,Joachim,謝謝。我還認爲我現在必須爲Object文件添加單獨的目標。如果您可以添加簡短的描述,儘管對於我來說,它會更好或者不太明顯。 PS。爲什麼'$(OBJ_DIR)/%。o:'目標被執行,我沒有在之前將它指定爲依賴關係? –