Thread: testing math.h

  1. #1
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65

    testing math.h

    I wrote this snippet to try some simple math but it appears I am the simple one:
    Code:
    // Test for math.h
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    
    int main(void)
    {
        double E;
        double M;
        double V;
        M = .25;
        V = 1400;
        E = M*(pow(V,2))/2;
        printf("Kinetic energy is %f", E);
        getchar();
        return(0);
    }
    Code:
    /*
    compiler error:
    gcc -Wall -o "mathtest" "mathtest.c" (in directory: /home/paul/Documents/C-files)
    /tmp/ccEbPuLK.o: In function `main':
    mathtest.c:(.text+0x45): undefined reference to `pow'
    collect2: error: ld returned 1 exit status
    Compilation failed.
    */
    I thought pow() was defined in math.h

    I am using Ubuntu 14.04.1, coding in C99, using Geany IDE.
    Am I ignorant? Am I missing the Math header file?

    Paul

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Am I missing the Math header file?
    No, but you need to add the math library to the compile line ( -lm ). Those are linker errors, not compile errors so the header file was found.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    jimblumberg,

    I am using an IDE - I don't understand how I am supposed to add the math library to the compile line.
    I don't add any of the other libraries - it is supposed to be done by the software...??

    Paul

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, it is not "supposed to be done by the software".

    There are two phases to building a program: compiling (which compiles source code to produce "compiled objects") and linking (which collects the "compiled objects" and connects them together to build an executable). For math functions to work, the link phase needs to link in the math library. Most real-world drivers (programs which orchestrate invoking the compiler and linker in some sequence) do NOT link in the math library by default. That includes IDEs. There are various historical reasons for that (arguing that it should be different won't change that state of affairs).

    The options to change that depend both on your IDE and (since IDEs can often be configured to work with different compilers and linkers) what compiler and linker you have installed.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Location
    SE Washington State
    Posts
    65
    Grumpy,
    So how do I add the math library to the compile line when using Geany in Ubuntu 14.04.1?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I've never used geany, so don't know specifically.

    If I was starting to use an IDE I haven't used before, I'd either read the documentation or explore the menus for build settings. Look specifically in a section related to "link options" or "link flags" and/or "link libraries" (or wordings that mean similar things). Essentially, you're looking for a place - as Jim said - where you can specify or select a "-lm" option (-l means library on the command line, m is the name of the math library).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I also don't use Geany (by the way, i use Anjuta), but i found a statement on web.
    Add -lm option to the end of gcc command line in the Build -> Set Build Commands menu.
    Link: c - How to link math.h library in geany? - Stack Overflow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 64 bit testing
    By DrSnuggles in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2007, 03:20 AM
  2. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  3. Testing Testing 123.. Oh yeah and...
    By minime6696 in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2001, 09:07 AM