The undefined reference to ' sin ' error occurs when compiling a C program with a mathematical function using the GCC compiler. This error is generally caused by a lack of libraries.
As a small program in the above diagram, the following problems occur when compiling under Linux:
Compile-time with the following options:
[HTML] view plain copy gcc Main.c-lm-o main
In fact, through this small problem, we can learn more knowledge.
Consider the following:
Clearly contains the MATH.H header file, how also shows that sin is not defined.
It turns out that the sin function of GCC is defined in libm.so.
The specified path is defined with the-l option. Do not fill in the words default is/lib and/usr/lib.
Because all of the function libraries in Linux start with Lib. So by removing the head and tail, then M is representing libm.so.
(M is the library name, libm.so is the library file name)
When using the "-l" parameter, it is customary to remove the "Lib" Function library header and subsequent version numbers, using the library name and parameter "-l" connections to form "-LM". As a result, we need to use "-l" to directly give the library name when GCC cannot find the library.
(To add: If the library is not in the default path, the "-l" option can be added to the path, gcc sin.c-lm-o sin equivalent to gcc sin.c -lm-l/lib-l/usr/lib -o sin) online all said libm.so in/lib or/usr/lib. In my Environment (ubuntu12.04) also successfully compiled.
But enter the directory (/lib and/usr/lib search is not found libm.so do not know why)
In a Sina blog see the following method search function:
I also tried this (but tragic, not found, screenshot below)
I see the first filename so there is a version number, I also relaxed the search scope.
[HTML] view plain copy nm-o/usr/lib/*.so.*|grep sin is still not found.
This is a lingering problem, which Bo friends know, hope to help me solve the doubt, first thank you
The first of the above Pictures borrowed Linux bar users asked when the picture (because the test did not capture, once compiled, the second not specified will be successful compilation, can not be screenshots)
Part of the above content to organize the Web page:
Http://blog.sina.com.cn/s/blog_60692ff60100hmpj.html
http://www.blogjava.net/ivanwan/archive/2005/08/04/ 9337.html
http://blog.sina.com.cn/s/blog_4dd855fe0100j9n1.html