John Leidegren
John Leidegren

Reputation: 61037

What exactly is a C++ header file and declaration?

I know the basics about header files and forward declarations but I'm wondering, if I declare the exact same thing in two separate header files and then compile that, will that work?

Is the C++ interface, in this case, portable, I mean, If I have two libs and they share the same interface (declaration or what not) somewhere could I theoretically just replicate the same declaration in the program and actually compile that, or if not, why?

How would C++ for instance be able to tell the difference between two identical declarations in two different files?

Say I had two distinct libs but they shared some interface, they are compiled separately but by the same tools, would it be possible to in a future step bring these to libs together and actually pass the interface between these two libs as if it was the same, compatible interface, even though it was originally compiled from different (but identical) header files?

Upvotes: 0

Views: 74

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477434

Function declarations, variable declarations and class definitions for a given identifier may appear in the source code as often as you like. They just have to be identical each time they appear (which is automatic if you include a given header file in multiple translation units).

It is only the definition of functions, variables and class member functions that must appear precisely once (with a special rule for inlined functions).

(It's a little different for templates: Template definitions may appear repeatedly, but again all occurrences have to be identical. But templates require some non-trivial deduplication work from the linker.)

Upvotes: 1

Related Questions