如何创建 makefile 而不必包含项目中的每个源文件?
我正在上一些 C++ 课程,并向老师发送了 9 个练习,每个练习都是一个名为“ex$”的简单目录,其中 $ 是数字。每个目录都有一个名为“ex$.cpp”的源文件。我想创建一个 makefile,它允许我输入:
make ex$
它将构建一个与“ex$”目录中编译的源文件相对应的可执行文件。问题是我想做到这一点而不为每个练习创建目标(某种“通用目标”)。我还需要有一个“all”目标,该目标将进入以“ex”开头的每个目录并在那里构建可执行文件。我怎样才能做到这一点?
I'm taking some C++ classes and have send the teacher 9 exercises, each exercise is a simple directory with the name 'ex$' where $ is the number. Each directory has a single source file named 'ex$.cpp. I want to create a makefile that will allow me to type:
make ex$
And it will build a executable that corresponds to the compiled source file inside 'ex$' directory. The catch is that I want to do that without creating a target for each exercise(Some kind of 'generic target'). I also need to have an 'all' target that will go into each directory starting with 'ex' and build the executable there. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的所有 C++ 目标都可以使用基本相同的命令构建,那么您可以相当轻松地完成此操作。阅读本文。尤其要查找
$@
。由于这是教育的一部分,所以我将把其余的内容模糊化。If all your C++ targets can be built with essentially the same command, you can do this fairly easily. Read this. Look for
$@
, in particular. Since this is part of an education, I'll leave the rest vague.我还可以建议您查看 CMake,它会为您使用 IMO 生成更好的 makefile。初始的高学习曲线可实现主要的长期收益。 :)
Can I also suggest looking at CMake which will make better makefiles for you to use IMO. Initial high learning curve for major long term gain. :)