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

发布于 2024-08-01 15:48:54 字数 1135 浏览 0 评论 0原文

这是我的两个问题:

  1. 我现在正在学习使用 CVS 管理我的代码,我只想为我的 C++ 文件、Makefile 和 bash 以及 python 脚本创建一个存储库,而不是目标文件和可执行文件。 所以我在项目目录下创建了几个子目录:src、bin、scripts、results 和 data。

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

  2. 由于我将 C++ 文件和 Makefile 放入 ~/myproject/src 并将对象和可执行文件放入 ~/myproject/bin,因此我必须在 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`

但是这会给出错误

make:***没有规则来创建目标/home/myproject/bin/myobject.o',由/home/myproject/bin/myexecutable'需要。

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

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. So I made several subdirectories under my project directory: src, bin, scripts, results and data.

    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.

  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. 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`

However this will give error

make: *** No rule to make target /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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盛装女皇 2024-08-08 15:48:54

如果你想学习make,GNU make手册非常好,既可以作为参考,也可以作为教程。 您可能需要考虑使用 patsubst 命令。 以下是我自己使用它的 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 $@ 
lt; $(CFLAGS) 

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

clean:
    rm -f $(ODIR)/*.o $(OUT)

If you want to learn make, the GNU make manual is very good, both as a reference and a tutorial. You might want to consider using the patsubst command. The following is a chopped down version of one of my own makefiles that uses it:

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 $@ 
lt; $(CFLAGS) 

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

clean:
    rm -f $(ODIR)/*.o $(OUT)
很糊涂小朋友 2024-08-08 15:48:54
  1. 您的目录结构似乎很合理。

  2. 我会为执行编译器制定一个显式规则,例如

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) 
lt;
  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) 
lt;
横笛休吹塞上声 2024-08-08 15:48:54
  1. 如果您愿意,您可以将文件保存在不同的目录中,但这不是必需的。 将文件或目录添加到 CVS 存储库一次,CVS 将无限期保留它。 从那时起,您可以更新它,签入它,等等。 如果您不将目标文件添加到存储库,CVS 将不会触及它。 如果您想添加整个目录树,并且您习惯将对象保留在那里,只需在执行此操作之前进行清理即可。

  2. Make 是一个很棒的工具,但它有一些明显的缺陷。 您所描述的是经典问题之一:Make 擅长使用那里的源来制作这里的内容,但反之则不然。 这里有几种方法可以帮助您完成您想要做的事情。

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

    ...
    all: myexecutable TAGS

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

    VPATH = /home/myproject/src
    ...

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

B) 通过暴力方式将对象放在 bin 目录中:

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

这会给您带来 Makefile.depend 的问题,您可以通过多种方式解决该问题。

C) 学习一些更高级的 Make 技术。 你可能还不应该尝试这个。

  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.

  2. 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) 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) Put the objects on the bin directory by brute force:

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

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.

三生殊途 2024-08-08 15:48:54

使用 automakeautoconf 用于构建您的项目。

至于文件的结构,只需查看任何大型开源 C++ 应用程序即可。 任何 KDE 应用程序
在这件事上会做得很好。 如果您发现使用 C++ 和 Python 的应用程序更好。

Use automake and autoconf for building your project.

As for the structure of files just look at any big open-source C++ application. Any KDE application
will do fine for that matter. If you find an application that uses C++ and Python even better.

醉态萌生 2024-08-08 15:48:54

为什么不选择 eclipse,它非常流行并且对于管理大型项目很方便。 您可以在 eclipse 中创建一个新项目,从其他项目导入导出代码到该项目中,还可以为您进行版本控制等。无需编写 make 文件,eclipse 会根据您在 GUI 中提到的首选项为您完成这些工作。
如果您参与 C++ 项目,只需在 eclipse 上安装 CDT 插件即可。

Why not go for eclipse, which is quite popular and handy for managing large projects. 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.
If you are involved in a C++ project, just install the CDT plugin over eclipse and your are done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文