组织项目并在Makefile中为目标文件指定目录 [英] organize project and specify directory for object files in Makefile

查看:175
本文介绍了组织项目并在Makefile中为目标文件指定目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的两个问题:

  1. 我现在正在学习使用CVS管理代码,我只想为我的C ++文件,Makefile,bash和python脚本(而不是目标文件和可执行文件)建立一个存储库.因此,我在项目目录下创建了几个子目录:src,bin,脚本,结果和数据.

  1. I am now learning to manage my code with CVS, and I just want to make a repository for my C++ files, Makefile and bash and python scripts only, not the object files and executables. So I made several subdirectories under my project directory: src, bin, scripts, results and data.

我将C ++文件和Makefile放在〜/myproject/src下,将Bash和Python脚本放在〜/myproject/scripts下,并将对象和可执行文件放在〜/myproject/bin下.我希望只有src和scripts下的文件才能通过CVS更新.我想知道您如何组织项目?只是希望遵循一些良好的习惯.

I put C++ files and Makefile under ~/myproject/src, Bash and Python scripts under ~/myproject/scripts and object and executables under ~/myproject/bin. I am hoping only the files under src and scripts will be updated via CVS. I wonder how you organize your projects? Just hope to follow some good habits.

由于我将C ++文件和Makefile放入〜/myproject/src,并将对象和可执行文件放入〜/myproject/bin,因此我必须在Makefile中指定目录.这是我在做什么

Since I put my C++ files and Makefile into ~/myproject/src and object and executable files into ~/myproject/bin, I have to specify the directories in Makefile. Here is what I am doing

Makefile:

...
BIN_DIR=/home/myproject/bin/

all: $(BIN_DIR)myexecutable TAGS

TAGS: *.cc *.h
    etags --members --declarations -l c++ *.cc *.h

$(BIN_DIR)myexecutable: $(BIN_DIR)myobject.o
    $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

Makefile.depend: *.h *.cc Makefile
    $(CXX) -M $(CXXFLAGS) *.cc > Makefile.depend

clean:
    \rm -f $(BIN_DIR)myexecutable $(BIN_DIR)*.o Makefile.depend TAGS`

但是这会导致错误

make:***没有使目标/home/myproject/bin/myobject.o', needed by/home/myproject/bin/myexecutable'的规则.

make: *** No rule to make target /home/myproject/bin/myobject.o', needed by/home/myproject/bin/myexecutable'.

如何为目标文件和可执行文件指定与Makefile中的C ++文件不同的目录?

How to specify a different directory for object and executable files from C++ files in Makefile?

推荐答案

  1. 您可以根据需要将文件保留在不同的目录中,但这不是必需的.一次将文件或目录添加到CVS存储库,CVS将无限期保留它.从那时起,您可以对其进行更新,签入,无论如何.如果您不将对象文件添加到存储库中,则CVS不会触摸它.如果要添加整个目录树,并且习惯于将对象保留在其中,请在执行操作之前先进行清理.

  1. You can keep your files in different directories if you like, but that isn't necessary. Add a file or directory to the CVS repository once, and CVS will retain it indefinitely. From then on you can update it, check it in, whatever. If you don't add an object file to the repository, CVS won't touch it. If you want to add a whole directory tree, and you're in the habit of keeping objects there, just make clean before you do it.

制作是一个很棒的工具,但是它有一些明显的缺点.您所描述的是经典问题之一:Make擅长使用来源那里此处制作东西,但并非相反.这里有几种方法可以做您想做的事情.

Make is a wonderful tool, but it has some glaring faults. What you're describing is one of the classic problems: Make is good at using a source there to make something here, but not the other way around. Here are a couple of ways to do what you're trying to do.

A)在二进制目录中运行make:

A) Run make in your binary directory:


    ...
    all: myexecutable TAGS

    myexecutable: myobject.o
        $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

    VPATH = /home/myproject/src
    ...

cd〜/myproject/bin
make -f ../src/makefile

B)通过蛮力将对象放在bin目录中:

cd ~/myproject/bin
make -f ../src/makefile

B) Put the objects on the bin directory by brute force:


    $(BIN_DIR)%.o: %.cc
        $(CXX) $(CXXFLAGS) -c -o $@ $^

这将为您提供Makefile.depend问题,您可以采用几种方法.

C)了解一些更高级的制作技术.您可能还不应该尝试此操作.

This will give you a problem with Makefile.depend, which you can approach several ways.

C) Learn some more advanced Make techniques. You probably shouldn't try this yet.

这篇关于组织项目并在Makefile中为目标文件指定目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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