Does g++ compilation order matter?

2020-07-06 04:58发布

I noticed that I was able to compile a child class before a parent class with g++. Is there any need to compile in a specific order, with consideration to dependencies?

标签: c++ g++
5条回答
戒情不戒烟
2楼-- · 2020-07-06 05:31

In general, no. The compiler will create symbols that represent anything it doesn't recognize but can safely ignore, and the linked will turn those symbols into proper code. In your case the header tells the compiler everything it needs to know to compile your child class, so the specifics can wait.

查看更多
老娘就宠你
3楼-- · 2020-07-06 05:44

...I was able to compile a child class before a parent class...

"Compile" is not a strictly defined term, so it is not exactly clear what you mean here. But in general, no, you can't compile child class before parent class. In C++ parent type must be complete before you'll be able to use as base class for any other child class type. You are either misinterpreted something or giving the term "compile" some rather unorthodox meaning.

查看更多
趁早两清
4楼-- · 2020-07-06 05:47

In short: No!

Each C++ compilation unit (C++ source file) is compiled independently. Class inheritance etc is set up at runtime. This is why you can have base classes in separately maintained libraries that can be updated without forcing descendant classes to be recompiled, as long as the API and the ABI remains compatible.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-07-06 05:49

Linking order can matter; compilation order does not.

查看更多
祖国的老花朵
6楼-- · 2020-07-06 05:52

To expand on ildjarn's answer, compiling a child class's implementation only needs the parent class's API/contract, not its implementation. That would live in a file like Parent.h which would be included by the file(s) containing the implementation of the child.

查看更多
登录 后发表回答