项目的多目录makefile [英] Multi directory makefile for project

查看:76
本文介绍了项目的多目录makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的目录的样子:

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

其他生成文件如下所示:(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目录的内容中创建一个可执行文件.

What I want to do is for all code in the 3 directories to be compiled and all objects to be stored in the /object directory. Then an executable will be created from the $DEPS and the contents of /object directory.

此makefile不能正常运行.您能否找到我做错的事情,也可以建议我一些改进代码的方法.(我对makefile很陌生.)

每当我尝试创建项目时,这就是输出:(Update2)

Also this is the output whenever I try making the project:(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 要构建哪个目标文件.确切地说,模式规则不能是默认规则.

This makefile contains one rule, which is a pattern rule, a way to build any file with a name like ../objects/foo.o. But it doesn't tell Make which object file it is to build. To be precise, a pattern rule cannot be the default rule.

解决此问题的最简单方法是添加一条普通规则:

The simplest way to fix this is with the addition of an ordinary rule:

../objects/$(OBJ):

一旦完成此工作,您将拥有目标文件,但是主makefile中仍然存在问题. run 规则不会生成可执行文件,并且如果您要执行该规则,则必须在命令行上调用它,它将不会自动执行.

Once you have this working you will have the object files, but there are still problems in the main makefile. The run rule will not build an executable, and if you want to execute that rule you will have to invoke it on the command line, it won't follow automatically.

您在掌握基础知识之前尝试递归使用Make(这很棘手).我建议您尝试使用makefile来构建目标文件,然后尝试使用命令行来构建可执行文件,然后仔细查看所使用的命令并重写 run 规则.

You are attempting recursive use of Make -- which is tricky -- before you've mastered the basics. I suggest you try using the makefile to build the object files, then try to build the executable using the command line, then look carefully at the command you used and rewrite the run rule.

一旦做到这一点,其他改进也是可能的.(Make 是一个强大的工具,但它的学习曲线很长.)

Once you get that far, other improvements are possible. (Make is a powerful tool, but it has a long learning curve.)

编辑:如果它根本不起作用,请先尝试更简单的方法.

If it isn't working at all, try something simpler first.

ceda_lib 中选择一个源文件,例如,我不知道 main.cpp .验证源文件存在,并且相应的目标文件( main.o )不存在.将makefile(在 ceda_lib/中)编辑为此:

Pick a source file in ceda_lib, like, I don't know main.cpp. Verify that the source file exists and that the corresponding object file (main.o) does not. Edit the makefile (in ceda_lib/) to this:

main.o: main.cpp
    $(CC) -c $< $(CFLAGS)

然后在 ceda_lib/中,尝试 make 看看会发生什么.

Then within ceda_lib/, try make and see what happens.

如果它生成 main.o ,则删除 main.o ,然后从 project/中尝试 make -C ceda_lib,然后看看会发生什么.如果构建了 ceda_lib/main.o ,那么我们可以继续使用更高级的Makefile.

If it builds main.o, then delete main.o, and then from project/ try make -C ceda_lib, and see what happens. If that builds ceda_lib/main.o, then we can move on to more advanced makefiles.

这篇关于项目的多目录makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆