views:

196

answers:

2

A library can be used in an application in two ways:

  1. Statically-linked
  2. Dynamically-linked

But how to do that using both Visual Studio (windows) & GCC?

I know libraries are distributed only in these 4 ways:

  1. Source
  2. header-only libraries
  3. *.lib files for windows. *.a for linux
  4. *.dll (windows) & *.so (linux).

Source distribution is just compiled."header-only libraries" are nothing but a source distribution.

Now if the desired library is distributed in *.lib form. Inorder to use it.

On Visual Stuido :

  1. We add directory path containing headers(*.h) to Configuration Properties > General > Additional Include Directories
  2. we add each *.lib file to Configuration Properties > Linker > Input > Additional Dependencies
  3. we add directory path of *.lib files to: Configuration Properties > Linker > Additional Library Directories

How to do the same thing for GCC/MingW? I don't know how to build my application when the library is distributed as *.dll or *.so too. Can someone tell me what do I need to do in these situations for both Visual studio (windows) and GCC(linux)/mingw(windows)

A: 

For DLL distributions the scenario is similar to that of .lib files. (your #3)

You will have to configure your project to build a DLL. The project will build LIB and DLL files.

Depending on your needs/architecture/design you can either

  • Link against the LIB file just as you do in your #3 above. Note that the DLL file will have to exist on the target machine at run-time otherwise the application will not load.

  • Call "LoadLibrary()" from the client app and skip the linking part/no need to have the LIB used in the client application.

I can't help you with the gcc specific questions.

Tim
I'm sorry but I didn't get what you are trying to answer. My question was not "How to produce *.dll / *.lib library distribution?". It is `how to use a library distributed as *.DLL?`
Pecker
That is also in my answer. See the itemized/list part.
Tim
A: 

On GCC, for static linking, you'll include the library in the command line. Lets say you've glib-2.0.lib and your program that uses GLib library is my_prog.c, then you invoke GCC as gcc -lglib-2.0 my_prog.c.

As for the dll and so, dynamic libraries are something you don't link to your programs by passing them to your linker. Instead the operating system gives you a function to load them when it's required, at run time. Thats the reason it's called dynamic. In Windows you've LoadLibrary and on Linux you've dlopen. Both these functions get a string (which is the dll or so's name) and load it if it's avaiable on the machine. Once it's loaded, the function you require from the library is looked-up by passing its name to GetProcAddress on Windows and dlsym on Linux; both returns a function pointer, with which you can call that function. Since you're not directly calling the functions provided by the libraries directly, but thru' function pointers, there'll be no need for you to link them statically (i.e. pass them to the linker) when you build your app.

legends2k
Thank you. That is very clear. So, unlike *.so/*.dll files static libraries doesn't contain "Export Table"?
Pecker
@Pecker: Yes, export symbols are something to do with dynamic libraries; in static libraries it's not explicitly symbols are not exported specifically, while in dynamic shared objects it's manipulated when created. For VC++, see http://msdn.microsoft.com/en-us/library/z4zxe9k8%28VS.80%29.aspx"; for GCC, see http://gcc.gnu.org/wiki/Visibility.If you like an answer in Stack overflow, you can accept it by clicking on the tick mark next to it :)
legends2k
the msdn link you gave doesn't seem to work
Pecker
Exporting from a DLL : http://msdn.microsoft.com/en-us/library/z4zxe9k8(VS.80).aspx is this the link you wanted to give?
Pecker
Yes, that's the link; sorry that was a typo.
legends2k