Here's my library Lib.c
file:
#include <stdio.h>
int helloworld(){
printf("Hello World DLL");
}
Here's my exe Main.c
file:
int helloworld();
int main(int argc, char** argv){
helloworld();
}
I would like to create Lib.dll
, and Main.exe
, where Lib.dll
comes from Lib.c
and Main.exe
links against Lib.dll
.
What are the specific steps to achieve this?
See this related question on how to build the DLL.
Your library code as it stands does not export any symbols and your executable does not import the symbols from your library. Two typical patterns for doing that are shown below but you might want to read up on that first.
The first method uses
__declspec()
to declare in the code what functions (or other items) are exported from your DLL and imported by other executables. You use a header file to declare the exported items and have a preprocessor flag used to control whether the symbols are exports or imports:mylib.h:
I have also specifically set the calling convention to
__stdcall
as are most DLL functions (I could have usedWINAPI
instead of__stdcall
if I had included windows.h) and have declared the functions asextern "C"
so their names do not get mangled when compiled as C++. Not such a problem here as it's all C, but if you were to build the DLL from C source and then try to use it from a C++ executable then the imported names would be incorrect.The code could then look like this:
mylib.c
You'd build your DLL using the following command line. As well as creating the DLL it will create the import library (.lib) required to use your DLL from another executable (as well as the export file, but that is only required in certain circumstances):
The
/DBUILDING_MYLIB
argument defines the preprocessor symbol used to control whether the functions in the DLL are exports (if it is defined) or imports (not defined). So you'd define it when building the DLL but not when building your application.The
/LD
parameter tells cl to produce a DLL.The second method is to use module definition files as mentioned in the comments. You can use the code you already have but you also need to create the module definition file. At it's simplest it looks like this:
In this case to build the DLL you require the following command line:
You could then code your application so that it used your library header with the imported version of your DLL function:
main.c
Which you could then compile with the following command line (assuming the import library from the DLL creation is in the same directory as your main.c). This step is the same whether you used declspec or module definition files:
Arguments passed after the
/link
argument are passed onto the linker command line as they appear, so as just a filename it is used as extra input to link into the executable. In this case we specify the import library generated when we built the DLL.The command lines I've shown here are pretty much the absolute minimum you'd need but it'll allow you to create a DLL and link an application to it.
I have assumed the calling convention is correct in all of the above and I have not experimented much to see whether I got it wrong at any point.