Hello,
There's a bunch of problems with dynamic loading entities defined in C++ shared libraries:
1) No portable way to define symbol as being imported/exported. All the C++ libraries are forced to reinvent BOOST_SYMBOL_EXPORT and BOOST_SYMBOL_IMPORT like macro [1].
I'm proposing to add following attributes to C++ Standard:
* [[import]] - for importing symbol (same as BOOST_SYMBOL_IMPORT)
* [[export]] - for exporting symbol in default section with default C++ mangling (same as BOOST_SYMBOL_EXPORT)
* [[section "section name"]] - places the symbol into section with specified name, constructs section if it does not exist(close to BOOST_DLL_SECTION [2])
Here are some usage examples:
namespace boost {
// exporting function with C++ mangled name.
[[export]] void export_me(std::sting, variant<int, short>);
// importing function with C++ mangled name
[[import]] void import_me(variant<int, char>, int);
extern "C" {
[[export]] void export_me_c(std::sting v1, variant<int, short> v2) {
export_me(v1, v2);
}
// Now we can load function from shared library using "export_me_c" name:
// dlsym(dlopen(library), "export_me_c");
// importing function with C mangled name
[[import]] void import_me_c(variant<int, char>, int);
}
// Creates section named to_strip and places variable `data` into that section
[[section "to_strip"]] char data[] = "Some minor data that will be stripped later";
}
////////////
// Now we can make nicer macro for header files that declare API, for example:
#if EXPORTING
# define API [[export]]
#else
# define API [[import]]
#endif
#define PLUGIN extern "C" API [[section "plugin"]]
namespace my_namespace {
// Shared object that contains "foo2" could be dynamically loaded or dynamically linked.
PLUGIN void foo2(std::sting);
// Class that will be dynamically linked
class API foo_class {
// ...
};
}
All the proposed attributes are either simple to implement or are already supported by modern compilers (MSVC has dllimport, dllexport, __pragma(section); GCC has dllimport, dllexport, section, visibility).
Any comments are wellcomed!
[1] Boost C++ libraries. Macros controlling shared library symbol visibility
[2] Boost C++ libraries. Macro BOOST_DLL_SECTION
--
Best regards,
Antony Polukhin