0

I just recently learn about linking an executable to a DLL either through implicit linking or explicit linking; however, it got me confused with project (or DLL) reference.

Why use implicit linking when you can add it as a reference in Visual Studio? Implicit linking requires you to export function by marking it __declspec(dllimport) in the header file in order to use it. On the other hand, if you add a project library as references, you can just use #include "header.h" and use the function like that from the DLL. What is the point of having this feature?

Ice Drake
  • 89
  • 1
  • 13

3 Answers3

1

The important passage in the link you posted about implicit linking is:

[...] where the operating system loads the DLL at the same time as the executable that uses it.

So your dll has to be present to even start up the application, regardless if you use it or not. Assuming you have an extendable application that uses dlls for its addon-system you would need to know beforehand what addons there will ever be and need all the libs for them.

Alternatively you just explicitely check for any addon dlls and import them at runtime.

More examples for explicit linking:

  • Selection of implementation based on current system
  • Selection of implementation based on user input
  • Failsafe mechanisms if the expected dll is not there (No DirectX, Use OpenGL)
  • ...

Also check out this thread about implicit vs explicit linking!

Some clarifications about terminology:

  • '#include', including happens at compiletime by the compiler and does not require fully defined classes and functions. This means if you are missing a library your compiler won't throw errors
  • linking happens after compiling and is done by the linker. Here the linker checks if all of your functions and classes (symbols) are defined.
  • loading a library (implicit or explicit) happens at runtime. Details of this are mentioned above.
Baumflaum
  • 749
  • 7
  • 20
  • What I don't understand is why go through the trouble of using implicit linking when you can use #include directive. In order to use implicit linking, the classes, functions, and data exported by the DLL must all be marked __declspec(dllimport) in the header file. If I added the DLL as references to the executable project, I can just add the #include "header.h" and use the function like that. In both cases, the operating system loads the DLL at the same time as the executable that uses it. When would you need to use implicit linking? – Ice Drake Nov 10 '21 at 01:19
  • @IceDrake 'must all be marked' is plain wrong. this stuff is not needed. after you linked against `.lib` you have all symbols. you can drop that case with `__declspec(dllimport)` haha – Алексей Неудачин Nov 10 '21 at 05:37
  • @IceDrake You are confusing a lot of concepts and terms. By `#include`ing a header file you are just showing the compiler there exists some structures and functions; at this stage it doesn't matter if they are defined or not. By specifying how to use libraries you tell the linker how and what libraries to use. – Baumflaum Nov 10 '21 at 15:57
  • @Baumflaum The start of the confusion begins with the fact that "the classes, functions, and data exported by the DLL must all be marked __declspec(dllimport) in the header file". It led me think that adding DLL as a reference is different from implicit linking. After watching a Youtube video full explaining how to use dynamic library though combined with АлексейНеудачин saying that __declspec(dllimport) is not needed, I come to find that they are the same thing. Mixing managed and unmanaged C++ got me even more confused. Hahaha. – Ice Drake Nov 11 '21 at 06:46
  • Please update your answer with that missing clarification so that I can accept it as the solution and others can benefit from it if they get confused as I did. I will reward you the bounty. – Ice Drake Nov 11 '21 at 06:53
1

Why use implicit linking when you can add it as a reference in Visual Studio?

Visual studio does implicit linking under the hood, when you reference it, as far as my knowledge goes. I think you're a bit confused about these 2 ways. Let me explain this clearly and separately:

Implicit Linking:

Here, you have a set of translation units, which gets compiled into a shared library(.dll on windows). And a .lib file is produced along with that. That .lib file contains the function pointer declarations and the definitions of these functions lie inside the .dll. When you link that .lib file, the application will automatically look over the .dll in its path. If it finds that, it will load that. Once the .dll is loaded, you can use any function within that .dll, as you linked with the .lib. Note that the functions which are marked as "__declspec(dllexport)" will be "exported".

So how Implicit Linking is useful?

  • Multiple applications can load a .dll, if you have same code that is shared between the applications, you can slap that in the .dll.
  • It makes using the .dll easier, but it comes at a cost, you cannot "reload" this unlike Explicit Linking.

Explicit Linking:

In this case, even if you produce the .lib, you don't link with it. Rather you use the Win32API(Or your OS's API) to load the shared library. On windows you use LoadLibrary("Path/To/The/DLLFile.dll"). Which gives you a HMODULE object. Then you retrieve the function pointers manually with the function GetProcAdress(sHMODULE, "Function Name"). Lastly, you call FreeLibrary(sHMODULE). to unload the library. Note that this happens at the runtime. In this case too you have to mark your functions with "__declspec(dllexport)".

So how Explicit Linking is useful?

  • In Game engines which has C++ scripting support(like Unreal) they use explicit linking to hot reload the scripting code.
  • If you want to have a plugin system in your application
Rid
  • 397
  • 2
  • 12
0

You are a bit confused.

  • Implicit Linking: Requires .lib file, where all the exported symbols are specified. This is the usual way to put the lib and the dll file in the search path (a working directory or some custom path that you must specify during the linker option). It will load the dll when the executable calls a function or references any symbol in that dll.

  • Explicit Linking: There are scenarios when you have to load the dll during runtime. In this case, the executable needs to use loadlibrary or a similar API to load the library from the specific path.

In your case, you should go for the first option.

user982042
  • 329
  • 3
  • 7