tags:

views:

828

answers:

4

Hi.

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?

+1  A: 

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.

Regards,

Sebastiaan

Sebastiaan Megens
Yes. However, this will involve installing the MSVC GUI - which will take a not inconsiderable period of time and almost certainly devestate my current system build configuration; and all that simply to create a single project so I can examine the project files.
Blank Xavier
True, but you can't get more accurate than that.If you use a VM you won't devestate anything, but yes, that takes even more time. :)Regards,Sebastiaan
Sebastiaan Megens
A: 

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

Blank Xavier
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
Blank Xavier
A: 

cl.exe /D_USRDLL /D_WINDLL <files-to-compile> <files-to-link> /link /DLL /OUT:<desired-dll-name>.dll

Ebow Halm
A: 

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.

John Knoeller