LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to dynamically link a library? (https://www.linuxquestions.org/questions/programming-9/how-to-dynamically-link-a-library-883337/)

PinoyAko 05-29-2011 08:32 AM

How to dynamically link a library?
 
I am creating a dll that needs to use zlib

I am using VC++ 6.0 to create my DLL the dll needs to compress some data that's why I want to use zlib.

I already compiled zlib1.dll and zlib.lib

My understanding is if you are using zlib.lib you are statically linking therefore the functions/codes in zlib will be compiled within my project.

If I use this code

Code:

#include <zlib.h>
#pragma comment(lib, "zlib1.lib")

Does that mean I am statically linking?

Can someone please explain/correct me if my understanding to this matter is not right :)

Is there any other way to not use "pragma comment(lib, "zlib1.lib")"

johnsfine 05-29-2011 11:17 AM

It is obvious you are asking a Windows specific question. There is nothing wrong with that in this sub forum within LQ. But it might have helped to explicitly state that you want Windows specific help (to avoid confusing anyone who might otherwise try to give a Linux answer).

Quote:

Originally Posted by PinoyAko (Post 4370120)
I already compiled zlib1.dll and zlib.lib

You may need to be more precise in that statement, and why zlib1 vs. zlib? Or was that just a typo in your post?

As part of creating zlib1.dll you normally create an interface .lib file (is that zlib.lib?). Then you can use the interface .lib file at link time to indicate use of the .dll at load time.

But, you might alternately or also have created a .lib file independently from the same source code as the .dll (is that what zlib.lib is?) such that the .lib used at link time is statically linking those functions instead of specifying use of the .dll at load time.

Quote:

My understanding is if you are using zlib.lib you are statically linking therefore the functions/codes in zlib will be compiled within my project.
In Windows, you use a .lib either way (for static or dynamic link of the specified functions). The difference occurs when you build that .lib file (do you build it as an interface .lib while building the .dll or do you build it as a static .lib independently from the same source code as the .dll).

In Linux (and other POSIX and POSIX-like systems), you include a .lib file at link time to statically link the contents, or you include a .so file at link time to dynamically link the contents. If you have used or read about that sensible system, then the Microsoft kludge of using two different kinds of .lib may be confusing you.

Quote:

Is there any other way to not use "pragma comment(lib, "zlib1.lib")"
You could specify the .lib name as part of the link command, rather than in the source code. In Visual Studio there is some GUI step where you specify .lib files to be included in the link command the GUI auto generates for you (but I forget exactly where that is in the GUI). You can also directly override the link command to add anything you want, but that is normally not done for including an ordinary .lib.

But why don't you like that pragma? That seems a more robust way to manage the need for a library vs. specifying the function declarations (the .h file) far away from specifying the linkage to the lib.

SigTerm 05-29-2011 11:54 AM

Quote:

Originally Posted by PinoyAko (Post 4370120)
I am using VC++ 6.0 to create my DLL the dll needs to compress some data that's why I want to use zlib.

I'd recommend to upgrade your compiler. Microsoft Visual Studio express is available for free, plus there are other compilers available for windows platform.

Quote:

Originally Posted by PinoyAko (Post 4370120)
Is there any other way to not use "pragma comment(lib, "zlib1.lib")"

Yes. Specify additioanl library within project settings. Since your IDE/compiler is 10 years old, I do not remember where exactly those settings are located.

Quote:

Originally Posted by johnsfine (Post 4370228)
You may need to be more precise in that statement, and why zlib1 vs. zlib? Or was that just a typo in your post?

See zlib dll faq.

John VV 05-29-2011 04:20 PM

Quote:

I am using VC++ 6.0 to create my DLL the dll needs
i would upgrade that very old program
i was using vc7 in `02

the current vs express ( cmd) is free for win7

PinoyAko 05-30-2011 05:27 AM

Quote:

Originally Posted by johnsfine (Post 4370228)
In Windows, you use a .lib either way (for static or dynamic link of the specified functions). The difference occurs when you build that .lib file (do you build it as an interface .lib while building the .dll or do you build it as a static .lib independently from the same source code as the .dll).

This is the answer I was looking for :) Thanks


Quote:

Originally Posted by SigTerm (Post 4370249)
I'd recommend to upgrade your compiler. Microsoft Visual Studio express is available for free, plus there are other compilers available for windows platform.


Yes. Specify additioanl library within project settings. Since your IDE/compiler is 10 years old, I do not remember where exactly those settings are located.


See zlib dll faq.

I already have Microsoft Visual Studio express but that requires .net to run and that's what I am avoiding.


All times are GMT -5. The time now is 09:25 PM.