简体   繁体   中英

How can I build a C/C++ program using `static linking` & `Dynamic linking` with gcc & Visual studio?

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)

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 (ie pass them to the linker) when you build your app.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM