GCC将无法正常math.h中包括 [英] gcc will not properly include math.h

查看:160
本文介绍了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以

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 ,而不是海湾合作委员会(因此退出状态消息) 。一般来说LD要求对象和库的顺序用户 供应商,其中用户指定是使用的库函数的对象和供应商是它提供的。对象

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

其中的大多数是指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 在后面的链接器命令,的之后 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天全站免登陆