#include <stdio.h> #include <dlfcn.h> typedef int(*fn_max)(int a, int b); int main() { printf("entery in main "); void* hdl = dlopen("liblinuxdlltest.so.1.0", RTLD_NOW);//RTLD_LAZY if (!hdl) { printf("load dll fail "); return; } fn_max max = (fn_max)dlsym(hdl, "max"); char* perr = dlerror(); if (perr) { printf("load symbol failed: %s ", perr); return; } int result = max(200, 123); printf("the max is %d ", result); dlclose(hdl); printf("exit main "); return 0; }
dllmain.h
#pragma once int max(int a, int b);
dllmain.c
#include <stdio.h> //int main() //{ // printf("hello from ConsoleApplication7! "); // // printf("this is my so dll test project"); // return 0; //} /************************************************************************/ /* 构造函数 */ /************************************************************************/ void __attribute__((constructor)) x_init(void) { printf("----so lib is loaded "); } /************************************************************************/ /* 析构函数 */ /************************************************************************/ void __attribute__((destructor)) x_fini(void) { printf("----so lib is unload "); } int max(int a, int b) { printf("enter in max .. "); return a > b ? a : b; }
工程源码下载: http://download.csdn.net/download/jiftlixu/10236153
导出符号控制:
https://www.cnblogs.com/lidabo/p/5703890.html