Gcc не будет включать math.h - программирование
Подтвердить что ты не робот

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

Любые идеи, почему моя программа не будет компилироваться?

4b9b3361

Ответ 1

Проблема исходит от компоновщика ld, а не gcc (следовательно, сообщение о статусе выхода). В общем случае ld требует, чтобы объекты и библиотеки указывались в порядке user supplier, где user - объект, который использует библиотечную функцию, и supplier - объект, который его предоставляет.

Когда ваш test.c скомпилирован для объекта, компилятор заявляет, что fmod является ссылкой undefined

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

(nm перечисляет все функции, на которые ссылается объектный файл)

Компоновщик изменяет ссылки undefined на определенные, просматривая ссылки, чтобы увидеть, поставляются ли они в других файлах.

$ 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 [email protected]@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 [email protected]@GLIBC_2.2.5
0000000000400550 t frame_dummy
0000000000400574 T main

Большинство из них относятся к функциям libc, которые запускаются до и после main, чтобы настроить среду. Вы можете видеть, что 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

Ответ 2

Из gcc (1) manpage: "размещение параметра -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.

Ответ 3

gcc -o test -lm test.c

выше команда решила мою проблему (математические функции, связанные с sqrt, pow, floor и т.д.)