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库的创建和使用:静态库+动态库的更多相关文章

  1. linux静态与动态库创建及使用实例

    一,gcc基础语法: 基本语法结构:(由以下四部分组成) gcc -o 可执行文件名 依赖文件集(*.c/*.o) 依赖库文件及其头文件集(由-I或-L与-l指明) gcc 依赖文件集(*.c/*.o ...

  2. Windows 下VC++6.0制作、使用动态库和静态库

    Windows 下VC++6.0制作.使用动态库和静态库 一.VC++6.0制作.使用静态库 静态库制作 1.如图一在VC++6.0中new一个的为win32 static library工程并新建一 ...

  3. 动态库与静态库的学习 博主写的很好 静态库 编译的时候 需要加上 static 动态库编译ok运行不成功就按照文章中的方法修改

    来源连接   http://www.cnblogs.com/skynet/p/3372855.html C++静态库与动态库 这次分享的宗旨是--让大家学会创建与使用静态库.动态库,知道静态库与动态库 ...

  4. windows 静态和动态库

    c++中共有两种库:1.动态链接库LIB包含了函数所在的DLL文件和文件中函数位置的信息(入口),代码由运行时加载在进程空间中的DLL提供,称为动态链接库dynamic link library.(这 ...

  5. Windows、Linux、Mac OSX编译jni动态库

    在不同平台下默认调用不同名字的动态库,在Windows平台调用name.dll,在Linux平台调用libname.so,在OSX下调用libname.jnilib.不同平台下的编译的方法也有些区别. ...

  6. c++动态库封装及调用(1、动态库介绍)

    1.一个程序从源文件编译生成可执行文件的步骤: 预编译 -->  编译 -->  汇编 --> 链接 (1)预编译,即预处理,主要处理在源代码文件中以“#”开始的预编译指令,如宏展开 ...

  7. c++生成的动态库移到其他电脑上,动态库不能运行

    最近的一个项目中遇到了一个问题,C++的一个动态库在我自己的电脑上可以被C#程序引用,我把程序安装到其他电脑上出现了异常,提示找不到DLL,偶然间发现我安装vsc++,C#的程序就不会报错.因为这个C ...

  8. Linux链接库三(C跟C++之间动态库的相互调用)

    http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...

  9. Windows下静态库与动态库的创建与使用

    Windows下静态库与动态库的创建与使用 学习内容:本博客介绍了Windows下使用Visual C++ 6.0制作与使用静态库与动态库的方法. --------CONTENTS-------- 一 ...

随机推荐

  1. Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  2. 转 php安装错误configure: error: Please reinstall the libcurl distribu

    今天配置一台server的php支持curl的时候, 出现如下报错 checking for cURL in default path... not foundconfigure: error: Pl ...

  3. 输出数组第k大的元素

    用快速排序的思想输出数组第k大的元素: #include<iostream> #include<algorithm> using namespace std; //递归实现:返 ...

  4. Chrome 好玩的插件

    1 Lastpass : 用来把往各个网址的密码云端存储. 2 EditThisCookie  : 查看网页的Cookie 3 Postman Interceptor :  用来配合Postman 进 ...

  5. 菜鸟云打印接入Demo

    菜鸟云打印接入Demo 0. 接入流程图 1. 连接打印客户端 首先要打开打印客户端,然后使用下面的方法,连接客户端(WebSocket协议): 地址 :  连接打印客户端 function doCo ...

  6. rac安装_grid安装校验报错之grid未建立信任关系

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  7. win32下编译glog

    既然编译第三方库了,google提供的VSproject是老版本的,构建不好升级.所以还是用cmake是王道. 采用out of source 编译,  以下是编译脚本bat: mkdir build ...

  8. codeM 2018 资格赛

    比赛链接:https://www.nowcoder.com/activity/2018codem/index?from=meituan 1.下单 给定若干商品,可以选择打折.满减两种方式. #incl ...

  9. protobuf3 iOS 接入 protobuf

    1.引入官方基础pod 谷歌将protobuf需要使用的基础类封装成了一个pod,因此可以直接安装该pod,不必再手工导入. 如下: pod "Protobuf", :git =& ...

  10. POJ 2976 Dropping tests (0/1分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4654   Accepted: 1587 De ...