简体   繁体   English

C++ Makefile 依赖:目标文件依赖哪些文件?

[英]C++ Makefile Dependencies: What Files Do Object Files Depend On?

I'm a little confused on the Makefile dependencies for .cpp files.我对 .cpp 文件的 Makefile 依赖项有点困惑。 For example, say I have a file main.cpp that includes Point.h and Rectangle.h.例如,假设我有一个包含 Point.h 和 Rectangle.h 的文件 main.cpp。 I would think that the Makefile dependency line for main.o looks like:我认为 main.o 的 Makefile 依赖行看起来像:

main.o: main.cpp main.o: main.cpp

But it seems most people would do:但似乎大多数人会这样做:

main.o: main.cpp Point.h Rectangle.h main.o: main.cpp Point.h Rectangle.h

I don't understand why.我不明白为什么。 Object files are created before linking, right?目标文件是在链接之前创建的,对吗? So when main.cpp is compiled to main.o, its references to Point and Rectangle functions are still unresolved references, in a way.因此,当 main.cpp 编译为 main.o 时,它对 Point 和 Rectangle 函数的引用在某种程度上仍然是未解析的引用。 Then, when making the final executable, the linker resolves the references with the machine code for Point and Rectangle.然后,在生成最终的可执行文件时,链接器会使用 Point 和 Rectangle 的机器代码解析引用。 In other words, main.o doesn't really get impacted if I change Point.h or Rectangle.h.换句话说,如果我更改 Point.h 或 Rectangle.h,main.o 并不会真正受到影响。 The final executable does depend on all three, but main.o does not.最终的可执行文件确实依赖于所有三个,但 main.o 不依赖。 What is wrong with my thinking?我的想法有什么问题?

Let's say your Point.h header file defines a class as follows:假设您的Point.h头文件定义了一个类,如下所示:

class Point {

    int x;

    int y;

    int hash() const
    {
          return x+y;
    }
};

The class may have other stuff in it, or some other class method.类中可能有其他东西,或者其他一些类方法。 This is just an example.这只是一个例子。

Your main.cpp calls the hash() method of some Point .您的main.cpp调用了某个Pointhash()方法。 And you compile it.你编译它。

Now you go back, and edit this header file:现在你回去,编辑这个头文件:

    int hash() const
    {
          return x*y;
    }

You don't change anything in main.cpp .您不会更改main.cpp任何内容。

But you obviously must recompile it, since it's now supposed to be doing something different, since it calls hash() .但你显然必须重新编译它,因为它现在应该做一些不同的事情,因为它调用hash()

Pretty much any kind of change in the header file potentially affects any .cpp file that includes it, hence they all must be recompiled.头文件中的几乎任何类型的更改都可能影响包含它的任何.cpp文件,因此它们都必须重新编译。

An #include is logically equivalent to replacing the line with the #include with the entire contents of the included file. #include在逻辑上等同于用包含文件的全部内容替换带有#include的行。

So, if you change anything in the header file, it is logically equivalent to changing something in the .cpp file that includes it.因此,如果您更改头文件中的任何内容,在逻辑上等同于更改包含它的.cpp文件中的某些内容。 It is the compiled code in the main.o that's affected by it, and not just the final executable.受其影响的是main.o中的编译代码,而不仅仅是最终的可执行文件。 Here, you'll just relink the same code in main.o that still uses the definition of hash() that you already changed now.在这里,您只需重新链接main.o中的相同代码,该代码仍然使用您现在已经更改的hash()定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM