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