第一个静态库程序与调用
一 第一个静态库程序
- 在vs2022中创建一个静态库项目,在新建项目中选择
Static Library,起名为StaticLib1
- 选择Solution Explorer->Property->c/c++->Procompiled Headers->选择Not Using Precompiled Headers
- 删除项目里的所有文件,添加头文件
StaticFunction.h与源文件StaticFunction.cpp
StaticFunction.h
1 2 3 4 5 6
| #ifndef STATICFUNCTION_H #define STATICFUNCTION_H
int sum(int a, int b);
#endif
|
MyStaticLib.cpp
1 2 3 4 5 6
| #include "StaticFunction.h"
int sum(int x, int y) { return x + y; }
|
- 编译生成得到StaticLib1.lib
二 使用c语言控制台程序调用静态库里的函数
1.在vs2022中创建一个控制台程序起名为Library
2.把头文件StaticFunction.h与静态库StaticLib1.lib添加到项目中
3.在项目里添加文件main.cpp
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12
| #include <stdio.h> #include "StaticFunction.h"
#pragma comment(lib, "StaticLib1.lib")
int main(int argc, char* argv[]) { printf("10+20=%d", sum(10, 20)); getchar(); return 0; }
|
- 编译运行
第一个动态库程序与调用
一 第一个动态库程序
在vs2022中创建一个动态库项目,在新建项目中选择Dynamic_Link Library(DLL),起名为Dll1
选择Solution Explorer->Property->c/c++->Procompiled Headers->选择Not Using Precompiled Headers
删除项目里的所有文件,添加头文件DllFunction.h与源文件DllFunction.cpp和dllmain.cpp
DllFunction.h
1 2 3 4 5
| #ifndef DLLFUNCTION_H #define DLLFUNCTION_H extern "C" __declspec(dllexport) int Add(int, int); extern "C" __declspec(dllexport) int Mul(int, int); #endif
|
DllFunction.cpp
1 2 3 4
| #include "DllFunction.h"
int Add(int a, int b){return a + b;} int Mul(int c, int d){return c * d;}
|
dllmain.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <windows.h>
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
|
- 编译生成
二 使用c语言控制台程序调用动态库里的函数
调用动态库的方法一:静态加载(显式调用)
1.在vs2022中创建一个控制台程序起名为Library
2.把头文件DllFunction.h与动态库Dll1.dll与Dll1.lib添加到项目中
3.在项目里添加文件main.cpp
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
| #include <stdio.h> #include "DllFunction.h"
#pragma comment(lib, "Dll1.lib")
int main(int argc, char* argv[]) { printf("10+20=%d\n", Add(10, 20)); printf("10x20=%d\n", Mul(10, 20)); getchar(); return 0; }
|
调用动态库的方法二:动态加载(隐式调用)
1.在vs2022中创建一个控制台程序起名为Library
2.把头文件DllFunction.h与动态库Dll1.dll与Dll1.lib添加到项目中
3.在项目里添加文件main.cpp
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include <windows.h>
int main(int argc, char* argv[]) {
printf("Hello World!\n"); HMODULE hmod = LoadLibrary(L"Dll1.dll"); typedef int(*LoadProc)(int x, int y); LoadProc Load_proc = (LoadProc)GetProcAddress(hmod, "Add"); int iRet = Load_proc(10, 20); printf("the Add the value is:%d\n", iRet); getchar(); return 0; }
|
用__declspec(dllimport)完善动态库的调用
一 创建动态库程序
DllFunction.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #ifndef DLLFUNCTION_H #define DLLFUNCTION_H
#ifdef DLLFUNCTION_EXPORTS #define DLL_API _declspec(dllexport) #else #define DLL_API _declspec(dllimport) #endif
extern "C" DLL_API int Add(int, int); extern "C" DLL_API int Mul(int, int); extern "C" int DLL_API varlabel;
#endif
|
DllFunction.cpp
1 2 3 4 5
| #include "DllFunction.h"
int Add(int a, int b) { return a + b; } int Mul(int c, int d) { return c * d; } int varlabel = 10;
|
dllmain.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <windows.h>
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
|
二 使用c语言控制台程序调用动态库里的函数
调用动态库:静态加载(显式调用)
1.在vs2022中创建一个控制台程序起名为Library
2.把头文件DllFunction.h与动态库Dll1.dll与Dll1.lib添加到项目中
3.在项目里添加文件main.cpp
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <stdio.h> #include "DllFunction.h"
#pragma comment(lib, "Dll1.lib")
int main(int argc, char* argv[]) { printf("10+20=%d\n", Add(10, 20)); printf("10x20=%d\n", Mul(10, 20)); printf("varlabel=%d\n", ); getchar(); return 0; }
|
使用def导出动态库调用
一 创建动态库程序
- 选择Solution Explorer->Property->Linker->Input->Module Definition File->输入def文件名Dll1.def
- 所有项目文件
DllFunction.h,DllFunction.cpp,dllmain.cpp,Dll1.def
DllFunction.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #ifndef DLLFUNCTION_H #define DLLFUNCTION_H
#ifdef DLLFUNCTION_EXPORTS #define DLL_API _declspec(dllexport) #else #define DLL_API _declspec(dllimport) #endif
extern "C" DLL_API int Add(int, int); extern "C" DLL_API int Mul(int, int); extern "C" int DLL_API varlabel;
#endif
|
DllFunction.cpp
1 2 3 4 5
| #include "DllFunction.h"
int Add(int a, int b) { return a + b; } int Mul(int c, int d) { return c * d; } int varlabel = 10;
|
dllmain.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <windows.h>
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; }
|
Dll1.def
1 2 3 4
| LIBRARY DLL1 EXPORTS Add @1 Mul @2
|
二 使用c语言控制台程序调用动态库里的函数
调用动态库的方法:动态加载(隐式调用)
1.在vs2022中创建一个控制台程序起名为Library
2.把动态库Dll1.dll添加到项目中
3.在项目里添加文件main.cpp
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <stdio.h> #include <windows.h>
int main(int argc, char* argv[]) {
printf("Hello World!\n"); HMODULE hmod = LoadLibrary(L"Dll1.dll"); typedef int(*LoadProc)(int x, int y); LoadProc Load_proc = (LoadProc)GetProcAddress(hmod, "Add"); int iRet = Load_proc(10, 20); printf("the Add the value is:%d\n", iRet); getchar();
return 0; }
|