Use mathematical functions in Linux

Source: Internet
Author: User
Today, when compiling a c file that uses the log function, an error occurs:
/Tmp/ccquh0ns. O (. Text + 0x2bb): In function 'compute ':
: Undefined reference to 'log'

I checked the problem on the Internet. The reasons and solutions are as follows:

This error occurs because the compiler cannot find the specific implementation of log. although we include the correct header file, we still need to connect to the determined library during compilation. in Linux, to use mathematical functions, we must connect to the mathematical library. Therefore, we need to add the-LM option.
GCC select. C-o select-LM
This is done!

The full text is as follows: (http://netsim.bokee.com/4068917.html)

3. Library Link

Compile the following program.

/* Temp. C */
# Include

Int main (INT argc, char ** argv)
{
Double value;
Printf ("value: % F \ n", value );
}

This program is quite simple, but when we use gcc-O temp. C to compile it, we will see the following:

.

/Tmp/cc33kydu. O: In function 'main ':
/Tmp/cc33kydu. O (. Text + 0xe): Undefined reference to 'log'
Collect2: LD returned 1 exit status

This error occurs because the compiler cannot find the specific implementation of log. Although we include the correct Header

File, but we still need to connect to the determined library during compilation. in Linux, in order to use mathematical functions, I

You must connect to the math library. Therefore, we need to add the-LM option. GCC-O temp. C-lm so that we can

Correct compilation. Someone may ask, why didn't we connect to the library before using the printf function? Yes

For the implementation of some common functions, the GCC compiler will automatically connect to some common libraries, so that we do not

It is necessary to specify the library path when compiling the program.

We need to use the-L option of the compiler to specify the path. For example, we have a library under/home/Hoyt/mylib.

In this way, we need to add-L/home/Hoyt/mylib during compilation. For some standard libraries, we do not

It is necessary to specify the path. As long as they are in the path from the default library, you can. The path of the default library of the system/lib

The/usr/lib/usr/local/lib Libraries under these three paths can be left unspecified.

Another problem is that sometimes we use a function, but we do not know the library name.

What should I do? Sorry, I do not know the answer to this question. I have only one silly solution.

Go to the standard library path and check if there are any libraries related to my functions. Then I found the thread.

(Thread) function library file (libpthread. a). Of course, if you cannot find it, there is only one stupid way. For example, if you want to find

In the library where the sin function is located, you have to use nm-O/lib/*. So | grep sin> ~ /Sin command, and then see ~ /Sin

File, find it there. In the sin file, I will find such a line of libm-2.1.2.so: 9fa0

W sin so I know that sin is in the libm-2.1.2.so library, and I can use the-LM option (remove the front

The Lib and the later version mark, M is left, so it is-lm ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.