makefiles & libraries

This is a discussion on makefiles & libraries within the Linux Programming forums, part of the Platform Specific Boards category; I have a project which builds to several object files ( .o ) but the thing that confuses me is ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    1

    makefiles & libraries

    I have a project which builds to several object files (.o) but the thing that confuses me is how to proceed from here if I want to create a single library file?

    .so and .ar files, what is the difference between those?

    I just want to create a basic library file which can be loaded by other libraries or executables. How about in case of static/dynamic libraries?

    I'm using Kubuntu and gnu c compiler + gmake.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by str800 View Post
    I have a project which builds to several object files (.o) but the thing that confuses me is how to proceed from here if I want to create a single library file?
    To make a .a library file:

    Code:
    ar ruvs libmylibrary.a *.o
    To make a .so shared object file:

    Code:
    ld -shared -o libmylibrary.so *.o
    .so and .ar files, what is the difference between those?
    You mean .a, not .ar. A .so is a shared object file which contains relocation information needed by the dynamic linker. All intra-library dependencies are pre-resolved and the code is packaged into a single object. Usually, when compiling code that will become a .so, you should use the -fPIC option of the gcc compiler to generate "position independent code."

    A .a is just an archive of .o file (like tar, but not quite) which contains an index of symbol names to accelerate linking against the .o files in the archive. There is no need for -fPIC in this case.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And which you should choose depends a bit on what you want to do, and how you're planning for this to be used in a more large scale.

    A shared library is loaded once in the system, only the data-section(s) of the shared library are "separate" if you have two or more applications using the library.

    The application itself only holds a reference to the shared library ("load libmylibrary.so version x.y.z.w" when this app loads).

    On the other hand, if you use a shared library, it needs to be available on the machine the application runs on. So if it's not a LARGE library and/or is expected to be used by many applications, it may be easier and more practical to use a "static" library (libmylibrary.a). Anythhing that is actually used in the library gets included into the final executable file.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC: Compiling with both static and shared libraries
    By eatwithaspork in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 02:48 PM
  2. Replies: 7
    Last Post: 05-13-2008, 04:47 AM
  3. MinGW thread-safe runtime libraries
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2006, 09:15 AM
  4. Libraries and headers
    By darksaidin in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2003, 07:24 AM
  5. QT and external libraries
    By Quacker in forum Linux Programming
    Replies: 1
    Last Post: 04-08-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21