I have been believed that C header files must be included in the top level of C++ program. Anyway, I accidentally discovered that C++ is allowing inclusion of C headers in a sub namespace.
namespace AAA {
extern "C" {
#include "sqlite3.h" // C API.
}
}
And then, all the C types and functions will be placed in the namespace. More interestingly, all the linked C functions are also just working! I also discovered that this may cause some preprocessor issue, but except that, it seems to be working pretty fine.
Is this a standard behavior? (I am using Clang 3.x) If it is, what is the name of this feature and where can I find this feature mentioned in the standard?
Header files serve two purposes. System header files declare the interfaces to parts of the operating system. You include them in your program to supply the definitions and declarations you need to invoke system calls and libraries.
Header files are needed to declare functions and variables that are available. You might not have access to the definitions (=the . c files) at all; C supports binary-only distribution of code in libraries.
Typically, you declare a namespace in a header file. If your function implementations are in a separate file, then qualify the function names, as in this example. A namespace can be declared in multiple blocks in a single file, and in multiple files.
You may even do strange things like
//test.c
int
#include "main.h"
{
return 1;
}
//main.h
main(void)
The preprocessor macros are expanded before any syntax check is done. The above example will expand to
int
main(void)
{
return 1;
}
which is legal code. While you really should avoid such examples, there are cases, where including into another element is quite useful. In your question it depends on how the names are mangled during compilation. If all the definitions in your header file are declared with extern "C"
, the names will be searched unmangled in the object file, this is, however, not the case if the object file containing the implementation does not use the same namespace as it's definition in the consuming code and does not declare it extern "C"
.
Is this a standard behavior?
Yes - the behaviour is supported by the Standard, as the C++ compiler doesn't really have a notion of code being "C" versus "C++" except when extern "C"
is used to inhibit namespace mangling.
The consequence of not inhibiting mangling is that you may end up with "unresolved symbol" errors at link time if you try to link with a C library defining the out-of-line symbols (extern
variables, functions) mentioned in the header.
If it is [a Standard feature], what is the name of this feature and where can I find this feature mentioned in the standard?
This is just a consequence of the way #include
works, which is defined in 16.2 Source file inclusion [cpp.include], crucially:
[an
#include
] causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters.
So, whatever happens with the "C" header is exactly as if the surrounding namespace
/extern
statements and braces existed at the top and bottom of the header file instead... by the time the next phase on compilation begins it's irrelevant exactly where the source code came from (except for purposes of displaying error messages that properly relate to the source file).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With