Dynamic vs static linking
Hello, I have some code I wrote, that I want to wrap into a library since many of my applications uses it.
I heard and read about 2 kind of libraries:
Dynamic and Static. In static the code from the library is attached (copied) to the exe, that makes a problem like if my codes logic (I'm not talking about API, API will probably always will stay the same) going to change sometime and Ill want my programs to use the never logic of that lib Ill need to recompile each application with the new .lib file, opposed to dynamic where I would need to only recompile the DLL.
On the other side, exporting classes (I'm talking only about C++ here) is easier with Static library than Dynamic (as far as I know).
One more thing I want to notice is some libraries (like SDL, and I think OpenGL also) are shipped with both .lib and .dll files, while the lib being attached to the project but the code wont be copied (as far as I know) and the program will still depend on the .dll file, if someone can explain a bit more about this method, or at least tell me the name of this method so I could google it, it would be nice.
Anyway the question I would like to ask is what is preferred for my case (where I want to wrap my code, so will be easy to add to future project) Static or Dynamic library? Also what Hobby/Professionals/Industries use?
Thanks a lot.
The method you're referring to is just a standard way of using DLLs. You compile against a small header/lib that just defines the functions exported by the DLL. Alternatively you could use LoadLibrary and GetProcAddress if you want to avoid that.
You mean like all my classes/functions are in the .dll file, than the .lib file imports them from the .dll and exports to my program, and program just uses it?
Is there any reference where I can read how this is done by programming language?
The main question is still active.
Thanks a lot.
Is there any reference where I can read how this is done by programming language?
The main question is still active.
Thanks a lot.
The .lib that is typically shipped with a DLL for you to link against is called the 'stub' or 'import' library. It is basically little more than a giant jump table that lets you use the functions exposed from the DLL without having to jump through the LoadLibrary() and GetProcAddress() hoops.
Using LoadLibrary() and GetProcAddress().
Depends. A dynamic link has some benefits you touched on, but also has additional cost associating with maintaining it (making sure you export what is appropriate, et cetera). A static library is easier to use but you use the flexibility of swapping out that DLL later.
Supporting such flexibility isn't necessarily trivial, though. Ultimately it boils down to what you want to get out of this aspect of your project: if you want to bother with swapping out the DLL, then clearly the DLL is your choice. If you don't care, then I'd go for the simpler option of static linking (as long as we're talking about your own libraries) until you really need do otherwise. You'll get more done.
Depends. Shouldn't have any impact on your decision.
Quote:
Is there any reference where I can read how this is done by programming language?
Using LoadLibrary() and GetProcAddress().
Quote:
yway the question I would like to ask is what is preferred for my case (where I want to wrap my code, so will be easy to add to future project) Static or Dynamic library?
Depends. A dynamic link has some benefits you touched on, but also has additional cost associating with maintaining it (making sure you export what is appropriate, et cetera). A static library is easier to use but you use the flexibility of swapping out that DLL later.
Supporting such flexibility isn't necessarily trivial, though. Ultimately it boils down to what you want to get out of this aspect of your project: if you want to bother with swapping out the DLL, then clearly the DLL is your choice. If you don't care, then I'd go for the simpler option of static linking (as long as we're talking about your own libraries) until you really need do otherwise. You'll get more done.
Quote:
Also what Hobby/Professionals/Industries use?
Depends. Shouldn't have any impact on your decision.
Thanks for your comment jpetrie
Sorry meant something else, I meant how I can accomplish this method, I mean to use dynamic dll I just create DLL Project in VS, to create static library I create a static library project in VS. But as I understood to create this lib+dll thing I must create 2 project, 1 for dynamic dll and the second on as static library that imports the API of the DLL and then exports it? I just tough maybe there is a template in Visual Studio that can do this.
I may disagree, for me it do have some impact, I don't want to use something not standard or something strange.
Quote:Using LoadLibrary() and GetProcAddress().
Sorry meant something else, I meant how I can accomplish this method, I mean to use dynamic dll I just create DLL Project in VS, to create static library I create a static library project in VS. But as I understood to create this lib+dll thing I must create 2 project, 1 for dynamic dll and the second on as static library that imports the API of the DLL and then exports it? I just tough maybe there is a template in Visual Studio that can do this.
Quote:Depends. Shouldn't have any impact on your decision.
I may disagree, for me it do have some impact, I don't want to use something not standard or something strange.
You don't need two projects. The dll project should by default create the lib you need to link against as well.
Quote:
I may disagree, for me it do have some impact, I don't want to use something not standard or something strange
There is no standard. Everybody makes decisions about their project build system to accommodate their needs, so worry about your needs and not how "the professionals" do it.
Put another way, "the professionals" do what makes sense for their project, so if you want to emulate them, do the same rather than mimic the specific process some other (or arbitrary set of other) projects used.
Quote:
You don't need two projects. The dll project should by default create the lib you need to link against as well.
And if your project template doesn't or you've disabled it, there's an option for naming the the import library that will cause it to be generated, somewhere in the project settings.
Oh thanks! Ill try that a bit later.
Well I understood that there is no rule-of-thumb when to use static and when to use dynamic libraries. Ill try to sit and look what my code fits more for, and than decide.
Anyway additional opinion would be appreciated.
Well I understood that there is no rule-of-thumb when to use static and when to use dynamic libraries. Ill try to sit and look what my code fits more for, and than decide.
Anyway additional opinion would be appreciated.
I found this article
http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx
It shows 3 ways to export C++ class from dynamic DLL.
I just wanted to clarify:
C++ Naive Approach Is the one widely used in libraries like SDL (for me its also the one that looks better and functional than others, except the export tree that can be pretty annoying)
Thanks again.
http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx
It shows 3 ways to export C++ class from dynamic DLL.
I just wanted to clarify:
C++ Naive Approach Is the one widely used in libraries like SDL (for me its also the one that looks better and functional than others, except the export tree that can be pretty annoying)
Thanks again.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement