Are most linux system headers/API C++ compatiable?
Today I was trying to do something like this:
#include <iostream>
extern "C" {
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
}
int main() {
//socket exercise
}
Then I was told by a friend:
shouldn't even need to do that (the extra extern "C" {}) in most cases as they are system headers.
try it first and then worry about it.
don't assume system headers aren't compatible.
You can actually break things doing that as some have C++ only exports
Bu then I asked:
If I include a C library function and compile without error. Does it ensure that the library I included is C++ compatiable and then I can safely use it?
The answer is:
No, there's no checking.
So try it first --> no error --> doesn't ensure that it's C++ compatiable.
Then what should I do for example with Linux system headers? Because I can't simply wrap the system headers with an extra extern "C" {}
statement ("I might actually break things doing that as some have C++ only exports").
So I need to check if there are __BEGIN_DECLS
and __END_DECLS
MARCOS defined in the system header file for each header file I use? If not, then I need to wrap it with extern "C" {}
? Otherwise I just include it directly?
FYI, __BEGIN_DECLS
and __END_DECLS
are defined as:
# define __BEGIN_DECLS extern "C" {
# define __END_DECLS }