即使 -lm 是编译标志,也未定义对“pow"的引用.[C] [英] Undefined reference to 'pow' even though -lm is a compile flag. [C]

查看:27
本文介绍了即使 -lm 是编译标志,也未定义对“pow"的引用.[C]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何理由

cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN

使用 math.h 的代码会产生错误吗?GCC 4.0.3 版(已记录的工作版本)和 4.6.3 版(我当前的版本)之间是否可能存在差异?

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)?

makefileasm.c @ https://gist.github.com/3801291

这是在 ubuntu 12.04 上

This is on ubuntu 12.04

我的终端输出是要点中的注释.

My terminal output is a comment in the gist.

推荐答案

代替

cc -g -lm -DBLITZ_HOST_IS_LITTLE_ENDIAN foo.c

试试:

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.

如果链接器在 foo.o 之前搜索 -lm,则 pow() 尚未定义.相反,如果 foo.o 首先出现,它会取消定义 pow(),然后 -lm 可以解析.

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:要在您的 makefile 中完成此建议,请进行以下更改:

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

这篇关于即使 -lm 是编译标志,也未定义对“pow"的引用.[C]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆