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

68,885

Solution 1

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).

Solution 2

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

Solution 3

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:

Solution 4

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.

Share:
68,885

Related videos on Youtube

denfromufa
Author by

denfromufa

Currently High-Performance Machine Learning Research at Total. Python, data science, machine learning, deep learning, high-performance computing, numerical simulations, optimization, operations research, software development. Former core developer for pythonnet. Former Faculty of ML @ NAU. Former Google GDE in Machine Learning.

Updated on October 19, 2020

Comments

  • denfromufa
    denfromufa over 3 years

    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 makefiles, to build my Windows applications.

    I now want to build a DLL and I'm confused.

    Ultimately, I will end up with both a .lib and a .dll. The .lib contains stub code which at run time loads the DLL and uses the code therein.

    I've been looking at the command lines for lib and link and it is not apparent to me exactly what is supposed to be done, to produce this output.

    So I have some questions;

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

    I know link needs /dll as an argument.

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

    What else do I need to know?

  • Admin
    Admin almost 15 years
    in fact, it's best to use a module definitions file, because that permits you to use prototypes without __declspec(dllexport), which in turn permits you to use the original header file of the dll you're hooking
  • Admin
    Admin over 13 years
    Thanks - it's not possible to know the "_D"s that Windows expects, without using MSVC to create a project.
  • nn0p
    nn0p over 8 years
    IMHO, _USRDLL and _WINDLL are not must need, simply use /LD without pass /DLL, /OUT to the linker by /link.
  • Admin
    Admin about 8 years
    kudos go to @nn0p - you've found the actual documented switch to do to-DLL compilation with cl; the original "solution" by Ebow Halm seems like a crude hack in comparison IMO.
  • Miscreant
    Miscreant over 3 years
    Note: /LD is case-sensitive, unlike many other options in Windows programs.