1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
/*
Example program for the claw::dynamic_library class.
*/
#include <iostream>
#include <claw/dynamic_library.hpp>
#ifdef _WIN32
# define DLLEXPORT __declspec(dllexport)
#else
# define DLLEXPORT
#endif
typedef void (*function_type)( const std::string& s );
extern "C"
DLLEXPORT void in_program( const std::string& s )
{
std::cout << "in_program : " << s << std::endl;
}
int main( int argc, char** argv )
{
if ( argc < 2 )
{
std::cout << argv[0] << " method_name...\n";
std::cout << "\tThe first method_name is searched in the current "
<< "program\n";
std::cout << "\tThe next arguments are searched in the ./libfunc.so "
<< "shared library." << std::endl;
}
else
{
/* Load the library. */
#ifdef _WIN32
claw::dynamic_library dl( "./libfunc.dll" );
#else
claw::dynamic_library dl( "./libfunc.so" );
#endif
/* Load the current program. */
claw::dynamic_library current_prog( argv[0], true );
function_type f;
/* Search in the current program. */
if ( current_prog.have_symbol( argv[1] ) )
{
f = current_prog.get_symbol<function_type>( argv[1] );
f( "The string passed to a method in the current program" );
}
else
std::cout << "Symbol '" << argv[1]
<< "' not found in the current program." << std::endl;
/* We try to find the next symbols passed as arguments */
for (int i=2; i<argc; ++i)
if ( dl.have_symbol( argv[i] ) )
{
f = dl.get_symbol<function_type>( argv[i] );
f( "The string passed to the method" );
}
else
std::cout << "Symbol '" << argv[i] << "' not found." << std::endl;
}
return 0;
}
|