Thread: Simple explainer on libraries

  1. #1
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81

    Simple explainer on libraries

    I was wondering if I could get a quick rundown on working with libraries in C.

    I know most systems include standard C libraries, such as stdio, stdbool or stdlib.

    But what about working with libraries that aren't included in the system?

    For example, I want to use a third-party library that parses CSV files in C. I know there is the libcsv library, but I'm not sure how I can install it and start using it. For context, I work primarily on macOS and Linux.

    Additionally, how do I include these libraries when compilling my code? Do I need to include a flag in clang or gcc, or does doing
    Code:
    #include <library.h>
    do the trick?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Often, libraries come with a README or INSTALL file (or both) that you can read to understand how to use it, or this could be in some external (possibly online) documentation. The details can vary by quite a bit, ranging from header-only libraries that you can just #include to libraries consisting of a single header and single source file that you can easily compile as just another file in your project to libraries that require a .configure && make && make install dance to libraries using some other build system... and then sometimes libraries could be installed using your OS distribution's package management system.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Thanks, laserlight, for the response. A couple of follow up questions:

    Quote Originally Posted by laserlight View Post
    The details can vary by quite a bit, ranging from header-only libraries that you can just #include to libraries consisting of a single header and single source file that you can easily compile as just another file in your project to libraries that require a .configure && make && make install dance to libraries using some other build system...
    So, depending on the installation instructions, do the library files go to something like /usr/lib in my system, or nestled in the project folder?

    and then sometimes libraries could be installed using your OS distribution's package management system.
    That was the case for my libcsv example. I installed it on a Mac using homebrew. I verified the library installed correctly by running man libcsv. The documentation appeared, so I assumed the library files were where they needed to be.

    Is that always the case? Is running the man page a good way to see if a library is usable? Is there a place where I can see which libraries I have installed?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by catacombs
    So, depending on the installation instructions, do the library files go to something like /usr/lib in my system, or nestled in the project folder?
    If they are "installed", that generally means they will be moved to somewhere on your system such that they can be used by multiple projects, or even other users.

    Quote Originally Posted by catacombs
    Is that always the case? Is running the man page a good way to see if a library is usable? Is there a place where I can see which libraries I have installed?
    For libraries installed by a package manager, you would check the package manager. Otherwise, it would typically be obvious to you during installation whether it succeeded or failed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Thanks, laserlight. I think I got the gist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple makefile question, shared libraries
    By dayalsoap in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2011, 08:42 PM
  2. how do I use libraries ?
    By Justin_H in forum C++ Programming
    Replies: 8
    Last Post: 07-23-2005, 08:37 PM
  3. libraries
    By Vertex34 in forum C Programming
    Replies: 7
    Last Post: 07-19-2004, 12:28 AM
  4. bad libraries or what?
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-20-2002, 07:15 PM
  5. C++ libraries
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2002, 11:52 AM

Tags for this Thread