Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Linking a binary, order of arguments

Name: Anonymous 2013-05-14 13:45

Sup /prog/oyim. I'm having trouble linking a binary. Apparently the order of arguments has something to do with it. My Makefile instruction expands to this:

cc -lm -o myprog myprog.o libmylib.a

This works fine with the following compilers:

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
gcc version 4.2.1 20070831 patched [FreeBSD]


However, gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) gives the follow

myprog.o: In function `p_expt':
myprog.c:(.text+0x4670): undefined reference to `pow'
myprog.o: In function `p_sqrt':
myprog.c:(.text+0x46e7): undefined reference to `sqrt'
myprog.o: In function `p_atan':
myprog.c:(.text+0x478c): undefined reference to `atan2'
myprog.c:(.text+0x47a8): undefined reference to `atan'
myprog.o: In function `p_acos':
myprog.c:(.text+0x4817): undefined reference to `acos'
myprog.o: In function `p_asin':
myprog.c:(.text+0x4887): undefined reference to `asin'
myprog.o: In function `p_tan':
myprog.c:(.text+0x48f7): undefined reference to `tan'
myprog.o: In function `p_cos':
myprog.c:(.text+0x4967): undefined reference to `cos'
myprog.o: In function `p_sin':
myprog.c:(.text+0x49d7): undefined reference to `sin'
myprog.o: In function `p_log':
myprog.c:(.text+0x4a47): undefined reference to `log'
myprog.o: In function `p_exp':
myprog.c:(.text+0x4ab7): undefined reference to `exp'
myprog.o: In function `p_round':
myprog.c:(.text+0x4b2d): undefined reference to `floor'
myprog.o: In function `p_floor':
myprog.c:(.text+0x4b97): undefined reference to `floor'
myprog.o: In function `p_truncate':
myprog.c:(.text+0x4c0d): undefined reference to `ceil'
myprog.o: In function `p_ceiling':
myprog.c:(.text+0x4c77): undefined reference to `ceil'
collect2: ld returned 1 exit status
make: *** [myprog] Error


And Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) compiles, but running gives me a segfault.

I managed to make it work on all platforms by moving my LDFLAGS to the end of the linker command.

cc -o myprog myprog.o libmylib.a -lm

My question is why does having the -lm flag first cause shit to go haywire?

Name: Anonymous 2013-05-14 14:07

Did you #include <math.h>?

Name: Anonymous 2013-05-14 14:13

linux is shit anyway, stick with bsd systems

Name: Anonymous 2013-05-14 14:35

Okay, I figured it out. Apparently -lx is an alias for /path/to/libx.{a,dynlib,whatever} so it must be included with the sources and kept out of LDFLAGS altogether.

Name: Anonymous 2013-05-14 18:41

try cc -o myprog myprog.o libmylib.a -lm instead

Name: Anonymous 2013-09-01 13:36


There is a transfinite sequence of cardinal numbers:
    0, 1, 2, 3, ..., n; ℵ0, ℵ0, ℵ1, ... ℵα, ...

Name: Anonymous 2013-09-01 15:54


Each set in this hierarchy is assigned (by transfinite recursion) an ordinal number α, known as its rank. The rank of a pure set X is defined to be the least upper bound of all successors of ranks of members of X.

Don't change these.
Name: Email:
Entire Thread Thread List