windows库的创建和使用:静态库+动态库
2020-12-13 04:53
标签:静态库 动态库 创建 使用 windows (1)首先创建工程test,测试代码如下: 1) test.h void test_print(); 2) test.cpp #include "test.h" #include void test_print() { printf("test_print in static lib."); } 3)右击工程test:属性——>配置属性——>常规——>配置类型:选择静态库lib 4)编译工程(build),然后会在解决方案里的Debug下会生成test.lib。 1)建一个新的工程uselib 2)点击资源文件——>添加已有项——>加入刚才生成的库test.lib 3)右击属性——>VC++目录——>包含目录:将test.h所在目录添加进来 工程代码如下: 1)main.cpp #include "test.h" #include int main() { test_print(); getchar(); } 调试就能看到调用成功。 1)创建工程static_load static_dll.h //导出类 class __declspec(dllexport) static_test { public: void print(); }; //导出函数 __declspec(dllexport) void static_function(); //导出对象 extern __declspec(dllexport) static_test static_ObjTest; static_dll.cpp #include "static_dll.h" #include void static_test::print() { printf("static_test::print() in static load dll.\n"); } void static_function() { printf("static_function in static load dll.\n"); } static_test static_ObjTest; __declspec(dllexport)表示作为动态库的导出项,否则会找不到变量或者函数或者类等。配置类型选择:动态库(dll). 首先创建一个dll.h //导入类 class __declspec(dllimport) static_test { public: void print(); }; //导入函数 __declspec(dllimport) void static_function(); //导入对象 extern __declspec(dllimport) static_test static_ObjTest; __declspec(dllimport)表明该项是从动态库导入的外部导入项。 创建一个测试工程uselib main.cpp #include "dll.h" #include int main() { static_test st; st.print(); static_function(); static_ObjTest.print(); getchar(); } 同时需要将static_load.lib和static_load.dll放到同一目录下,且.lib文件导入到资源文件下。 1. 为区别对待这里建立工程dynamic_load dynamic_dll.h //导出类 class __declspec(dllexport) dynamic_test { public: void print(); }; //导出函数 __declspec(dllexport) void dynamic_function(); //导出对象 extern __declspec(dllexport) dynamic_test dynamic_ObjTest; dynamic_dll.cpp #include "dynamic_dll.h" #include void dynamic_test::print() { printf("dynamic_test::print() in dynamic load dll."); } void static_function() { printf("dynamic_function in dynamic load dll."); } dynamic_test dynamic_ObjTest; 配置类型选择:动态库(dll).然后编译(build)。 创建一个工程uselib #include #include int main() { HINSTANCE hDLL = LoadLibraryA("dynamic_load.dll"); if(hDLL==NULL) { FreeLibrary(hDLL); return 0; } typedef void(_stdcall*func)(); func f; f = (func)GetProcAddress(hDLL,"dynamic_function"); if(f) (*f)(); getchar(); } 注意我们必须在LoadLibraryA中传入dll的准确路径,且若使用char*的路径,则使用LoadLibraryA,否则若是unicode则使用LoadLibraryW。至于动态导入类有点麻烦,故在此先不讨论。 windows库的创建和使用:静态库+动态库 标签:静态库 动态库 创建 使用 windows 原文地址:http://blog.csdn.net/liaoyoujinb/article/details/37952305windows库的创建和使用:静态库+动态库
一、静态库的创建和使用
1. 静态库创建
2. 使用静态库 = lib文件 + 头文件
二、 动态库的创建与使用
1. 静态加载动态库
2)静态加载的方式使用动态库
2 动态加载动态库