2015年10月13日 星期二

Linux C dev tips

errno.h strerror (errno) dlfcn.h void *handle; handle = dlopen("libkido.so", RTLD_LAZY); if (!handle) { fprintf(stderr, "%s \n", dlerror()); exit(EXIT_FAILURE); } int (*test)() = dlsym (handle,"test3"); if (!test) { fprintf(stderr, "%s \n", dlerror()); exit(EXIT_FAILURE); } int a; a=(*test)();

Linux C program tips

compile object files gcc -c 1.c --> automatically generate 1.o file link object files and generate binary file gcc -o app 1.o 2.o main.o build archives -- static library ar cr libkido.a 1.o 2.o main.o gcc -L . -lkido -o app build shared library gcc -c -fPIC 1.c --> automatically generate 1.o file gcc -shared -fPIC -o libkido.so main.o 1.o 2.o gcc -o app -L . -lkido -Wl,-rpath,./ <-- if rpath is not defined, LD_LIBRARY_PATH need to be defined in environment variable to have the binary locate library file