3

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    }
Rick
  • 7,007
  • 2
  • 49
  • 79
  • Compiling without errors most certainly does not mean that they're compatible... but yes, most headers that *can* be made compatible *are so* and those that aren't probably wouldn't become any better by wrapping them in `extern "C" {}` – Antti Haapala -- Слава Україні Oct 16 '19 at 15:57
  • 2
    It's not a strict duplicate, but this is similar to https://stackoverflow.com/questions/8087438/do-i-need-an-extern-c-block-to-include-standard-c-headers, which might give some insight. – Demosthenes Oct 16 '19 at 16:02
  • @AnttiHaapala Then what's the **best practice** I should have? Like I said, check those marcos first and then decide? – Rick Oct 16 '19 at 16:06
  • All the POSIX headers will be fine. And for `GNU/Linux` all system headers will be `C++` friendly. Third party libraries you will need to check. (eg `libffmpeg`) – Galik Oct 16 '19 at 16:07
  • @Galik Ok thanks. But to be curious, where did you know that "for GNU/Linux all system headers will be C++ friendly."? I mean.. I was told by someone.. And I should have known that from the Internet. I've almost finished reading *APUE* and it doesn't mention it at all! – Rick Oct 16 '19 at 16:10
  • @Rick I am currently looking for documentation. But the `GNU` project that wrote the `GNU` operating system that runs on most Linux (desktop) implementations also wrote the `GNU C/C++` compiler and their headers are naturally `C++` friendly. – Galik Oct 16 '19 at 16:12
  • @Galik That deduction would be pretty hard for a newbie... I can't just persuade myself on everything by guessing. ;D (I know you are looking for documenation, thank you ) – Rick Oct 16 '19 at 16:18
  • @Rick Yes I see that. I am also pretty sure POSIX specifies that their headers should be C++ friendly (GNU OS is a POSIX implementation) but I'm having trouble proving it :) – Galik Oct 16 '19 at 16:28
  • @Galik Ok got it. :) Told by 2 experts then a newbie can only take it as a yes. :). But is that true: "You can actually break things doing that as some have C++ only exports", if I add extra `extern "C"` block? I saw opposite opinion here.. https://stackoverflow.com/a/8087530/5983841 – Rick Oct 16 '19 at 16:31
  • @Demosthenes Thank you that's helpful. – Rick Oct 16 '19 at 16:35

0 Answers0