3

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)

3
  • Are you asking how to use mingw and GCC libraries, or how to mix GCC and VC++ libraries? Commented Feb 11, 2010 at 21:14
  • I'm asking how to use mingw and GCC libraries. btw.. would mixing GCC and VC++ libraries be a problem? Commented Feb 11, 2010 at 22:47
  • You have three questions: how to use static libs with gcc, how to build DLLs, how to build .so. Better to ask three separate questions. Commented Aug 11, 2012 at 22:36

2 Answers 2

3
+50

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 my_prog.c -L<library_dir_here> -lglib-2.0.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That is very clear. So, unlike .so/.dll files static libraries doesn't contain "Export Table"?
@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 msdn.microsoft.com/en-us/library/z4zxe9k8%28VS.80%29.aspx"; for GCC, see 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 :)
Exporting from a DLL : msdn.microsoft.com/en-us/library/z4zxe9k8(VS.80).aspx is this the link you wanted to give?
Yes, that's the link; sorry that was a typo.
1

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.

2 Comments

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?
That is also in my answer. See the itemized/list part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.