5 ms·
Declarations, not definitions. Declarations go into object files as symbol names for the purpose of linking. You can have multiple declarations as long as they
by astrobe_ 3y ago
Declarations, not definitions. Declarations go into object files as symbol names for the purpose of linking. You can have multiple declarations as long as they are the same (or if they are not they define different things in C++ due to overloading), but you cannot have multiple definitions (even in different object files) even if they are identical (compiler doesn't bother checking they are, it sees it as fishy and just drops a "multiple definitions" error).
- tlb 3y agoNo, C++ header files contain definitions too, which are emitted into every object file where they're used. If it's something allowed to be multiply defined it's emitted as a "weak" symbol, and the linker eliminates duplicates. This includes every inline function that's used, and many default-generated functions, such as default constructors & destructors and virtual function tables. For a simple example, See https://godbolt.org/z/qhvaP8Eed https://godbolt.org/z/qhvaP8Eed and observe the Foo::Foo() constructor, which would be emitted in every .o file with the 'struct Foo' definition.
- astrobe_ 3y agoOk, thanks for the correction.