Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use the POSIX C libraries in c++?

I am new in the field of Linux system programming.I currently program in C and want to switch to c++.

Can we use all the functions defined in POSIX C libraries in c++ without any change ?

like image 765
Shikhar Srivastava Avatar asked Feb 11 '23 07:02

Shikhar Srivastava


1 Answers

In principle you should be able to use any C API from C++; the language includes features to facilitate it, and most C library authors are aware that people want to do this and will take the appropriate steps. For the system programming interfaces specified by POSIX, C++ compatibility is an explicit design goal.

However, you may still encounter problems. In my experience, the most common problems are:

  1. C API headers often dump hundreds of symbols into the global namespace. Some of those symbols may conflict with C++ library symbols, which will get you in trouble if you using namespace std (but you weren't doing that, right?)
  2. C API headers often make heavy use of macros, including macro names that, yep, might conflict with C++ library symbols; std:: won't save you there.
  3. Compiling your program in a strict conformance mode (e.g. -std=c++11 -D_XOPEN_SOURCE=700) may expose bugs in system headers. This is more likely to happen with C++ than C.
  4. A small handful of the POSIX APIs have abnormal control-flow behavior that may interact poorly with C++ exceptions and destructors, depending on how thorough your C library implementor was about avoiding the problem. setjmp and longjmp are obviously a concern here (has anyone done a C library that implements those on top of DWARF-style exception handling?) but so are fork, setcontext and friends, pthread_cancel, pthread_cleanup_push, and probably a few others I can't remember off the top of my head. (I recall a giant, ultimately inconclusive argument between Ulrich Drepper and the GCC C++ guys back in 2004 or so about exactly how pthread_cancel should behave in the presence of destructors.)

If you go beyond POSIX, you may also have problems with:

  1. Headers that don't bother to wrap all the declarations in an extern "C" block when compiled as C++, which means all the function names get mangled when they shouldn't have been, and the link fails.
  2. Headers that don't even bother to stick to the intersection of C and C++. In the worst case, this can cause failures that don't manifest until the program is run. The most common instances of this are:
    • Blithely using some C++ keyword as a declared-name (e.g. int template;)
    • Assuming that void * is assignment compatible with other pointer types (e.g. that it is not necessary to cast the result of malloc)
    • Assuming that struct foo; does not define a typedef-name foo

Note that the headers specified by POSIX frequently contain system-specific extensions that have not been as carefully thought out as the POSIX interfaces themselves.

like image 87
zwol Avatar answered Feb 13 '23 04:02

zwol