gcc 不会正确包含 math.h [英] gcc will not properly include math.h

查看:17
本文介绍了gcc 不会正确包含 math.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个概述我的问题的最小示例

Here is a minimal example outlining my problem

test.c:

#include <stdio.h>
#include <math.h>

main ()
{
   fmod ( 3, 2 );
}

这是我发出的用于编译 test.c

And here is the command I am issuing to compile test.c

gcc -lm test.c -o test

这是我发出上述命令时得到的输出

And here is the output I get when I issue the above command

/tmp/ccQmRk99.o: In function `main':
test.c:(.text+0x3e): undefined reference to `fmod'
collect2: ld returned 1 exit status

如果我使用 cc,我会得到相同的输出.我正在使用以下版本的 gcc

I get the same output if instead I use cc. I am using the following version of gcc

gcc-4.6.real (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

知道我的程序无法编译的原因吗?

Any ideas why my program won't compile?

推荐答案

问题来自链接器 ld,而不是 gcc(因此出现退出状态消息).一般来说,ld 需要按照user supplier 的顺序指定对象和库,其中user 是使用库函数的对象,supplier 是提供它的对象.

The problem is coming from the linker, ld, rather than gcc (hence the exit status message). In general ld requires objects and libraries to be specified in the order user supplier, where user is an object that uses a library function and supplier is the object which provides it.

当你的 test.c 被编译成一个对象时,编译器声明 fmod 是一个未定义的引用

When your test.c is compiled to an object the compiler states that fmod is an undefined reference

$ gcc -c test.c
$ nm test.o
                 U fmod
0000000000000000 T main

(nm 列出目标文件引用的所有函数)

(nm lists all the functions referred to by an object file)

链接器将未定义的引用更改为已定义的引用,查找引用以查看它们是否在其他文件中提供.

The linker changes the undefined references to defined ones, looking up the references to see if they are supplied in other files.

$ gcc -lm test.o
$ nm a.out
0000000000600e30 d _DYNAMIC
0000000000600fe8 d _GLOBAL_OFFSET_TABLE_
00000000004006a8 R _IO_stdin_used
                 w _Jv_RegisterClasses
0000000000600e10 d __CTOR_END__
...
0000000000601018 D __dso_handle
                 w __gmon_start__
...
                 U __libc_start_main@@GLIBC_2.2.5
0000000000601020 A _edata
0000000000601030 A _end
0000000000400698 T _fini
0000000000400448 T _init
0000000000400490 T _start
00000000004004bc t call_gmon_start
0000000000601020 b completed.7382
0000000000601010 W data_start
0000000000601028 b dtor_idx.7384
                 U fmod@@GLIBC_2.2.5
0000000000400550 t frame_dummy
0000000000400574 T main

其中大部分是指在 main 之前和之后运行以设置环境的 libc 函数.可以看到 fmod 现在指向 glibc,共享库系统会在那里解析.

Most of these refer to libc functions that are run before and after main to set the environment up. You can see that fmod now points to glibc, where it will be resolved by the shared library system.

我的系统默认设置为使用共享库.如果我改为强制静态链接,我会得到您看到的订单依赖性

My system is set up to use shared libraries by default. If I instead force static linking I get the order dependency you see

$ gcc -static -lm test.o
test.o: In function `main':
test.c:(.text+0x40): undefined reference to `fmod'
collect2: ld returned 1 exit status

-lm 放在链接器命令的后面,after test.o,允许它成功链接.检查符号 fmod 现在应该解析为实际地址,确实是

Putting -lm later in the linker command, after test.o, allows it to link successfully. Checking the symbols fmod should now be resolved to an actual address, and indeed it is

$ gcc -static test.o -lm
$ nm a.out | grep fmod
0000000000400480 T __fmod
0000000000402b80 T __ieee754_fmod
0000000000400480 W fmod

这篇关于gcc 不会正确包含 math.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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