Thread: Clarification regarding linking libraries using gcc

  1. #1
    Registered User
    Join Date
    Feb 2020
    Posts
    5

    Clarification regarding linking libraries using gcc

    I am using Linux Mint 19.03

    The inclued files (.h) are in /usr/include

    geeksforgeeks.org says -

    -lm :
    This command link math.h library to our source file, -l option is used for linking particular library, for math.h we use -lm.

    Code:
    gcc -Wall source.c -o opt -lm

    My first question is, how do I know -lm represents math.h?

    What if I want to link malloc.h
    or locale.h? What am I supposed to put after -l if I want to link malloc.h or locale.h? How do I know what am I supposed to put after `-l` if I want to link malloc.h or locale.h?

    My second question is, Why dont I have to link stdio.h to build:

    Code:
    
    #include <stdio.h>
    
    int main()
    {
      printf("Hello world\n");
      return 0;
    }


    My third question is, where is the inline code BBCode?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,744
    The standard library header files such as stdio.h, string.h, stdlib.h etc are all resolved by libc, and the compiler automatically uses that.

    Historically, the math library was separate because all floating point was done in software. If you didn't use FP, then there was no need to pay a price.

    > How do I know what am I supposed to put after `-l`
    The manual pages tell you.

    > My third question is, where is the inline code BBCode?
    It's the # format icon on the advanced editor, or you just put [code][/code] tags around the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    864
    The entire standard library (libc) is available by default. I believe the library option is -lc, but you don't need to manually add that.

    However, for historical reasons, the math library (libm) is separate (at least on *nix systems), and therefore you need to append -lm if you use any of its functions.

    Note that header files are not libraries. They just contain the information that a compilation unit needs in order to be compiled into an object file. Then the linker links the object file(s) with the standard library and any other libraries that you request.

    There is no inline code, but you can use the Courier New Font for a fixed-width font.
    Last edited by john.c; 02-09-2020 at 10:30 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by blueray
    My first question is, how do I know -lm represents math.h?
    It doesn't. Take a look at the GCC documentation for Options for Linking. You will see this section:
    -llibrary
    -l library

    Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

    The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

    The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

    Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.

    It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
    So, -lm would link to the library named m (or in context, libm), which should be contained in a library file named libm.a or libm.so. In this case the library file is provided by gcc itself as it is part of the standard library, but when dealing with third party library files, you may need to specify the directory containing libfoo.a with the -L option, then link the foo library with -lfoo.

    Every library file would have one or more header files associated with it, such as math.h, but you don't link to the header files. Rather, you include them, so refer to Options for Directory Search for third party library header files.

    Quote Originally Posted by blueray
    My second question is, Why dont I have to link stdio.h to build:
    The relevant library is linked to by default. It is possible to turn this off, as you can see in "Options for Linking". I think there is some historical reason why the library associated with math.h didn't receive similiar treatment (though it can), but I am afraid that I am not certain as to the facts.
    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
    Join Date
    Feb 2020
    Posts
    5
    So many good answers in such a short time. I have just become a fan. How do I mark the question as solved?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    864
    There is no "solved" marker.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  7. #7
    Registered User
    Join Date
    Feb 2020
    Posts
    5
    my heartfelt gratitude to all of you. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking libraries
    By trollwarrrior1 in forum C Programming
    Replies: 3
    Last Post: 11-29-2014, 01:00 AM
  2. linking to libraries
    By davbeck in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 04:56 PM
  3. Help with linking to other c++ libraries via .net
    By pipercubusa in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2005, 01:08 PM
  4. Linking and Libraries
    By krygen in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2004, 03:01 PM
  5. Dynamically linking libraries
    By neandrake in forum Windows Programming
    Replies: 5
    Last Post: 06-18-2003, 02:54 PM

Tags for this Thread