有这个不错的 makefile(来自本教程:https ://sites.google.com/site/michaelsafyan/software-engineering/how-to-write-a-makefile )。然而,正如网站上提到的,这个 makefile 将所有 .c 和 .cpp 文件从单个目录编译为可执行文件。
program_NAME := myprogram
program_C_SRCS := $(wildcard *.c)
program_CXX_SRCS := $(wildcard *.cpp)
program_C_OBJS := ${program_C_SRCS:.c=.o}
program_CXX_OBJS := ${program_CXX_SRCS:.cpp=.o}
program_OBJS := $(program_C_OBJS) $(program_CXX_OBJS)
program_INCLUDE_DIRS :=
program_LIBRARY_DIRS :=
program_LIBRARIES :=
CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))
.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
如何修改这个 makefile 以编译以下程序?例如,如果我的目录具有以下结构:
/project
Makefile
main.cpp
/src
Class1.cpp
Class2.cpp
/obj
Class1.obj
Class2.obj
/bin
myProgram
/inc
Class1.h
Class2.h
/lib
libX.a