Re: Problem compiling simple C program
On 2008-04-15 20:39 +0200, John Salmon wrote:
> I'm running Debian Etch on a PC. When I try to compile the following
> (called test.c);
>
> #include <math.h>
> #include <stdio.h>
>
> int main()
> {
> double
> val = 1.55;
>
> printf("sine: %g\n", sin(val));
>
> return 0;
> }
>
> using the command line
>
> gcc -Wall -o test test.c
>
> I get
>
> /tmp/cciDV02m.o: In function `main':
> test.c:(.text+0x21): undefined reference to `sin'
> collect2: ld returned 1 exit status
You need to link to the math library by specifying -lm _at the end_ of
the gcc command line, otherwise the linker does not know about the sin()
function.
> When I compile the equivalent C++ program using the apropriate C++
> parameters, everything goes great. Have I neglected to load a Debian
> package? Any help will be appreciated.
That is to be expected, because C++ programs are automatically linked
against the math library. In C you have to tell the linker to use it
with -lm.
Sven
Reply to: