Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
915 views
in Technique[技术] by (71.8m points)

gcc - Undefined reference to 'pow' even though -lm is a compile flag. [C]

Any reason

cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN

would produce an error with code using math.h? Is it possible there's a difference between GCC version 4.0.3 (documented working version) and version 4.6.3 (my current version)?

makefile and asm.c @ https://gist.github.com/3801291

This is on ubuntu 12.04

My terminal output is a comment in the gist.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Instead of

cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c

Try:

cc -g -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c -lm

When the linker searches a library, it links in modules that contain definitions for previously-undefined symbols.

If the linker searches -lm before foo.o, then pow() is not yet undefined. Conversely, if foo.o comes first, it undefines pow(), which -lm can then resolve.


EDIT: To accomplish this advice in your makefile, make these changes:

CFLAGS=-g -DBLITZ_HOST_IS_LITTLE_ENDIAN
LDLIBS=-lm

...

asm: asm.c
        $(CC) $(CFLAGS) asm.c $(LDLIBS) -o asm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...