cpp-dlopen

cpp-dlopen.git
git clone git://git.lenczewski.org/cpp-dlopen.git
Log | Files | Refs

myexe.cpp (542B)


      1 #include <dlfcn.h>
      2 
      3 #include <stdio.h>
      4 
      5 int
      6 main(int argc, char **argv)
      7 {
      8 	if (argc < 2) {
      9 		fprintf(stderr, "Usage: %s <path/to/lib.so>\n", argv[0]);
     10 		return 1;
     11 	}
     12 
     13 	char *path = argv[1];
     14 
     15 	void *lib = dlopen(path, RTLD_NOW);
     16 	if (!lib) {
     17 		fprintf(stderr, "dlopen(libmylib.so): %s\n", dlerror());
     18 		return 1;
     19 	}
     20 
     21 	void *fn = dlsym(lib, "mylib_foo");
     22 	if (!fn) {
     23 		fprintf(stderr, "dlsym(lib, mylib_foo): %s\n", dlerror());
     24 		return 2;
     25 	}
     26 
     27 	int res = ((int (*)(int, int)) fn)(1, 2);
     28 	if (res != 3)
     29 		return 3;
     30 
     31 	dlclose(lib);
     32 
     33 	return 0;
     34 }