Thread: [C] General questions about using libraries

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    11

    Question [C] General questions about using libraries

    Hello everyone,
    I am still not very clear how to use external libraries in my programs.
    Correct me if I'm wrong:
    The libraries can be downloaded from the classics or apt-get and they have a name like *- dev or installed directly from source code by "configure/make/make install"
    Once installed, the libraries are usually copied in / usr / local / lib and have the extension *. so
    When I write my program, I add "#include <headerlibrary.h>" on the top of my code.
    When I compile the program with gcc I type "gcc myprog.c -llibraryname"

    So far correct?
    But the library's header (*. h) should automatically go into /usr/include?
    Where the compiler searches for them?

    When I distribute my program I have to make sure that you also install the library used, right?

    Thanks for yours answers.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what gcc has to say about where to find included header files: Include Syntax - The C Preprocessor.
    The rules are similar for where to find libraries you're linking with. Basically, you can put headers or libraries wherever, so long as you tell gcc where to find them. But /usr/include, or a subdirectory thereof, would be conventional for headers.

    EDIT: The end user does not need the library headers. Those are for programming only.

    Here's some info on static vs. shared libraries: shared vs static libraries - Google Search.

    In short, with static libraries, you have to deploy new binaries if you update the library version you use (major pain), but you don't have to worry about whether the end user has the necessary libraries on their system. Also, you have larger executables. With shared libraries, executables are smaller (the libraries are NOT part of the executable), and you can update your program independent of the libraries, but you must rely on the end user having a copy of the libraries your program requires.

    EDIT: Added magenta text
    Last edited by anduril462; 04-27-2011 at 10:35 AM. Reason: Added critical missing word: NOT

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anduril462 View Post
    EDIT: The end user does not need the library headers. Those are for programming only.
    Unless the end user is building from source. Then they need everything you used.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General C questions
    By bluej322 in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 03:00 AM
  2. A Few General C Questions
    By Jedijacob in forum C Programming
    Replies: 13
    Last Post: 02-17-2005, 02:47 AM
  3. General Libraries question
    By subnet_rx in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2004, 01:17 AM
  4. General Dev Questions
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2002, 08:06 PM
  5. two general questions
    By Draco in forum C Programming
    Replies: 8
    Last Post: 05-17-2002, 10:46 PM