build a dynamic library (dll)
1) with extern "C" __declspec(dllexport) in the prototype:
cl.exe /LD hello.cpp
2) with a def file
cl.exe /LD /DEF hello.def hello.cpp
build static library
commands:
1) generate obj file
cl.exe /c a.cpp b.cpp
2) generate lib file
lib.exe a.obj b.obj /OUT:mylib.lib
generate import library with lib
lib.exe /DEF:hello.def
link with dynamic library
cl.exe /c main.cpp
link.exe main.obj hello.lib
/MT and /MD
/MTmulti-threaded (implies static lib)/MDmulti-threaded dynamic (impliesdll)- No
/MLanymore.
useful options of dumpbin
/HEADERS/DEPENDENTS/EXPORTS/IMPORTS/SYMBOLSfor object files
more see: DUMPBIN Options
calling conventions
__cdecl:
- default.
- Caller clean the stack.
- Underscore(_) is prefixed to names, except exported.
/Gd
__stdcall:
- used to call Win32 APIs.
- Callee clean the stack.
- Underscore(_) is prefixed to the name.
/Gz
x64
- x64 just uses the __fastcall calling convention and a RISC-based exception-handling model.
- The __fastcall model uses registers for the first four arguments and the stack frame to pass the other parameters.
- /Gd, /Gr, /Gv, /Gz (Calling Convention)
- Overview of x64 Calling Conventions
本文详细介绍了使用Visual C++创建动态链接库(DLL)及静态库的方法,包括使用extern C __declspec(dllexport)和def文件构建DLL,以及生成对象文件和库文件的过程。此外还探讨了不同类型的调用约定(__cdecl, __stdcall)及其在x64平台上的表现。
3359

被折叠的 条评论
为什么被折叠?



