Compiling problem [Archive] - C Board

PDA

View Full Version : Compiling problem


Thantos
11-24-2003, 01:20 AM
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:
/tmp/ccFbD8lA.o: In function `main':
/tmp/ccFbD8lA.o(.text+0x9d): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Code is:

#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;
}

Salem
11-24-2003, 02:20 AM
gcc prog.c -lm

That's lowercase LM

Thantos
11-24-2003, 07:53 AM
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 :)

Salem
11-24-2003, 08:28 AM
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.

Thantos
11-24-2003, 08:31 AM
Thanks Salem. I understand now.