Thread: Compiling problem

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Compiling problem

    Trying to compile some code on my debian machine and am getting an error. I have to trouble getting it to compile on my XP machine. Compiler is gcc on both machines.

    Error is:
    Code:
    /tmp/ccFbD8lA.o: In function `main':  
    /tmp/ccFbD8lA.o(.text+0x9d): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    Code is:
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
            int a, b, c, a2;
            float x1, x2, part1;
    
    
            printf("A: ");
            scanf("%d", &a);
            printf("B: ");
            scanf("%d", &b);
            printf("C: ");
            scanf("%d", &c);
    
            part1 = sqrt( (b*b) - 4*a*c);
            a2 = 2*a;
            printf("-b = %d\nB^2 - 4AC = %f\n2A = %d.\n", -b, part1, a2);
    
            x1 = ((-b) + part1) / (2*a);
            x2 = ((-b) - part1) / (2*a);
    
            printf("X = %f\nX = %f", x1, x2);
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    gcc prog.c -lm

    That's lowercase LM
    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
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok it worked but what does the -lm do? I searched the man page for it and didn't even see that switch listed. Also it outputed the exe as a.out instead of prog.exe. Anyway to change this?

    BTW not used to command line compilers so bear with me please

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The 'l' specifies that you want to include additional libraries (usually found in /usr/lib)

    The parameter -lxxx
    means you want to link a library called libxxx.a
    That is, the lib prefix and .a suffix are assumed.

    It's a feature of history that libm is separate from libc

    The companion -L option specifies where to look for libraries, in case you have your own.

    > Also it outputed the exe as a.out instead of prog.exe.
    gcc -o prog.exe prog.c -lm

    a.out, short for assember.out, being the traditional default name for the final result of compiling and linking a program.
    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.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks Salem. I understand now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Running Program After Compiling
    By kristentx in forum C++ Programming
    Replies: 13
    Last Post: 09-12-2007, 10:46 AM
  2. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. compiling problem
    By tinkerbell20 in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2005, 12:12 PM
  5. simple compiling problem
    By waxydock in forum C++ Programming
    Replies: 2
    Last Post: 03-26-2005, 10:33 AM