0

I added the header <math.h> to a project to start to test out the fmod function, which returns a fractional part of float division. I have more questions on that, but i'll save it for another question.

So after adding the header and using the function, no lint errors went off then I try to build the program.

I get this error:

/usr/bin/ld: CMakeFiles/main.dir/main.c.o: undefined reference to symbol 'fmod@@GLIBC_2.2
.5'
//lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

okay so getting better at reading c errors i noticed ld which means linker, okay fine.

Next I start googling the error and I find the answer. I need to add the linker flag -lm

why? I don't know so I start searching google what that linker flag means and I didn't really find any answers. Went back to SO and found this question and answer.

these were the quotes under the main question.

Read the library documentation ... – pmg

...and then google. – Ori Pessach

linking semantics vary across operating systems and compilers. We'd need a lot more detail to make any useful contributions. – radical7

pkg-config seems okay if I have a one off file but as a project grows if you get these linker errors it becomes hard to figure out what's wrong.

Another answer was read the docs. I went back to google and came across this documentation about math.h

but nowhere does it say anything about the actual linker flags to use.

So my question what documentation was @pmg talking about in his reply? What's a general purpose way to find this documentation on my system just in case I cannot use google for whatever reason? Especially since I have the files on my computer and able to include them.

Community
  • 1
  • 1
user1610950
  • 1,837
  • 5
  • 33
  • 49
  • here is an answer related to your question http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – terence hill Dec 03 '15 at 11:34

2 Answers2

1

Well, if you type man fmod into Google (man is the UNIX help system) and click the first link you get documentation about the function. This clearly states what headers you must include and what library you must link with:

Synopsis

#include <math.h>

double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

Link with -lm.
Joe
  • 7,378
  • 4
  • 37
  • 54
1

Basically you always have to link the library when you use a function included in the library. There are some libraries, the most used, that are linked by default (see Why do you have to link the math library in C?) by the compiler.

If you are on a Unix like system you can refer to man pages:

man func

or

man -k func

the last gives you all the man pages related to func. There are also general man pages as the one for all the math functions (man math) or the ascii table (man ascii).

Unfortunately not all man pages report the needed link to add to the command line. In the man page for math there is no explicit state of -lm however, in the very end, the name of the library is reported:

"The libm functions declared in math.h provide mathematical library functions"

This is enough because, if the name of the library is libm then you know that to link it you should use:

-lm

As the general rule is that the linker remove the lib prefix when linking with -l, see for example this http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

Community
  • 1
  • 1
terence hill
  • 3,354
  • 18
  • 31
  • I actually tried man math but on debian 8 jessie it returned no manual entry for math. man fmod worked though and it showed the -lm linker argument which is cool! Thanks for the additional resources, just what I was looking for! Kudos for keeping it mostly on the system with man. – user1610950 Dec 03 '15 at 12:06