windows库的创建和使用:静态库+动态库
windows库的创建和使用:静态库+动态库
一、静态库的创建和使用
1. 静态库创建
(1)首先创建projecttest,測试代码例如以下:
1) test.h
|
void test_print(); |
2) test.cpp
|
#include "test.h" #include <stdio.h> void test_print() { printf("test_print in static lib."); } |
3)右击projecttest:属性——>配置属性——>常规——>配置类型:选择静态库lib
4)编译project(build),然后会在解决方式里的Debug下会生成test.lib。
2. 使用静态库 = lib文件 + 头文件
1)建一个新的projectuselib
2)点击资源文件——>加入已有项——>加入刚才生成的库test.lib
3)右击属性——>VC++文件夹——>包括文件夹:将test.h所在文件夹加入进来
project代码例如以下:
1)main.cpp
|
#include "test.h" #include<stdio.h> int main() { test_print(); getchar(); } |
调试就能看到调用成功。
二、 动态库的创建与使用
1. 静态载入动态库
1)创建projectstatic_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 <stdio.h> 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).
2)静态载入的方式使用动态库
首先创建一个dll.h
|
//导入类 class __declspec(dllimport) static_test { public: void print(); }; //导入函数 __declspec(dllimport) void static_function(); //导入对象 extern __declspec(dllimport) static_test static_ObjTest; |
__declspec(dllimport)表明该项是从动态库导入的外部导入项。
创建一个測试projectuselib
main.cpp
|
#include "dll.h" #include<stdio.h> int main() { static_test st; st.print(); static_function(); static_ObjTest.print(); getchar(); } |
同一时候须要将static_load.lib和static_load.dll放到同一文件夹下,且.lib文件导入到资源文件下。
2 动态载入动态库
1. 为差别对待这里建立projectdynamic_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 <stdio.h> 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)。
创建一个projectuselib
|
#include<stdio.h> #include<Windows.h> 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。至于动态导入类有点麻烦,故在此先不讨论。
有一篇非常好地资料:http://www.cnblogs.com/skynet/p/3372855.html
动态载入dll中的类:http://www.cppblog.com/codejie/archive/2009/09/24/97141.html
windows库的创建和使用:静态库+动态库的更多相关文章
- linux静态与动态库创建及使用实例
一,gcc基础语法: 基本语法结构:(由以下四部分组成) gcc -o 可执行文件名 依赖文件集(*.c/*.o) 依赖库文件及其头文件集(由-I或-L与-l指明) gcc 依赖文件集(*.c/*.o ...
- Windows 下VC++6.0制作、使用动态库和静态库
Windows 下VC++6.0制作.使用动态库和静态库 一.VC++6.0制作.使用静态库 静态库制作 1.如图一在VC++6.0中new一个的为win32 static library工程并新建一 ...
- 动态库与静态库的学习 博主写的很好 静态库 编译的时候 需要加上 static 动态库编译ok运行不成功就按照文章中的方法修改
来源连接 http://www.cnblogs.com/skynet/p/3372855.html C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库 ...
- windows 静态和动态库
c++中共有两种库:1.动态链接库LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic link library.(这 ...
- Windows、Linux、Mac OSX编译jni动态库
在不同平台下默认调用不同名字的动态库,在Windows平台调用name.dll,在Linux平台调用libname.so,在OSX下调用libname.jnilib.不同平台下的编译的方法也有些区别. ...
- c++动态库封装及调用(1、动态库介绍)
1.一个程序从源文件编译生成可执行文件的步骤: 预编译 --> 编译 --> 汇编 --> 链接 (1)预编译,即预处理,主要处理在源代码文件中以“#”开始的预编译指令,如宏展开 ...
- c++生成的动态库移到其他电脑上,动态库不能运行
最近的一个项目中遇到了一个问题,C++的一个动态库在我自己的电脑上可以被C#程序引用,我把程序安装到其他电脑上出现了异常,提示找不到DLL,偶然间发现我安装vsc++,C#的程序就不会报错.因为这个C ...
- Linux链接库三(C跟C++之间动态库的相互调用)
http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...
- Windows下静态库与动态库的创建与使用
Windows下静态库与动态库的创建与使用 学习内容:本博客介绍了Windows下使用Visual C++ 6.0制作与使用静态库与动态库的方法. --------CONTENTS-------- 一 ...
随机推荐
- js 树结构数据遍历条件判断
代码: /** * 树结构数据条件过滤 * js 指定删除数组(树结构数据) */ function filter (data, id) { var newData = data.filter(x = ...
- 【leetcode】solution in java——Easy2
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...
- picasso 在魅族手机无法加载缩略图的bug
09-03 12:46:30.722 19088-19088/com.tongyan.subway.maintenance W/PerfScheduler: Cann't find the ListV ...
- 为wget命令设置代理
实验环境:ubuntu 12.04 LTS goagent 方法一.在环境变量中设置代理 export http_proxy=http://127.0.0.1:8087 方法二.使用配置文件 为wg ...
- oracle 两个网络不通的远程数据库如何将一个库中的表数据导入到另一个库中?
1.情景展示 本地可以直接连接2个不同的远程数据库: 两个数据库由于网络不通,无法建立DBLINK完成数据传输: 将A库中C表的数据插入到B库中C表,如何快速实现? 2.解决方案 通过kettle ...
- 〖Linux〗Shell十进制数值转换十六进制
dec2hex(){ printf } a=$(dec2hex ) echo $a
- JAVA中的CountDownLatch、CyclicBarrier、Semaphore的简单测试
因公司需要做一个对于CountDownLatch的分享,特写了此blog. 具体细节可以参见:小结java自带的跟锁相关的一些类 在做这个分享的过程中发现了Main和junit的运行的区别,在另外一个 ...
- DLib库Base64编解码示例
代码 #include <iostream> #include <fstream> #include <sstream> #include <string&g ...
- .NET 中的 async/await 异步编程
原文出处: Teroy 的博客 前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入的,由于之前对于异步编程不是很了解,所以花费了一些时间学习一下相关 ...
- Publish to a Linux Production Environment
Publish to a Linux Production Environment By Sourabh Shirhatti In this guide, we will cover setting ...