LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-29-2011, 08:32 AM   #1
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Rep: Reputation: 0
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")"
 
Old 05-29-2011, 11:17 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 View Post
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.

Last edited by johnsfine; 05-29-2011 at 11:27 AM.
 
Old 05-29-2011, 11:54 AM   #3
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by PinoyAko View Post
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 View Post
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 View Post
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.
 
Old 05-29-2011, 04:20 PM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,623

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
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
 
Old 05-30-2011, 05:27 AM   #5
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by johnsfine View Post
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 View Post
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.

Last edited by PinoyAko; 05-30-2011 at 05:31 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What's the difference between static and dynamically linked library... trist007 Linux - Newbie 5 09-19-2010 08:40 PM
[SOLVED] g++ dynamically linked library not being found posop Programming 6 04-14-2010 03:20 PM
dynamically list all symbols from a shared library famsinyi Programming 2 09-09-2009 06:21 PM
How do you load dynamically a library from C? Hano Programming 4 06-07-2002 03:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration