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. JSP之include动态包含与静态包含

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6044676.html JSP中,include是一个经常用到的标签.当应用程序中所有的页面的某些部分(如标题. ...

  2. 〖Android〗代理与正常网络分开同步CyangenMod源码

    为了同步CyanogenMod源代码,你也学会了FQ,对吗? 通常 .repo/manifest.xml 文件有Google AOSP的Project,也有Github的Project: 访问Gith ...

  3. 〖Linux〗穿越城墙之后,直接连接国内网站的路由配置

    因为有需要做Android相关的开发工作,很多时候要穿越之后才能做事情: 如Android文件加密预研.Android NDK/SDK的下载,都需要使用得到Google: 但是穿越之后,访问国内网站就 ...

  4. kibana对logstash监控获取不到数据

    需求: kibana监控elasticsearch+kibana+logstash的性能寻找,性能瓶颈! 发现启动logstash加载logstash.yml,不读取配置好的xpack.monitor ...

  5. 编译安装linux内核步骤

    编译安装linux内核步骤: 一.获取内核源码 源码网址:www.kernel.org 二.解压内核源码 首先以root帐号登录,然后进入/usr/src子目录.如果用户在安装Linux时,安装了内核 ...

  6. Android实现中文汉字笔划(笔画)、中文拼音排序、英文排序

    发布时间:2018-11-16   技术:Android   概述 最近要做一个类似微信的,在登录界面选择国家地区的功能,微信有中文汉字笔画排序以及中文拼音排序等几种方式,如下所示: 简体中文 拼音排 ...

  7. [转载]Linux下终端字体颜色设置方法

    原文地址:Linux下终端字体颜色设置方法作者:router 网上类似的文章有很多,但是都是转来转去的,没有经过测试,按照很多文章的方法会造成你设置之后的终端在换行和删除输入字符时终端显示会乱七八糟, ...

  8. [POST] What Is the Linux fstab File, and How Does It Work?

    If you’re running Linux, then it’s likely that you’ve needed to change some options for your file sy ...

  9. 【RS】List-wise learning to rank with matrix factorization for collaborative filtering - 结合列表启发排序和矩阵分解的协同过滤

    [论文标题]List-wise learning to rank with matrix factorization for collaborative filtering   (RecSys '10 ...

  10. java hibernate Criteria 删除数据 delete data 2种方法

    public String deleteByUserAccount(String account) { 方式一: Session session = this.getCurrentSession(); ...