3

I plan to use some Linux API to finish a network program task in C++ language. All the Linux API or structure are defined in the C header file, such as <netinet/in.h>, How to use them correctly in Cpp source file? How to use a struct defined in C header file? Should I use C++ linkage specification? such as:

    extern "C" {
        #include <netinet/in.h>
};
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
linyuwang
  • 321
  • 1
  • 3
  • 10
  • 1
    C++ is mostly backward compatible to C. Is there any issues? – user202729 Jan 27 '18 at 05:51
  • In C language, when I have to use a struct to define an object, I have to put the "struct " keyword before the struct name, but in CPP, we don't have to do that. – linyuwang Jan 27 '18 at 05:54
  • 1
    Doesn't mean you can't do that. – user202729 Jan 27 '18 at 05:55
  • OK, you mean , In C++, if we want to use a structure defined in C, we still have to put the "struct" keyword before the structure name? Is there some better choice? – linyuwang Jan 27 '18 at 05:57
  • 1
    ... Again, _is there any issues_? Why can't you just assume the header is written in C++ and proceed normally? – user202729 Jan 27 '18 at 05:59
  • `extern "C"` is for linking other programs to yours, not yours to others. And as @user202729 keeps saying, you don't need to worry about that anyways. Simply `#include ` then when you need a `sockaddr_in` structure, simply `sockaddr_in sock_obj;` or `sockaddr_in* sock_obj = new sockaddr_in();`. – txtechhelp Jan 27 '18 at 06:02
  • @user202729 you can't because C and C++ are different languages, C allows things that are not allowed in C++ linyuwang gave one example above, another difference is the list of reserved words. – Jasen Jan 27 '18 at 06:03
  • @Jasen _C allows things that are not allowed in C++_ .. prey tell? – txtechhelp Jan 27 '18 at 06:05
  • 1
    Ok, use `extern "C"` as described [here](https://stackoverflow.com/q/3329159/5267751). Because it's C++ anyway (outside of the `extern "C"` block) you don't need the `struct`. **Just try it before asking**. – user202729 Jan 27 '18 at 06:08
  • `void this(void) {;} // not allowed in c++` – Jasen Jan 27 '18 at 06:11
  • Or `int new;`. But it's unrelated here. – user202729 Jan 27 '18 at 06:12
  • @Jasen .. that's not a valid example of something not allowed. Keywords are keywords. – txtechhelp Jan 27 '18 at 06:21
  • 3
    voting to close as "cannot reproduce issue". – SherylHohman Jan 27 '18 at 06:25
  • @user202729 Even if C grammar is equivalent to C++ grammar, `extern C` must be used because of ABI incompatibilities. The first that comes to my mind is name mangling. Inside an object file, a function `int f(int)` will be named `f` if it has C linkage and `_Z1fi` if it has C++ linkage. – Oliv Jan 27 '18 at 10:13

1 Answers1

3

The system headers in Linux are already C++ compatible, you don't need to do anything other than #include them. In fact the vast majority of C libraries have C++ compatible headers so you just #include them without issue.

There are some exceptions (like ffmpeg). Those you need to wrap with extern "C" {}. Although it may not always be possible to use a native C header simply by wrapping it in that way because C can contain other incompatibilities.

See here for an example using ffmpeg

Galik
  • 47,303
  • 4
  • 80
  • 117
  • For the detail, those libraries that are C++ compatible use conditionnaly defined preprocessor macros so that when included in a C++ translation unit, all their content is inclosed in `extern "C"`. For example `sys/cdefs.h` file defines `__BEGIN_DECLS extern "C"{` if compiled inside a C++ TU, which is then used in hundreds of other headers. – Oliv Jan 27 '18 at 10:22