写了一个最简单的动态链接库程序,使用g++命令行编译。怕以后忘记,就把它记到blog中。
动态库导出头文件:
/** * file: dll.h * Powered by JGood 2009-09-22 */ #ifndef __dll_h__ #define __dll_h__ #ifdef __MY_DLL_LIB__ #define DLL_EXPORT extern "C" __declspec(dllexport) #else #define DLL_EXPORT extern "C" __declspec(dllimport) #endif DLL_EXPORT int jmax(int x, int y); #endif
动态库实现:
编译成obj文件:g++ -c -o dll.obj dll.cpp
链接obj,生成dll: g++ -shared -o dll.so dll.obj
/** * file: dll.cpp * Powered by JGood 2009-09-22 */ #define __MY_DLL_LIB__ #include "dll.h" int jmax(int x, int y) { return x > y ? x : y; }
调用动态库:
直接编译成exe: g++ main.cpp dll.so -o main.exe
/** * file: main.cpp * Powered by JGood 2009-09-22 */ #include "dll.h" #include <iostream> using namespace std; int main() { int a = 20; int b = 40; cout << jmax(a, b) << endl; return 0; }