Results 1 to 5 of 5

Thread: problem with <math.h> using gcc

  1. #1
    Junior Member
    Join Date
    Jan 2004
    Posts
    3

    problem with <math.h> using gcc

    I started using Knoppix (3.3) some days ago, mainly to write C-programs (but I'm liking it more&more, also for the rest of the package!).
    I am, however, experiencing problems in compiling programs that use <math.h>. F.i.:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(){
       printf("%f\n",sqrt(123.456));
       return 0;
       }
    Won't compile, giving:
    Code:
    knoppix@ttyp0[test]$ gcc test.c
    /tmp/cc8Hr3am.o(.text+0x1a): In function `main':
    : undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    Can someone explain what's going wrong? I have no problems including <ctype.h> or <string.h>.

    I'm a complete Linux noob, so please help me...

  2. #2
    Senior Member registered user
    Join Date
    Mar 2003
    Posts
    555
    It's a link-time error; you need to add '-lm' to your command line in order to pull in the math library.

  3. #3
    Junior Member
    Join Date
    Jan 2004
    Posts
    3
    Thx very much! It worked!

    Are there more problems to be expected when including standard header files? Is -lm always enough to cure these kind of linking problems?
    And more important: where can I find info on these matters?

    Thx in advance.

  4. #4
    Senior Member registered user
    Join Date
    Mar 2003
    Posts
    555
    These are not problems, just the way the C compiler works on pretty much every unix system. -lm is a shortcut meaning you want the linker to look in standard places for a library called libm.a. On linux, tens of libraries live under /usr/lib, amongst which the default libc.a. If you wanted to link against say libcups.a, you could similarly specify -lcups. If you want to see what's in a library , use ar (eg "ar -tvf /usr/lib/libm.a").

    As to where to look for information, I'm afraid the references I might give you would be embarassingly outdated (hints: Kernighan & Richie, Kernighan & Pike, Rochkind, Stevens). Just google for "unix programming". Have fun ... and be prepared for what might become a long journey!

  5. #5
    Junior Member
    Join Date
    Jan 2004
    Posts
    3
    Thanks again!
    I'm not afraid of a long journey!

Similar Threads

  1. COPROCESSOR AND MATH EMULATION
    By kb in forum General Support
    Replies: 0
    Last Post: 06-10-2004, 06:51 PM
  2. Math symbols messed up in Mozilla
    By Arkaein in forum Hdd Install / Debian / Apt
    Replies: 0
    Last Post: 05-03-2004, 07:58 PM
  3. openoffice math tutorial
    By cherry in forum General Support
    Replies: 0
    Last Post: 03-22-2004, 06:48 PM
  4. Problem Unique to Knoppix Later Versions!(SIS problem!)
    By bongski55 in forum Hardware & Booting
    Replies: 4
    Last Post: 11-15-2003, 10:19 AM
  5. Wierd Problem With USB Keyboard, Maybe Motherboard Problem
    By Triple Six in forum Hardware & Booting
    Replies: 2
    Last Post: 04-15-2003, 04:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


1U IBM x3550 M5 4 Bay SFF SAS3 Server 2x E5-2683 V3 28 Core 128GB DDR4 2x Tray picture

1U IBM x3550 M5 4 Bay SFF SAS3 Server 2x E5-2683 V3 28 Core 128GB DDR4 2x Tray

$318.00



IBM SYSTEM x3650 M4 Xeon E5-2609 2.40GHZ 16GB DDR3-1066MHZ 2x 550W PSU TESTED picture

IBM SYSTEM x3650 M4 Xeon E5-2609 2.40GHZ 16GB DDR3-1066MHZ 2x 550W PSU TESTED

$109.95



IBM Power S822 8284-22A 12SFF Power8 3.89GHz 6Core 64GB RAM No HDD Server System picture

IBM Power S822 8284-22A 12SFF Power8 3.89GHz 6Core 64GB RAM No HDD Server System

$359.99



IBM Power S822 12-Bay Server System Power8 Core 3.42Ghz DVD-Rom Drive 64GB No HD picture

IBM Power S822 12-Bay Server System Power8 Core 3.42Ghz DVD-Rom Drive 64GB No HD

$399.99



IBM SYSTEM x3200 M2 Server - HDD wiped, No OS picture

IBM SYSTEM x3200 M2 Server - HDD wiped, No OS

$75.00



IBM Power8 S822L 20-Core 3.42GHz 256Gb 1.2Tb 40G Elastic Storage Server 5148-22L picture

IBM Power8 S822L 20-Core 3.42GHz 256Gb 1.2Tb 40G Elastic Storage Server 5148-22L

$599.95



IBM 7875-AC1 for BladeCenter picture

IBM 7875-AC1 for BladeCenter

$66.50



IBM AC1 7870 BladeCenter Blade Server 2*Xeon X5570 2.93GHz 12GB SEE NOTES picture

IBM AC1 7870 BladeCenter Blade Server 2*Xeon X5570 2.93GHz 12GB SEE NOTES

$11.13



IBM Lenovo X3650 M5 2U 8x 2.5” CTO Rack Server – 2x HS, 2x 750W picture

IBM Lenovo X3650 M5 2U 8x 2.5” CTO Rack Server – 2x HS, 2x 750W

$199.00



IBM System X3650 Server M2 2 x Xeon X5570 2.93 Ghz w/128 GB/DVDRW picture

IBM System X3650 Server M2 2 x Xeon X5570 2.93 Ghz w/128 GB/DVDRW

$169.99