1
votes

The linking of math.lib using -lm is working is a different manner in gcc 4.6.3.

It use to work gcc -lm file.c but in gcc 4.6.3 its gcc file.c -lm.

What is the reason for this change? Or is it a bug?

Thanks

This is a part of the code.

    float i = 100;
    printf("%f", sqrt(i));
    return 0;

I complied like this

gcc -lm mat.c

/tmp/ccPxTEjS.o: In function `main':

mat.c:(.text+0x2c): undefined reference to `sqrt'

collect2: ld returned 1 exit status

Operating System : Ubuntu 12.04

1
This really surprises me. Are you sure? What actually happens if you run gcc -lm file.c on 4.6.3? You didn't specify what the problem is. - Jonathon Reinhart
The question stands. What happens? - Jonathon Reinhart
@JonathonReinhart Changes made in the question with the sample code. - Anish Chandran
@AnishChandran: I looked around and there are conflicting conclusion: some blaming gcc and some blaming the binutil linker. I don't know the exact answer myself. Probably what Basile Starynkevitch suggested is correct. - nhahtdh

1 Answers

3
votes

You said you tried to compile it as:

gcc -lm mat.c

Some configurations of gcc require strict ordering of options, which requires libraries to come after the source code that uses them:

gcc mat.c -lm

Does this work?