Linking my first DLL
-
I am trying to build and use my first DLL. After editing in the source code and building the Debug directory contains the .dll, the .lib and four additional files. A console app is created in the same solution but another project and built without referencing the dll. The .dll and .lib are copied to the solution directory. In the console app I added:
Quote:
#include "E:\CODE\Common_Utilities\Common_Utilities\Common_Utilities.h"
Ctl-F7 produces a successful compile. A build produces this error.
Quote:
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Get_Text_For_Error_Code(unsigned long,wchar_t *,unsigned int)" (__imp_?Get_Text_For_Error_Code@@YAXKPA_WI@Z) referenced in function _wmain E:\CODE\Common_Utilities\Test_Get_Text_For_Error_Code\Test_Get_Text_For_Error_Code.obj Test_Get_Text_For_Error_Code
After some searches my latest attempt to fix is by: Right click solution -> Properties -> Linker -> General -> Additional Library Directories There I edited in the path to the DLL. No help. This is my first DLL attempt and I strongly suspect there is something simple I have missed. Please point me in the right direction.
Thank you for your time
-
I am trying to build and use my first DLL. After editing in the source code and building the Debug directory contains the .dll, the .lib and four additional files. A console app is created in the same solution but another project and built without referencing the dll. The .dll and .lib are copied to the solution directory. In the console app I added:
Quote:
#include "E:\CODE\Common_Utilities\Common_Utilities\Common_Utilities.h"
Ctl-F7 produces a successful compile. A build produces this error.
Quote:
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Get_Text_For_Error_Code(unsigned long,wchar_t *,unsigned int)" (__imp_?Get_Text_For_Error_Code@@YAXKPA_WI@Z) referenced in function _wmain E:\CODE\Common_Utilities\Test_Get_Text_For_Error_Code\Test_Get_Text_For_Error_Code.obj Test_Get_Text_For_Error_Code
After some searches my latest attempt to fix is by: Right click solution -> Properties -> Linker -> General -> Additional Library Directories There I edited in the path to the DLL. No help. This is my first DLL attempt and I strongly suspect there is something simple I have missed. Please point me in the right direction.
Thank you for your time
At link time you need to include the .lib file, which contains the exports from the build of the library. The .dll file is only used at run time whan the system loader gets all the separate modules into the program's address space. Did you follow the guide at Walkthrough: Creating and Using a Dynamic Link Library (C++)[^]?
-
At link time you need to include the .lib file, which contains the exports from the build of the library. The .dll file is only used at run time whan the system loader gets all the separate modules into the program's address space. Did you follow the guide at Walkthrough: Creating and Using a Dynamic Link Library (C++)[^]?
I had not found that one, but went through it. I read the instructions, adapted the steps to my solution as I understand them, and the project in the same solution that used the DLL worked perfectly. That is cool. However, I noted something. From that walkthrough web page is this:
Quote:
To use in the app the math routines that you created in the DLL, you must reference it. To do this, select the MyExecRefsDll project in Solution Explorer, and then on the menu bar, choose Project, References. In the Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Adding references in Visual C++ projects.
When I arrived at the dialog to add a reference the only allowed option was the DLL in the same solution. I created another solution in another directory to test using the DLL from another directory and solution. I navigated to same place within the IDE and the new solution with:
Quote:
right click solution -> References -> Add New Reference
The response is "No items found." The dialog offers no method of discovering a DLL somewhere else on the computer. There is still something simple I am missing. How does one specify an arbitrary DLL to use as a procedure or class resource? Edit: From the new solution I tried this: in the dot cpp file, console app, I added this line:
#include "E:\CODE\Common_Utilities\Common_Utilities\Common_Utilities.h"
It is a reference to the h file from the solution where the DLL was created. In that new console app I called a function from the DLL. Then the following steps were taken:
Quote:
Right click on solution followed by: Properties -> configuration Properties -> Linker -> General -> field Additional library directories.
In that field I edited in the location of the dll from that dll solution. The compile succeeds but the link continues to fail with error LNK 2019 unresolved external symbol and __delspec(dllimport)
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
-
I had not found that one, but went through it. I read the instructions, adapted the steps to my solution as I understand them, and the project in the same solution that used the DLL worked perfectly. That is cool. However, I noted something. From that walkthrough web page is this:
Quote:
To use in the app the math routines that you created in the DLL, you must reference it. To do this, select the MyExecRefsDll project in Solution Explorer, and then on the menu bar, choose Project, References. In the Property Pages dialog box, expand the Common Properties node, select Framework and References, and then choose the Add New Reference button. For more information about the References dialog box, see Adding references in Visual C++ projects.
When I arrived at the dialog to add a reference the only allowed option was the DLL in the same solution. I created another solution in another directory to test using the DLL from another directory and solution. I navigated to same place within the IDE and the new solution with:
Quote:
right click solution -> References -> Add New Reference
The response is "No items found." The dialog offers no method of discovering a DLL somewhere else on the computer. There is still something simple I am missing. How does one specify an arbitrary DLL to use as a procedure or class resource? Edit: From the new solution I tried this: in the dot cpp file, console app, I added this line:
#include "E:\CODE\Common_Utilities\Common_Utilities\Common_Utilities.h"
It is a reference to the h file from the solution where the DLL was created. In that new console app I called a function from the DLL. Then the following steps were taken:
Quote:
Right click on solution followed by: Properties -> configuration Properties -> Linker -> General -> field Additional library directories.
In that field I edited in the location of the dll from that dll solution. The compile succeeds but the link continues to fail with error LNK 2019 unresolved external symbol and __delspec(dllimport)
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
You are still (not surprisingly) missing an important part of this. When you create a DLL the build (more specifically the linker) creates two files. The first file is the DLL itself which contains the actual code that you want to call when your program is run. This file is only ever needed at run time, it has no part in the linking process when creating your executable. The second, equally important, is the .LIB file, which contains the names and offsets of the exported functions. This second file is the one that you must add to your project in order for the linker to satisfy all the
__delspec(dllimport)
declarations. So when you add to theAdditional library directories
the path to your library, you also need to add the name of the .LIB file to theAdditional Dependencies
part of theInput
section in the Linker properties. Of course, you could save yourself much of this pain by creating a static library rather than a dll, but that is probably an exercise for another day. -
You are still (not surprisingly) missing an important part of this. When you create a DLL the build (more specifically the linker) creates two files. The first file is the DLL itself which contains the actual code that you want to call when your program is run. This file is only ever needed at run time, it has no part in the linking process when creating your executable. The second, equally important, is the .LIB file, which contains the names and offsets of the exported functions. This second file is the one that you must add to your project in order for the linker to satisfy all the
__delspec(dllimport)
declarations. So when you add to theAdditional library directories
the path to your library, you also need to add the name of the .LIB file to theAdditional Dependencies
part of theInput
section in the Linker properties. Of course, you could save yourself much of this pain by creating a static library rather than a dll, but that is probably an exercise for another day.My next step then is: Right click the project level containing the code that uses the DLL and select: Properties -> Configuration Properties -> Linker -> Input There is found the field Additional Dependencies. Currently it contains:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
edit in:
Common_Utilities.lib
and click OK. The project built successfully. Great! I try to run it and get a dialog stating:
Quote:
The program can't start because Common_Utilities is missing from your computer. Try reinstalling the program to fix this problem.
Ok, going back to: Solution -> Properties -> Configuration Properties -> General and field: Additional LIbrary directories I find this text:
Quote:
E:\CODE\Common_Utilities\Debug;%(AdditionalLibraryDirectories)
That is indeed the path of the directory that contains the DLL. I don't understand why it says the DLL is missing. Good grief, I really wish I knew someone that knows this stuff and could sit here and say: Right there, change that.
Thank you for your time
-
My next step then is: Right click the project level containing the code that uses the DLL and select: Properties -> Configuration Properties -> Linker -> Input There is found the field Additional Dependencies. Currently it contains:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
edit in:
Common_Utilities.lib
and click OK. The project built successfully. Great! I try to run it and get a dialog stating:
Quote:
The program can't start because Common_Utilities is missing from your computer. Try reinstalling the program to fix this problem.
Ok, going back to: Solution -> Properties -> Configuration Properties -> General and field: Additional LIbrary directories I find this text:
Quote:
E:\CODE\Common_Utilities\Debug;%(AdditionalLibraryDirectories)
That is indeed the path of the directory that contains the DLL. I don't understand why it says the DLL is missing. Good grief, I really wish I knew someone that knows this stuff and could sit here and say: Right there, change that.
Thank you for your time
Make sure the Common_Utilities DLL is in the same folder as the executable so the system can find it. The Additional Libraries setting tells it where to find the LIB files.
The difficult we do right away... ...the impossible takes slightly longer.
-
My next step then is: Right click the project level containing the code that uses the DLL and select: Properties -> Configuration Properties -> Linker -> Input There is found the field Additional Dependencies. Currently it contains:
kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
edit in:
Common_Utilities.lib
and click OK. The project built successfully. Great! I try to run it and get a dialog stating:
Quote:
The program can't start because Common_Utilities is missing from your computer. Try reinstalling the program to fix this problem.
Ok, going back to: Solution -> Properties -> Configuration Properties -> General and field: Additional LIbrary directories I find this text:
Quote:
E:\CODE\Common_Utilities\Debug;%(AdditionalLibraryDirectories)
That is indeed the path of the directory that contains the DLL. I don't understand why it says the DLL is missing. Good grief, I really wish I knew someone that knows this stuff and could sit here and say: Right there, change that.
Thank you for your time
As I said, the AdditionalLibraryDirectories path is where it looks for the .LIB file at link time. At run time nothing in your project settings have any meaning to the Windows operating system. When the program loader starts your executable it looks for all the .DLL files that are required. It first looks in the directory where the executable is loaded from, and then in all the directories listed in your system's
PATH
environment variable. So you either copy your dll into the directory where the .exe file resides, or add its location to thePATH
variable.