现在的位置: 首页 > 综合 > 正文

windows库的创建和使用:静态库+动态库

2018年02月19日 ⁄ 综合 ⁄ 共 2581字 ⁄ 字号 评论关闭
文章目录

windows库的创建和使用:静态库+动态库

 

一、静态库的创建和使用

1. 静态库创建

1)首先创建工程test,测试代码如下:

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)右击工程test:属性——>配置属性——>常规——>配置类型:选择静态库lib

4)编译工程(build),然后会在解决方案里的Debug下会生成test.lib

 

2. 使用静态库 = lib文件 头文件

1)建一个新的工程uselib

2)点击资源文件——>添加已有项——>加入刚才生成的库test.lib

3)右击属性——>VC++目录——>包含目录:将test.h所在目录添加进来

工程代码如下:

1)main.cpp

#include "test.h"

#include<stdio.h>

int main()

{

      test_print();

     getchar();

}

调试就能看到调用成功。

 

二、 动态库的创建与使用

1. 静态加载动态库

1)创建工程static_load

static_dll.h

//导出类

class __declspec(dllexport) static_test

{

public:

         void print();

};

//导出函数

__declspec(dllexportvoid 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(dllimportvoid static_function();

//导对象

extern __declspec(dllimport) static_test  static_ObjTest;

__declspec(dllimport)表明该项是从动态库导入的外部导入项。

创建一个测试工程uselib

main.cpp

#include "dll.h"

#include<stdio.h>

int main()

{

        static_test st;

        st.print();

       static_function();

       static_ObjTest.print();

       getchar();

}

同时需要将static_load.libstatic_load.dll放到同一目录下,.lib文件导入到资源文件下。

 

2 动态加载动态库

1. 为区别对待这里建立工程dynamic_load

dynamic_dll.h

//导出类

class __declspec(dllexport) dynamic_test

{

public:

        void print();

};

//导出函数

__declspec(dllexportvoid 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)

 

创建一个工程uselib

#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

抱歉!评论已关闭.