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

Exit Print View

Updated: July 2017

14.8 Using dlopen to Access a C++ Library From a C Program

If you want to use dlopen() to open a C++ shared library from a C program, make sure that the shared library has a dependency on the appropriate C++ runtime (libCrun.so.1 for -compat=5).

To do this, add -lCrun for -compat=5 to the command line when building the shared library. For example:

example% CC -G -compat=5... -lCrun

When using -compat=g, -std=c++03, -std=c++11, or -std=c++14 options, you need to link the g++ support libraries instead:

example% CC -G -std=c++11 ... -lstdc++ -lgcc_s -lCrunG3

On Linux, libCrunG3, when used, is linked statically by default. You can get the same behaviour by using:

example% CC -G -std=c++11 ... -lstdc++ -lgcc_s -Bstatic -lCrunG3 -Bdynamic

If the shared library uses exceptions and does not have a dependency on the C++ runtime library, your C program might behave erratically.