Albert - namccart/201507_codesprint GitHub Wiki
Search tool (app_search.cc)
Build with: g++ app-search.cc -o app-search -ldl
#include <iostream>
#include <dirent.h> //directory listing routines
#include <dlfcn.h> //library linking routines
using namespace std;
int main(int argc, char **argv) {
//set up vars
DIR *dp;
struct dirent *dir_s;
string dir = "./apps-lib";
//attempt to open directory
cout << " Searching apps path: " << dir << endl;
dp = opendir( dir.c_str() );
if(dp == NULL){
cout << " -Application directory not found." << endl;
return -1;
}
//browse through files
while ((dir_s = readdir(dp)) != NULL) {
if( dir_s->d_type != 0x8 ) continue;
cout << "\n -File found: " << dir_s->d_name << endl;
string file = dir + "/" + dir_s->d_name;
//attempt to load as a library
void *p_lib;
p_lib = dlopen( file.c_str(), RTLD_LAZY );
if ( p_lib == NULL){
cout << " - Not a valid lib." << endl;
continue;
}
cout << " - File opened as library." << endl;
//look for our entry point of interest
void (*p_fn)(void);
*(void **)(&p_fn) = dlsym(p_lib, "gc_register");
if(p_fn == NULL){
cout << " - Not a valid lib. Registration procedure not found." << endl;
dlclose(p_lib);
continue;
}
//register your library
(*p_fn)();
if(p_lib) dlclose(p_lib);
}
if(dp) closedir(dp);
return 0;
}
Common App Header (common_app.hh)
#ifndef COMMON_APP_HH
#define COMMON_APP_HH
#ifdef __cplusplus
extern "C"{
#endif
void gc_register(void);
#ifdef __cplusplus
}
#endif
#endif //COMMON_APP_HH
App .so Example (lte_app.cc)
Build with: g++ -Wall -shared -fPIC -o lte.so lte_app.cc
#include "common_app.hh"
#include <iostream>
void gc_register(void)
{
std::cout << "...LIB INTERNAL LTE_APP REGISTRATION..." << std::endl;
}