To use this module, include ccbase/dynamic.hpp . Please note that this module currently works on OS X and Linux only.

This module does the following two things:

Loading Dynamic Libraries¶

Suppose that you have a library called test.dll , which defines two symbols of interest: msg, a std::string and test() , a function with the signature int(const std::string&, const std::string&) . Assuming that both symbols have C linkage, then you could load them as follows:

using sig = int ( const std :: string & , const std :: string & ); auto img = cc :: image { "test.dll" , cc :: binding_mode :: lazy }; auto test = cc :: load < signature > ( "test" , img ). get (); auto & msg = cc :: load < std :: string > ( "msg" , img ). get ();

The function load<T>() returns an expected<T> object, which contains either the object loaded from the symbol, or the exception that prevented the object from being loaded. Invoking get() on the expected<T> objects throws the stored exception if the object is in an invalid state, and returns the stored value otherwise.

Notice that when we loaded msg from the image, we accepted the return value by reference rather than by value. This is because load<T>() returns a reference to the object at the symbol’s address, so mutating the reference will also change the data at the symbol’s address in memory.

One can also query information about the loaded objects, as in the following example:

using sig = std :: string ( std :: string , std :: string ); auto img = cc :: image { "test.dll" , cc :: binding_mode :: lazy }; auto test = cc :: load < signature > ( "test" , img ). get (); auto test_info = cc :: query ( test ). get (); cc :: println ( test_info . name ()); // Prints "test". cc :: println ( test_info . address ()); // Prints the address of `test`.