Undefined reference to link Error

Source: Internet
Author: User
Tags mathematical functions

The reason is that a c file is compiled today. After entering the following code

The gop12.c file code is roughly

 

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <math. h> <br/> int main (INT argc, char * argv []) <br/>{< br/> // mathematical functions are used somewhere <br/>}

 

GCC compiles this file

 

Gcc-O gop12 gop12.c <br/>

Error

/Tmp/ccgjwwz6.o: In function 'main ':

Gop12.c :(. Text + 0x346): Undefined reference to 'ceil'

Gop12.c :(. Text + 0x3d4): Undefined reference to 'ceil'

Gop12.c :(. Text + 0x555): Undefined reference to 'ceil'

Gop12.c :(. Text + 0x617): Undefined reference to 'ceil'

Gop12.c :(. Text + 0x6d9): Undefined reference to 'ceil'

/Tmp/ccgjwwz6.o: gop12.c :(. Text + 0x991): More undefined references to 'ceil 'follow

Collect2: LD returned 1 exit status

I found the reason from this blog for a long time and expressed my heartfelt thanks to the blogger: http://chongchong.blog.edu.cn/2007/216174.html

I am using Ubuntu 10.0.4 + gcc4.43

In the new GCC version, GCC divides the standard library in c99 into libc and libm. libm includes some math libraries. h> (I used the ceil () function here), so if you want to use libm, you must add the compilation option-LM (that is, link libm). Otherwise, an error will be reported.

Correct the compilation command

Gcc-lm-O gop12 gop12.c

Compiled successfully.

Summary: when a special library is used, the corresponding link parameters should be added to the compilation command.

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.