5

这是我的目录的样子:

/project
    makefile
    /ceda_lib
        makefile
        files....
    /general
        makefile
        files....
    /CLI
        makefile
        files....
    /objects
         files.o 

生成文件(主要):

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 $@ $^

其他生成文件如下所示:(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.
4

1 回答 1

3

这里:

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 (in 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。

于 2017-07-16T16:36:03.730 回答