标签:
静态链接库在链接时,编译器会将 .obj 文件和 .LIB 文件组织成一个 .exe 文件,程序运行时,将全部数据加载到内存。
- #include<stdio.h>
- extern int add(int, int); // 也可以是 _declspec(dllimport) int add(int, int);
- extern int sub(int, int); // 也可以是 _declspec(dllimport) int sub(int, int);
- int main(){
- int a=10, b=5;
- printf("a+b=%d\n", add(a, b));
- printf("a-b=%d\n", sub(a, b));
- return 0;
- }
- #include<stdio.h>
- #pragma comment(lib, "dllDemo.lib")
- _declspec(dllimport) int add(int, int);
- _declspec(dllimport) int sub(int, int);
- int main(){
- int a=10, b=5;
- printf("a+b=%d\n", add(a, b));
- printf("a-b=%d\n", sub(a, b));
- return 0;
- }
- #ifndef _DLLDEMO_H
- #define _DLLDEMO_H
- #pragma comment(lib, "dllDemo.lib")
- _declspec(dllexport) int add(int, int);
- _declspec(dllexport) int sub(int, int);
- #endif
- #include<stdio.h>
- #include "dllDemo.h"
- int main(){
- int a=10, b=5;
- printf("a+b=%d\n", add(a, b));
- printf("a-b=%d\n", sub(a, b));
- return 0;
- }
- #include<stdio.h>
- #include<stdlib.h>
- #include<windows.h> // 必须包含 windows.h
- typedef int (*FUNADDR)(); // 指向函数的指针
- int main(){
- int a=10, b=5;
- HINSTANCE dllDemo = LoadLibrary("dllDemo.dll");
- FUNADDR add, sub;
- if(dllDemo){
- add = (FUNADDR)GetProcAddress(dllDemo, "add");
- sub = (FUNADDR)GetProcAddress(dllDemo, "sub");
- }else{
- printf("Fail to load DLL!\n");
- system("pause");
- exit(1);
- }
- printf("a+b=%d\n", add(a, b));
- printf("a-b=%d\n", sub(a, b));
- system("pause");
- return 0;
- }
动态链接库DLL的加载:隐式加载(载入时加载)和显式加载(运行时加载)
标签:
原文地址:http://blog.csdn.net/shuimanting520/article/details/51335988