【发布时间】:2014-04-24 15:05:01
【问题描述】:
我是 linux 新手,也是 makefile 新手,所以在这一点上我有点难过。我已经盯着制作手册看了一个小时,我想我会问:
我想为一个相对较小的项目创建一个生成文件,其源文件位于proj/src/ 中,并且位于该位置的子目录中。我在proj/makefile 写了一个makefile,想用它来收集所有源文件,找到它们的依赖关系,并将结果编译成proj/build/。这是我写的:
# here are my files and directories (there are also header files that are not
# listed here, but these are referred to from within the .cpp files with
# respect to the proj/src directory)
# proj/makefile
# proj/src/
# proj/src/main.cpp
# proj/src/dir1/
# proj/src/dir1/source1.cpp
# proj/src/dir2/
# proj/src/dir2/source2.cpp
# proj/build/
srcDir = src/
buildDir = build/
# This is a list of all the object files (can I get this programatically?)
objects = main.o source1.o source2.o
all: prog
# Here I want to compile all source (.cpp) files from src/ and all of its subdirectories, and to find the dependencies I want to call g++ -MM to automatically generate the list:
%.o: %.cpp
$(CXX) $(CXXFLAGS) -MM -I $(srcDir)
clean:
rm -rf *o $(buildDir)/prog
这远非实用,但我想在问之前先给出它。运行它给出了
make: * 没有规则来制作目标
main.o', needed byprog'。停下来。
所以我想自动写入目标文件的尝试失败了。如果有人能给我一些很好的方向,我希望你能在 cmets 和代码之间找出意图。
编辑:我现在尝试了以下方法:
SHELL := /bin/bash
srcDir = src
buildDir = bin
sources := $(shell find $(srcDir) -name *.cpp)
objects := $(sources:%.cpp=%.o)
-include $(sources:%.cpp=%.d)
# This is a list of all the object files (can I get this programatically?)
all: prog
prog: $(objects)
g++ $(objects) -o $(buildDir)/prog
# Here I want to compile all source (.cpp) files from src/ and all of its subdirectories, and to find the dependencies I want to call g++ -MM to automatically generate the list:
%.o: %.cpp
$(CXX) $(CXXFLAGS) -MMD -MP -c -I $(srcDir)
clean:
rm -rf *o $(buildDir)/hello
但是当我运行它时,我收到以下错误:
g++ -MMD -MP -I src
g++: fatal error: no input files
compilation terminated.
make: *** [src/core/Cell.o] Error 1
【问题讨论】:
-
小心
rm -rf -
查看 github.com/cdesjardins/makefiles 以获得简单的 makefile 框架。
-
main.o 的 .cpp 文件在哪里?如果它的 src/main.cpp 目标必须是 src/main.o ...看看 makefile 函数,如通配符等。
-
%.o: %.cpp 你有所有你想做的对象对应的.cpp吗?