Go to main content
Oracle® Developer Studio 12.6: C++ User's Guide

Exit Print View

Updated: July 2017

14.7 Building a Library That Has a C API

If you want to build a library that is written in C++ but that can be used with a C program, the library must have a C API (application programming interface) . This means that all the exported functions are declared extern "C". Note that you can do so only for ordinary functions and data, not for class members. In addition, all types used in the interface must be types that can be declared in C.

If a C-interface library needs C++ runtime support (for example, you use class types or C++ library functions internally) and you are linking with the C compiler cc, you must also link with the appropriate C++ runtime libraries, as described below.

You should create a dynamic (shared) library instead of a static (archive) library. A dynamic library will be self-contained with its own references to the C++ runtime libraries it depends on. If you create a static library, users of your library will need to link the C++ runtime libraries into their program, and will need to either use CC (not cc) to link the program or duplicate the ld command line that CC would generate.

When you create your dynamic library, list the C++ runtime libraries associated with the compilation mode (-compat=x, -std=x) you are using. These libraries are listed in Building Dynamic (Shared) Libraries.

It may be possible to build your library without a dependency on any of the C++ runtime libraries. At a minimum, you would need to follow these guidelines:

  • Do not use the array forms of new or delete.

  • Do not use single-object forms of new or delete, unless you provide your own versions of operator new and operator delete.

  • Do not use exceptions. (Compile with –noex.)

  • Do not use runtime type information (RTTI). (Compile with –features=no%rtti.)

  • Do not use dynamic_cast.

  • Do not use any of the standard C++ headers.