简体   繁体   English

组织项目并在Makefile中指定目标文件的目录

[英]organize project and specify directory for object files in Makefile

Here are my two questions: 这是我的两个问题:

  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. 我现在正在学习使用CVS管理我的代码,我只想为我的C ++文件,Makefile和bash以及python脚本创建一个存储库,而不是目标文件和可执行文件。 So I made several subdirectories under my project directory: src, bin, scripts, results and data. 所以我在我的项目目录下创建了几个子目录:src,bin,scripts,results和data。

    I put C++ files and Makefile under ~/myproject/src, Bash and Python scripts under ~/myproject/scripts and object and executables under ~/myproject/bin. 我把〜/ myproject / src下的C ++文件和Makefile,〜/ myproject / scripts下的Bash和Python脚本以及〜/ myproject / bin下的对象和可执行文件放在下面。 I am hoping only the files under src and scripts will be updated via CVS. 我希望只有src和脚本下的文件才能通过CVS更新。 I wonder how you organize your projects? 我想知道你是如何组织你的项目的? Just hope to follow some good habits. 只希望遵循一些好习惯。

  2. 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. 由于我将我的C ++文件和Makefile放入〜/ myproject / src以及对象和可执行文件到〜/ myproject / bin中,我必须在Makefile中指定目录。 Here is what I am doing 这就是我在做的事情

Makefile: 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`

However this will give error 但是这会给出错误

make: *** No rule to make target /home/myproject/bin/myobject.o', needed by /home/myproject/bin/myexecutable'. make:***没有规则来制作目标/home/myproject/bin/myobject.o', needed by / home / myproject / bin / myexecutable'。

How to specify a different directory for object and executable files from C++ files in Makefile? 如何在Makefile中的C ++文件中为对象和可执行文件指定不同的目录?

If you want to learn make, the GNU make manual is very good, both as a reference and a tutorial. 如果你想学习make,那么GNU make手册非常好,无论是作为参考还是教程。 You might want to consider using the patsubst command. 您可能需要考虑使用patsubst命令。 The following is a chopped down version of one of my own makefiles that uses it: 以下是我自己的一个使用它的makefile的简化版本:

OUT = bin/csvfix.exe
CC = g++
IDIR = inc
ODIR = obj
SDIR = src
INC = -Iinc -I../alib/inc
LIBS = ../alib/lib/alib.a -lodbc32 

_OBJS = csved_atable.o \
        csved_case.o \
        csved_cli.o \
        csved_command.o \
        csved_date.o \

OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS))

$(ODIR)/%.o: $(SDIR)/%.cpp 
    $(CC) -c $(INC) -o $@ $< $(CFLAGS) 

$(OUT): $(OBJS)
    $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    strip $(OUT)

clean:
    rm -f $(ODIR)/*.o $(OUT)
  1. Your directory structure seems sensible. 你的目录结构似乎很合理。

  2. I would make an explicit rule for executing the compiler, like 我会为执行编译器制定一个明确的规则,比如

TARGET_DIR=bin
SRC_DIR=src
CXX=g++
CXXFLAGS=
ETC=

OBJS=$(TARGET_DIR)/test.o

all: $(OBJS)

$(TARGET_DIR)/%.o: $(SRC_DIR)/%.cc
        $(CXX) -c -o $@ $(CXXFLAGS) $(ETC) $<
  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. 将文件或目录添加到CVS存储库一次,CVS将无限期地保留它。 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. 如果您不将对象文件添加到存储库,CVS将不会触及它。 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. 如果你想要添加一个完整的目录树,并且你习惯于在那里保留对象,那么在你做之前先做好清理。

  2. Make is a wonderful tool, but it has some glaring faults. Make是一个很棒的工具,但它有一些明显的缺点。 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) Run make in your binary directory: A)在二进制目录中运行make:

...
    all: myexecutable TAGS

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

    VPATH = /home/myproject/src
    ...

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

B) Put the objects on the bin directory by brute force: B)通过强力将对象放在bin目录中:

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

This will give you a problem with Makefile.depend, which you can approach several ways. 这将给你一个Makefile.depend的问题,你可以通过几种方式。

C) Learn some more advanced Make techniques. C)学习一些更先进的制作技巧。 You probably shouldn't try this yet. 你可能不应该尝试这个。

Use automake and autoconf for building your project. 使用automakeautoconf构建项目。

As for the structure of files just look at any big open-source C++ application. 至于文件的结构,只需看看任何大型的开源C ++应用程序。 Any KDE application will do fine for that matter. 任何KDE应用程序都可以解决这个问题。 If you find an application that uses C++ and Python even better. 如果您发现使用C ++和Python的应用程序更好。

Why not go for eclipse, which is quite popular and handy for managing large projects. 为什么不选择eclipse,这对于管理大型项目非常流行和方便。 You can make a new project in eclipse, import-export code into the project from other projects, does version control for you as well etc. No need to write your make files, eclipse does it for you with your mentioned preferences in GUI. 您可以在eclipse中创建一个新项目,从其他项目导入 - 导出代码到项目中,也可以为您执行版本控制等。不需要编写make文件,eclipse会根据您在GUI中提到的首选项为您执行此操作。 If you are involved in a C++ project, just install the CDT plugin over eclipse and your are done. 如果您参与了C ++项目,只需在eclipse上安装CDT插件即可完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM