Are the Unix and Linux API headers C ++ compatible?

I previously wrote C ++ code where the #include

Unix and Linux API headers and these programs cause the expected behavior. However, I don't know what to rely on. It is possible that incompatibilities between C and C ++ could cause valid C headers to act in unexpected ways when used by C ++ programs.

Is it possible to reliably use Unix and Linux API headers with code that will compile as C ++?

Is this the purpose of the authors of these headings? Or are these headers only meant to be valid C?

Are there any known pitfalls with this?

Obviously, Unix and Linux distributions are plentiful and I don't expect a response for each distribution one at a time. My expectation is that the same answer will apply to almost all Unix and Linux distributions, and exceptions will validate this rule. If this assumption is incorrect, an explanation for this would also be the correct answer.

By Unix headers, I mean the following:

http://www.unix.org/version3/apis/headers.html

By Linux headers, I mean headers provided by Linux distributions, usually as a package named "linux-headers", which allows programs to interact with the Linux kernel. For example, this Debian package:

https://packages.debian.org/wheezy/kernel/linux-headers-3.2.0-4-amd64

I understand that the Unix link is only a specification and that every Linux distribution is different, but I suspect it is wise to ask this question for most distributions. If not, then correct me.

Edit I am only referring to headers used by user-space programs.

+3


source to share


2 answers


Standard C headers such as <stdio.h>

, <stdlib.h>

etc. are specified in Appendix D of the C ++ standard, which says:

These are deprecated functions, in which deprecations are defined as: Normative for the current edition of the standard, but not guaranteed to be part of the Standard in future versions.

Non-legacy C ++ versions of the standard C headers have type names <cstdio>

, <cstdlib>

etc., and they technically put their definitions in a std

(non-global) namespace. Thus, to be 100% compatible with the non-deprecated part of the C ++ specification, you need to write something like this:

#include <cstdio>

int main() {
  std::printf("Hello, world!\n");
}

      

However, to my knowledge, no existing implementation actually forces you to do this, and in my opinion it hardly ever will. So in practice, you can safely use the standard C headers in C ++ without much concern.

Also, if you are (for example) on a POSIX system, you can generally use POSIX functionality with C ++ equally safely. Of course, no one is going to deliberately violate this because users will rebel.

However, accidental breakdown is conceivable by mixing paradigms. If both the platform and locale provide some function, you should use one or the other, but not both. In particular, I would not confuse POSIX streaming and synchronization mechanisms with the standard C ++ 11 streaming and synchronization mechanisms, because it is easy to imagine that the optimizer knows too much about the latter and generates code that is incompatible with the former.



[Update to clarify a little]

<unistd.h>

is an example of what I mean about platform specific functionality. It generally works great with C ++, and neither the library nor the compiler developers will mess with it because it would be too annoying. So go ahead and call getpid()

or pipe()

or whatever.

But keep in mind that mixing paradigms raise all kinds of questions. To name just a few from the head:

  • Can you call new

    from a signal handler?
  • Can you use dup2

    0 for descriptor for redirection cin

    ?
  • What POSIX functions can be safely called during static initialization (i.e. before execution main

    )?

These issues and others like them are not covered by any specification. Answers are implementation specific and may change between versions.

Having said all that ... Almost every non-trivial C ++ program relies on platform-specific functionality that is exposed by some C interface. So what you are describing will work great in practice if you (a) have an understanding of what is happening "under the hood"; (b) have reasonable expectations; and (c) do not try to mix the standard and platform paradigms.

+5


source


1) Yes: standard headers are standard. You can safely use them regardless of platform.

2) Yes: you can mix C headers (for example <stdio.h>

) with C ++ headers (for example <iostream>

) in the same C ++ translation unit.

3) NO: you must NOT use Linux kernel headers in a user-mode program, and vice versa.



Linux kernel headers are for kernel-mode drivers, not for "normal" user-space applications.

Here's a little more information:

+1


source







All Articles