3

Im currently writing a program for a uni asssessment and they have a set line to compile it, so if it doesn't work with that it won't be accepted. They command they use is

gcc -Wall -ansi -lm program.c -o program.out

My program will not compile that way, and it'll give me a undefined referance error (Referring to my log10 using math.h library) if i use:

gcc -Wall -ansi program.c -o program.out -lm

it works

What could be my issue?

Im using windows 10 64bit and have windows bash installed and gcc.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Paloking
  • 41
  • 4
  • What is the version of the gcc compliler? – Serkan Pekçetin Oct 13 '17 at 03:56
  • This is consistent with how GCC, at least, has always behaved for me - it will only resolve names in any given compilation unit (e.g. the object file created from `program.c`) to addresses in a later compilation unit (like `libm.a`, or would it be `m.lib` on Windows?). So I'm surprised that your university expects the compilation to work with `-lm` before the source file. – David Z Oct 13 '17 at 04:02
  • Im using gcc 5.4.0! Yea i though the same, but i'm not sure what to do.. Unless there some work around to make it work! – Paloking Oct 13 '17 at 04:07
  • @DavidZ: That's not quite how it works—the libraries are resolved in order but the object files are not. – Dietrich Epp Oct 13 '17 at 04:14
  • @DietrichEpp Ah, I've been putting my libraries last out of habit for so long I forgot. Still, though, the point stands for the question here: `-lm` needs to be at the end since it's a library, right?\ – David Z Oct 13 '17 at 04:16
  • @DavidZ: Yeah, that's the traditional behavior (unless you use gold), but the order of `.o` files relative to each other is not important. – Dietrich Epp Oct 13 '17 at 15:31
  • 1
    Does this answer your question? [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – phuclv Oct 24 '20 at 16:06

2 Answers2

2

This would be explained if your instructors are using gold and you are using GNU ld. These are two linkers, both are part of the GNU project, and both are commonly used with GCC.

If you are using GNU ld, you get the "traditional" behavior:

The order of specifying the -L and -l options, and the order of specifying -l options with respect to pathname operands is significant.

This means that you have to put -lm after any object files and libraries that depend on it.

However, if you are using gold, the -l options may appear first.

If you have gold installed on your system, you can test it yourself.

Here is what I get:

$ gcc -lm program.c 
/tmp/ccJmBjmd.o: In function `main':
program.c:(.text+0x15): undefined reference to `sin'
collect2: error: ld returned 1 exit status

But if I use gold, it works fine:

$ gcc -lm program.c -fuse-ld=gold
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
0

-lm needs to be at the end of the command, most likely in the first case with the literal the compiler is optimizing out the call to any function and therefore does not need to link against the library. This is called constant folding and for example we can see in the gcc docs on Other Built-in Functions Provided by GCC says:

GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with __builtin_ are always treated as having the same meaning as the C library function even if you specify the -fno-builtin option. (see C Dialect Options) Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.

krpra
  • 466
  • 1
  • 4
  • 19