Undefined reference to pow

Undefined reference to pow

I’m trying to make a simple fibonacci calculator in C but when compiling gcc tells me that I’m missing the pow and floor functions. What’s wrong?

6 Answers 6

You need to compile with the link flag -lm , like this:

This will tell gcc to link your code against the math lib. Just be sure to put the flag after the objects you want to link.

Add -lm to your link options, since pow() and floor() are part of the math library:

For the benefit of anyone reading this later, you need to link against it as Fred said:

One good way to find out what library you need to link is by checking the man page if one exists. For example, man pow and man floor will both tell you:

An explanation for linking math library in C programming — Linking in C

In regards to the answer provided by Fuzzy:

I actually had to do something slightly different.

Project -> Properties -> C/C++ Build -> Settings -> GCC C Linker -> Libraries

Click the little green add icon, type m and hit ok. Everything in this window automatically has -l applied to it since it is a library.

To find the point where to add the -lm in Eclipse-IDE is really horrible, so it took me some time.

If someone else also uses Edlipse, here’s the way how to add the command:

Project -> Properties -> C/C++ Build -> Settings -> GCC C Linker -> Miscelleaneous -> Linker flags: in this field add the command -lm

All answers above are incomplete, the problem here lies in linker ld rather than compiler collect2: ld returned 1 exit status . When you are compiling your fib.c to object:

Читайте также:  Office 365 для студентов как подтвердить статус

Where nm lists symbols from object file. You can see that this was compiled without an error, but pow , floor , and printf functions have undefined references, now if I will try to link this to executable:

Im getting similar output you get. To solve that, I need to tell linker where to look for references to pow , and floor , for this purpose I will use linker -l flag with m which comes from libm.so library.

You can now see, functions pow , floor are linked to GLIBC_2.2.5 .

Parameters order is important too, unless your system is configured to use shared librares by default, my system is not, so when I issue:

Note -lm flag before object file. So in conclusion, add -lm flag after all other flags, and parameters, to be sure.

работает следующий бит кода:

в то время как это другое не делает?

Даже если я попробую

Что я делаю неправильно?

Когда это работает, это потому, что вычисление было выполнено самим компилятором (и включено в двоичный файл, как если бы вы его записали)

Когда это не работает, это потому, что функция pow включена в математическую библиотеку, а математическая библиотека по умолчанию не связана с вашим двоичным файлом.
Чтобы связать математическую библиотеку, если ваш компилятор gcc, используйте

С другими компиляторами должно быть одинаково:)
но прочитайте документацию

undefined reference to ‘pow’ звучит как ошибка компоновщика. Вы не связываетесь в математической библиотеке, даже если вы введете функцию pow , включив .

С помощью gcc используйте параметр командной строки -lm для ссылки в math lib.

Thread Tools
Search Thread
  • Linear Mode
  • Switch to Hybrid Mode
  • Switch to Threaded Mode
Читайте также:  Friday the 13th the game xbox one

"Undefined Reference" to pow and sqrt even though math.h header is there

Compiling this code tells me that there is no reference to pow() or sqrt(). The book didn’t tell me under which header these functions are located but I assume they are under math.h. Am I missing something here?

If you’re using gcc as your compiler, try adding -lm to the end of your compile line to link in the math library.

> scanf("%lf", side1);
And don’t forget the & either when using scanf

gcc -W -Wall -ansi -pedantic -O2 foo.c
Tells you when you make a mess of printf/scanf conversions (amongst other things)

What does the -lm argument do?

Is there anyway to get cc to tell me if I made a mess(should I be using gcc instead of cc?)?

-l specifies an additional library to search
m is the name of the library, in this case, its libm.a

-lfoo always maps to libfoo.a on a Unix/Linux system.

> should I be using gcc instead of cc?
Maybe you already are — cc is mapped to gcc on my box.

Ссылка на основную публикацию
Adblock detector