如何动态重命名 makefile 中的目标文件

发布于 2024-11-03 19:22:26 字数 625 浏览 6 评论 0原文

我对 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 技术交流群。

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

发布评论

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

评论(2

旧人 2024-11-10 19:22:26

首先,我们对你表示深切的同情。

Make 并不是真正设计来处理这种歧义的,所以我们必须使用拼凑。您还没有说出要将文件重命名为什么,所以现在假设我们将 file.o 移动到 file_save.o

# This tells Make that these are not real targets, so that it will
# proceed even if file.o already exists.
.PHONY: $(OBJECTS)

$(OBJECTS): %.o: %.cpp 
    # If file.o exists, move it to file_save.o
    @if [ -f $@ ]; then mv $@ $*_save.o; fi
    $(CC) $(CPPFLAGS) -c 
lt; -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 to file_save.o.

# This tells Make that these are not real targets, so that it will
# proceed even if file.o already exists.
.PHONY: $(OBJECTS)

$(OBJECTS): %.o: %.cpp 
    # If file.o exists, move it to file_save.o
    @if [ -f $@ ]; then mv $@ $*_save.o; fi
    $(CC) $(CPPFLAGS) -c 
lt; -o $@
稚气少女 2024-11-10 19:22:26

扩展我对问题的评论:

也许您应该考虑将对象文件放置在与源文件相同的目录层次结构中,以防止命名冲突。

这样src/network/client.cpp就被编译为build/obj/network/client.o。

我对 makefile 非常生疏,但我相信我通过执行以下操作解决了这个问题:

$SRC= src/network/client.cpp src/main.cpp .....
$OBJ= makefile_replace_function($SRC, .o)

$(OBJ) : $(SRC)
   compile_instructions

您必须将 makefile_replace_functioncompile_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:

$SRC= src/network/client.cpp src/main.cpp .....
$OBJ= makefile_replace_function($SRC, .o)

$(OBJ) : $(SRC)
   compile_instructions

Where you will have to replace makefile_replace_function and compile_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.

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