组织项目并在 Makefile 中指定目标文件的目录
这是我的两个问题:
我现在正在学习使用 CVS 管理我的代码,我只想为我的 C++ 文件、Makefile 和 bash 以及 python 脚本创建一个存储库,而不是目标文件和可执行文件。 所以我在项目目录下创建了几个子目录:src、bin、scripts、results 和 data。
我将 C++ 文件和 Makefile 放在 ~/myproject/src 下,将 Bash 和 Python 脚本放在 ~/myproject/scripts 下,将对象和可执行文件放在 ~/myproject/bin 下。 我希望只有 src 下的文件和脚本会通过 CVS 更新。 我想知道你如何组织你的项目? 只是希望遵循一些良好的习惯。
由于我将 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:
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果你想学习make,GNU make手册非常好,既可以作为参考,也可以作为教程。 您可能需要考虑使用 patsubst 命令。 以下是我自己使用它的 makefile 之一的精简版本:
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:
您的目录结构似乎很合理。
我会为执行编译器制定一个显式规则,例如
Your directory structure seems sensible.
I would make an explicit rule for executing the compiler, like
如果您愿意,您可以将文件保存在不同的目录中,但这不是必需的。 将文件或目录添加到 CVS 存储库一次,CVS 将无限期保留它。 从那时起,您可以更新它,签入它,等等。 如果您不将目标文件添加到存储库,CVS 将不会触及它。 如果您想添加整个目录树,并且您习惯将对象保留在那里,只需在执行此操作之前进行清理即可。
Make 是一个很棒的工具,但它有一些明显的缺陷。 您所描述的是经典问题之一:Make 擅长使用那里的源来制作这里的内容,但反之则不然。 这里有几种方法可以帮助您完成您想要做的事情。
A) 在二进制目录中运行 make:
cd ~/myproject/bin
make -f ../src/makefile
B) 通过暴力方式将对象放在 bin 目录中:
这会给您带来 Makefile.depend 的问题,您可以通过多种方式解决该问题。
C) 学习一些更高级的 Make 技术。 你可能还不应该尝试这个。
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 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:
cd ~/myproject/bin
make -f ../src/makefile
B) Put the objects on the bin directory by brute force:
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.
使用 automake 和 autoconf 用于构建您的项目。
至于文件的结构,只需查看任何大型开源 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.
为什么不选择 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.