Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Linking my first DLL

Linking my first DLL

Scheduled Pinned Locked Moved ATL / WTL / STL
helpdebuggingquestionlounge
7 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • B bkelly13

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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++)[^]?

      B 1 Reply Last reply
      0
      • L Lost User

        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++)[^]?

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • B bkelly13

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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 the Additional library directories the path to your library, you also need to add the name of the .LIB file to the Additional Dependencies part of the Input 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.

          B 1 Reply Last reply
          0
          • L Lost User

            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 the Additional library directories the path to your library, you also need to add the name of the .LIB file to the Additional Dependencies part of the Input 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.

            B Offline
            B Offline
            bkelly13
            wrote on last edited by
            #5

            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

            Richard Andrew x64R L 2 Replies Last reply
            0
            • B bkelly13

              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

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • B bkelly13

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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 the PATH variable.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups