How to build a DLL from the command line in Windows using MSVC

后端 未结 5 2013
时光说笑
时光说笑 2020-11-29 22:28

I\'ve been doing C for about 20 years but I\'ve never built a DLL; I\'ve always prefered to statically link.

I use the command line - cl.exe, etc - and gnumake makef

相关标签:
5条回答
  • 2020-11-29 22:52

    Turns out it happens automatically.

    If you have exported functions (e.g. /export, __declspec(dllexport), etc) the linker will automatically generate the .lib file (you do of course need /dll on the linker command line).

    0 讨论(0)
  • 2020-11-29 22:53

    Does cl need any additional arguments, to indicate it is compiling for a DLL? (I know the DLL exported prototypes need __declspec(dllexport)).

    Not since Win3x went away, now you just just need either __declspec(dllexport) or a .DEF file which defines the names of the symbols you want exported. a .def file allows you to export symbols with modified names, so it can still be useful for special cases.

    I know link needs /dll as an argument.

    Yep.

    Will I run lib and link, to produce the .lib and .dll respectively, or will link produce both?

    lib is used only to create static libraries (or to add .objs to your implib) Link will produce both a .dll and an import .lib for it.

    0 讨论(0)
  • 2020-11-29 22:55

    Simlar to Ebow Halm's answer, but using a .def file for listing the exported functions and newer command line arguments:

    "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\vcvars32.bat" && cl /O2 /Iall /Iyour /Iincludes /D_USRDLL /D_WINDLL /DOTHER_DEFINES <libs> <source files> /LD /Fe<dll name> /link /DEF:<def name>.def
    

    References:

    • https://msdn.microsoft.com/en-us/library/34c30xs1.aspx
    • https://msdn.microsoft.com/en-us/library/19z1t1wy.aspx
    0 讨论(0)
  • 2020-11-29 23:04

    The easiest way to find out is to make a MSVC project where you set everything as you want, then enable build logging, make a build and analyze the buildlog for all the commands and their arguments.

    0 讨论(0)
  • 2020-11-29 23:07

    On the command line use:

    cl.exe /LD <files-to-compile>
    

    or, if you prefer the more verbose & explicit version:

    cl.exe /D_USRDLL /D_WINDLL <files-to-compile> <files-to-link> /link /DLL /OUT:<desired-dll-name>.dll
    
    0 讨论(0)
提交回复
热议问题