Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Questions about C

  1. Mar 21, 2012 #1
    Just a few questions about C.

    When using #include <math.h>, why is it that when compiling in gcc, I need to add the option -lm. I understand that -l links to some libraries, but why does the compiler need to be linked to whatever 'm' links to? Why can't the compiler find math.h by itself? And what is linking in terms of compilers?

    And can I make my own C libraries with my own code in it, like how custom modules can be used in python? And how would I use these custom libraries in other code? Would I just write #include <lib.h> or something? And how do I tell the compiler where to look for these custom libraries?
     
  2. jcsd
  3. Mar 21, 2012 #2

    jhae2.718

    User Avatar
    Gold Member

    I don't remember why, but math.h is the only header in the C standard library that needs to be explicitly linked when compiling.

    You can write your own libraries; include them with:
    Code (Text):

    #include "../path/to/my_library.h"
     
     
  4. Mar 21, 2012 #3

    rcgldr

    User Avatar
    Homework Helper

    microsoft visual c/c++ uses this syntax to include libraries, although I've only seen it used for the multi-media library.

    #pragma comment(lib, "winmm.lib") /* include winmm.lib */

    The assembler includes a directive, INCLUDELIB, but that pragma doesn't result in generating an INCLUDELIB if I compile to assembler, so I'm not sure what that pragma actaully does, other than it works (my guess is that it causes the c compiler to pass a parameter to the linker).

    old microsoft link about a change to that pragma:

    http://support.microsoft.com/kb/153901

    current msdn link:

    http://msdn.microsoft.com/en-us/library/7f0aews7(v=VS.80).aspx
     
    Last edited: Mar 21, 2012
  5. Mar 21, 2012 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    The C standard allows for a "minimum subset" version of the language that does not include the math functions. That was really intended for small microcontrollers that don't support floating point arithmetic in hardware, but the consequence is that you often have to explicitly call up the math library if you want to use it.
     
  6. Mar 21, 2012 #5
    You are getting confused between libraries and headers.
    math.h is a header. You don't need -lm for finding math.h
    The compiler and find and compile it fine without it.

    -l is a linker option. On gcc, -labc tells the linker to find a libabc.a and link to it.
    The functions prototyped in math.h exist in a library called libm.a - hence you need to tell the linker to link in a libm.a

    This is not just for math.h or libm.a
    There are a few libraries which get linked in by default, for all the others you need to specify a -l linker option. As you start writing more programs you will need more & more libraries.

    My build scripts had 5-6 -l options a lot of times.


    Just google for linker and you will find enough info on it.
     
    Last edited: Mar 21, 2012
  7. Mar 21, 2012 #6
    Compiler and linker used to be completely different programs. Compilers are specific to a language like C, Fortran, or whatever, whereas the linker puts together executable programs from the different compiled parts in .o files and .a libraries. The libraries may or may not be specific for a given language.

    The reason the linker is called via the C compiler is that it needs to include a few files that are part of each and any C program - the standard library and a bit of code that puts in place argc and argv and then calls main().

    Obviously you want your executable to be as small as possible, so you don't include libraries that your program does not need. A lot of basic C programs do not need floating point math, therefore it has been split off into libm.a. Therefore if you do need floating point math, you have to tell the compiler (math.h) and the linker (-lm). The same goes for several other libraries.
     
  8. Mar 22, 2012 #7
    Thanks to everyone for answering my questions :D
     
  9. Mar 22, 2012 #8

    rcgldr

    User Avatar
    Homework Helper

    Note that microsoft c (at least on msdos or windos) or visual studio doensn't need a special linker option.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Questions about C
  1. Question about C (Replies: 3)

Loading...