这是我的目录的样子:
/project
makefile
/ceda_lib
makefile
files....
/general
makefile
files....
/CLI
makefile
files....
/objects
files.o
Makefile(主要):
1 #start other makefiles
2
3
4 o= ./objects
5 DEPS= Affine.hpp CEDA.hpp generalParameters.hpp generalFunctions.hpp
6 OBJ= $o/main.o $o/Affine.o $o/generalFunctions.o
7 CC=g++
8 CFLAGS= -Wall -g -I.
9 export CC
10 export CFLAGS
11 export DEPS
12
13 all:
14 ▸---+$(MAKE) -C general
15 ▸---+$(MAKE) -C ceda_lib
16 ▸---+$(MAKE) -C CLI
17
18 run: $(OBJ) $(DEPS)
19 ▸---$(CC) -o $@ $^
其他makefile看起来像这样:(update2)
1 include ../makefile.variables
2
3 OBJ = main.o
4 all: $(OBJ)
5
6 $(OBJ): %.o: %.cpp $(DEPS)
7 ▸---$(CC) -o ../objects/$@ -c $< $(CFLAGS)
我想要做的是编译3个目录中的所有代码,并将所有对象存储在/ object目录中。然后将从$ DEPS和/ object目录的内容创建一个可执行文件。
这个makefile不会令人遗憾地工作。您能否找到我做错的事情,也可以建议我一些改进代码的方法。 (我对makefile很陌生)。
这也是我每次尝试创建项目时的输出:(Update2)
make: Entering directory '/home/george/Documents/CEDA'
make -C general
make[1]: Entering directory '/home/george/Documents/CEDA/general'
g++ -o ../objects/generalFunctions.o -c generalFunctions.cpp -Wall -g -I.
make[1]: Leaving directory '/home/george/Documents/CEDA/general'
make -C ceda_lib
make[1]: Entering directory '/home/george/Documents/CEDA/ceda_lib'
g++ -o ../objects/Affine.o -c Affine.cpp -Wall -g -I.
Affine.cpp:4:33: fatal error: generalParameters.hpp: No such file or directory
#include "generalParameters.hpp"
^
compilation terminated.
makefile:7: recipe for target 'Affine.o' failed
make[1]: *** [Affine.o] Error 1
make[1]: Leaving directory '/home/george/Documents/CEDA/ceda_lib'
makefile:8: recipe for target 'All' failed
make: *** [All] Error 2
make: Leaving directory '/home/george/Documents/CEDA'
这是makefile.variables
1 #variables used by all makefiles in project directory
2
3 PATH_TO_DIR = ~/Documents/CEDA
4 c = $(PATH_TO_DIR)/ceda_lib
5 g = $(PATH_TO_DIR)/general
6 e = $(PATH_TO_DIR)/CLI #e for executable
7
8 DEPS= $c/Affine.hpp $c/CEDA.hpp $g/generalParameters.hpp $g/generalFunctions.hpp
9 CC=g++
10 CFLAGS= -Wall -g -I.
最佳答案
这里:
OBJ= main.o
../objects/%.o: %.cpp $(DEPS)
$(CC) -c $< $(CFLAGS)
这个makefile包含一个规则,这是一个模式规则,这是一种使用
../objects/foo.o
之类的名称构建任何文件的方法。但是它并没有告诉Make要建立哪个目标文件。确切地说,模式规则不能是默认规则。解决此问题的最简单方法是添加一条普通规则:
../objects/$(OBJ):
完成这项工作后,您将拥有目标文件,但是主makefile中仍然存在问题。
run
规则将不会生成可执行文件,并且如果您想执行该规则,则必须在命令行上调用它,它将不会自动执行。在掌握基础知识之前,您正在尝试递归使用Make-这很棘手。我建议您尝试使用makefile来构建目标文件,然后尝试使用命令行来构建可执行文件,然后仔细查看所使用的命令并重写
run
规则。一旦达到目标,就可以进行其他改进。 (Make是功能强大的工具,但学习曲线较长。)
编辑:如果根本不起作用,请先尝试一些更简单的方法。
在
ceda_lib
中选择一个源文件,例如,我不知道main.cpp
。验证源文件存在,并且相应的目标文件(main.o
)不存在。将makefile(在ceda_lib/
中)编辑为此:main.o: main.cpp
$(CC) -c $< $(CFLAGS)
然后在
ceda_lib/
中,尝试make
看看会发生什么。如果生成的是
main.o
,请删除main.o
,然后从project/
尝试make -C ceda_lib
,然后看看会发生什么。如果构建了ceda_lib/main.o
,那么我们可以继续使用更高级的Makefile。