gpt4 book ai didi

makefile - 项目的多目录 makefile

转载 作者:行者123 更新时间:2023-12-02 22:06:43 25 4
gpt4 key购买 nike

这是我的目录的样子:

/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。

关于makefile - 项目的多目录 makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45130372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com