gcc不会正确包含math.h

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gcc不会正确包含math.h相关的知识,希望对你有一定的参考价值。

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

test.c的:

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

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

这是我发布的用于编译test.c的命令

gcc -lm test.c -o test

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

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

如果我使用cc,我得到相同的输出。我使用的是以下版本的gcc

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

我的程序无法编译的任何想法?

答案

问题来自链接器ld,而不是gcc(因此退出状态消息)。通常,ld要求以user supplier的顺序指定对象和库,其中user是使用库函数的对象,supplier是提供它的对象。

当您的test.c编译为对象时,编译器声明fmod是未定义的引用

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

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

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

$ 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,它将由共享库系统解析。

我的系统默认设置为使用共享库。如果我改为强制静态链接,我会得到你看到的顺序依赖

$ 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现在应解析为实际地址,实际上是

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

从gcc(1)联机帮助页:“-l选项的位置很重要。”

特别:

   -llibrary
   -l library
       Search the library named library when linking.  (The second alternative with the library as a
       separate argument is only for POSIX compliance and is not recommended.)

       It makes a difference where in the command you write this option; the linker searches and processes
       libraries and object files in the order they are specified.  Thus, foo.o -lz bar.o searches library z
       after file foo.o but before bar.o.  If bar.o refers to functions in z, those functions may not be
       loaded.

       The linker searches a standard list of directories for the library, which is actually a file named
       liblibrary.a.  The linker then uses this file as if it had been specified precisely by name.

       The directories searched include several standard system directories plus any that you specify with
       -L.

       Normally the files found this way are library files---archive files whose members are object files.
       The linker handles an archive file by scanning through it for members which define symbols that have
       so far been referenced but not defined.  But if the file that is found is an ordinary object file, it
       is linked in the usual fashion.  The only difference between using an -l option and specifying a file
       name is that -l surrounds library with lib and .a and searches several directories.

以上是关于gcc不会正确包含math.h的主要内容,如果未能解决你的问题,请参考以下文章

math.h 的问题

linux(ubuntu)GCC编译包含库函数的问题

通过使用 AVX 内部函数重写来提高 math.h 函数的性能

GCC混淆标准标题与个人名称相同

-Ofast 的 GCC 问题?

Relay.js 没有正确解析组合片段