Cpp programming on Linux using library dynamic advanced

Main.c

#include

#include 
#include 

typedef void (*simple_demo_function)(void);

int main(int argc, char * argv[]){

	const char *error;
	void *module;
	simple_demo_function demo_function;

	module = dlopen(argv[1], RTLD_LAZY);
	if(!module){
		fprintf(stderr, "Couldn't open %s %sn", argv[1], dlerror());
		exit(1);
	}
	dlerror();
	demo_function = dlsym(module, "hello");

	if(error = dlerror()){
	fprintf(stderr, "Couldn't find hello: %sn", error);
	exit(1);
	}
	(*demo_function)();		

	dlclose(module);
	return 0;
}

Libhello.h

void hello(void);

Libhello.c

#include 
void hello(void){
	printf("Hello lib n");
}

Makefile

main: main.o
	gcc -g -o main main.o -ldl
main.o: libdyn.so
	gcc -g -c main.c
libdyn.so: libhello.o
	gcc -g -shared -nostdlib -o libdyn.so libhello.o
libhello.o:
	gcc -g -c -fPIC libhello.c
clean:
	rm -f *.o *~ main *.so

Run dynamic library: .so file is located in "/root/Cpp3"

[root@localhost Cpp3]# export LD_LIBRARY_PATH="/root/Cpp3"
[root@localhost Cpp3]# ./main libdyn.so

Loading