如何动态重命名 makefile 中的目标文件
我对 makefile 非常陌生。我之前做过的最复杂的任务是在已经设计的 makefile 中包含新的 .h 和 .cpp 或 .c。
我必须将项目的编译过程从 Visual Studio 移植到 gcc,并且已经有一个同事为此编写了解决方案,但他使用了 4 个 bash 脚本和一个 makefile 来编译该解决方案。
我没有这样做,而是寻找更容易维护的解决方案。我承认我的问题可能很愚蠢,但我无法在任何地方找到它,也无法正确理解它。
在下面的目标中:
$(OBJECTS): %.o: %.cpp
$(CC) $(CPPFLAGS) -c $< -o $@
我想测试正在创建的 .o 是否已经存在并将其重命名为其他名称。这是必要的,因为项目中存在多个同名但内容不同的源文件。
例如,如果当前正在编译的 .cpp 称为 file.cpp 并且将生成的对象是 file.o,则规则应测试 file.o 是否已存在并将当前 file.o 重命名为其他名称。
如果您知道任何解释此问题的好教程,请告诉我。我找到了很多示例,展示了如何对按该规则编译的当前文件进行测试,但没有一个示例会将对象重命名为 .o。
提前致谢,并对这里写的任何“愚蠢”表示歉意。 :-)
I am very very new to makefiles. The most complex task I had done before was to include new .h and and .cpp or .c in already designed makefiles.
I have to port the compilation process of a project from Visual Studio to gcc and there is an already made solution for this written by a colleague but he used 4 bash scripts and a makefile to compile the solution.
Instead of doing that I was looking for solutions to make it easier to maintain. My question may be very dumb I admit, but I could not find it anywhere nor I could understand it all properly.
In the target below:
$(OBJECTS): %.o: %.cpp
$(CC) $(CPPFLAGS) -c lt; -o $@
I would like to test if the .o being created already exists and rename it to something else. This is necessary because in the project there are several source files that have the same name yet they are different in content.
For example, if the current .cpp being compiled is called file.cpp and the object that will be generated is file.o, the rule should test whether file.o already exists and rename the current file.o to something else.
If you know any good tutorial that explains this, please let me know. I found lots of examples that show how to make tests for the current file being compiled by that rule, but none that would rename the object .o.
Thanks in advance and sorry for any "dumbness" written here. :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我们对你表示深切的同情。
Make 并不是真正设计来处理这种歧义的,所以我们必须使用拼凑。您还没有说出要将文件重命名为什么,所以现在假设我们将
file.o
移动到file_save.o
。First of all, you have our deep sympathy.
Make is not really designed to handle such ambiguity, so we'll have to use a kludge. You haven't said what you want to rename the file to, so for now lets say we move
file.o
tofile_save.o
.扩展我对问题的评论:
也许您应该考虑将对象文件放置在与源文件相同的目录层次结构中,以防止命名冲突。
这样src/network/client.cpp就被编译为build/obj/network/client.o。
我对 makefile 非常生疏,但我相信我通过执行以下操作解决了这个问题:
您必须将
makefile_replace_function
和compile_instructions
替换为它们真正的等效项,因为我然后就忘记了......我意识到这可能不是很有帮助,但至少这是一个值得考虑的想法。
Expansion of my comment on the question:
Perhaps you should consider placing object files in the same directory hieryarchy as the source files, to prevent naming conflicts.
So that src/network/client.cpp is compiled to build/obj/network/client.o.
I'm extremely rusty when it comes to makefiles but I believe I solved that by doing something like:
Where you will have to replace
makefile_replace_function
andcompile_instructions
to their real equivalents since I have forgotten then...I realize this might not be very helpful but atleast its an idea to consider.