C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-24-2003, 01:20 AM   #1
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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;
}
Thantos is offline   Reply With Quote
Old 11-24-2003, 02:20 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,681
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.

Salem is offline   Reply With Quote
Old 11-24-2003, 07:53 AM   #3
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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
Thantos is offline   Reply With Quote
Old 11-24-2003, 08:28 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,681
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.

Salem is offline   Reply With Quote
Old 11-24-2003, 08:31 AM   #5
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
Thanks Salem. I understand now.
Thantos is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Running Program After Compiling kristentx C++ Programming 13 09-12-2007 10:46 AM
Problem compiling simple code examples Wintersun Windows Programming 8 08-14-2007 10:30 AM
A question related to strcmp meili100 C++ Programming 6 07-07-2007 02:51 PM
compiling problem tinkerbell20 C++ Programming 6 06-21-2005 12:12 PM
simple compiling problem waxydock C++ Programming 2 03-26-2005 10:33 AM


All times are GMT -6. The time now is 01:49 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22