我开始研究一个小项目来实现C ++中的todo应用程序。
我写了一个Makefile来编译我的C ++文件但是,Makefile只编译main.cc而没有别的。
这是我的MakeFile
#MakeFile for Building the ToDoList App
# Compile CXX Files
CXX := g++-4.9
CXXFLAGS := -std=c++11
CXXFILES := $(wildcard src/*.cc src/lexer/*.cc)
CXXOBJECTS := $(CXXFILES:.cc=.o)
CXXHEADERS := $(wildcard *.h)
# Link all Object Files
LD := ld
LDFLAGS :=
# Declare Clean and Run as Phony Targets
.PHONY: clean run
# The Final Product will be the To-Do APP
all: TedO.app
# Compile the ToDO APP
TedO.app: $(CXXOBJECTS)
$(LD) $(LDFLAGS) -o build/$@ $^
# Run the APP from the Build Folder
run: TedO.app
./build/$^
# Compile the CC files into Object files and place it into the same directory.
%.o: %.cc
$(CXX) $(CXXFLAGS) -o $@ $<
# Clean all the Objects
clean: $(CXXOBJECTS)
rm $^
这是我的目录结构
.
├── Makefile
├── Makefile.swp
├── build
│ ├── tedo
│ └── tedo.txt
├── doc
├── src
│ ├── lexer
│ │ ├── lexer.cc
│ │ └── lexer.h
│ └── main.cc
└── tree.txt
4 directories, 8 files
这是运行Make
时控制台的输出g++-4.9 -std=c++11 -o src/main.o src/main.cc
Undefined symbols for architecture x86_64:
"lexer::lexer()", referenced from:
_main in ccmWqgIU.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [src/main.o] Error 1
答案 0 :(得分:2)
摆脱编译模式规则
%.o: %.cc
$(CXX) $(CXXFLAGS) -o $@ $<
Make已经有.cc
文件的内置规则有效(您忘记添加-c
标志以阻止GCC链接)。
您的makefile还有一些问题:Ted0.app
的规则是错误的,因为您实际上是在链接build/Ted0.app
,而您的clean
规则会在删除之前重新制作所有目标文件它们。
CXX := g++-4.9
CXXFLAGS := -std=c++11
CXXFILES := $(wildcard src/*.cc src/lexer/*.cc)
CXXOBJECTS := $(CXXFILES:.cc=.o)
.PHONY: all run clean
all: build/TedO.app
build/TedO.app: CC := $(CXX)
build/TedO.app: $(CXXOBJECTS)
$(LINK.o) $(LDFLAGS) -o $@
run: build/TedO.app
$<
clean: ; $(RM) $(CXXOBJECTS)