Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » How to use math.h in Eclipse with gcc?
How to use math.h in Eclipse with gcc? [message #213974] Tue, 15 April 2008 11:00 Go to next message
Eclipse UserFriend
I tried to add the "-lm" option for gcc in the project properties:
-c -fmessage-length=0 -std=c99 -lm

However the eclipse still complains that:
undefined reference to `sqrt'

In the command line I can compile the code successfully.


Here is the code:

#include <stdio.h>
#include <math.h>

int main(void)
{

double x = 10;

double y;

y = sqrt(x);

printf("%.4f\n", y);

return 0;
}

Thanks for any help!

Regards
Warren
Re: How to use math.h in Eclipse with gcc? [message #214058 is a reply to message #213974] Wed, 16 April 2008 04:50 Go to previous messageGo to next message
Eclipse UserFriend
Hi Warren,

The "-lm" option is a linker option - looks like you've added it to the
Compiler 'Other flags', where it has no effect.
Add it to the linker options, Libraries-> add it in as a new library "m",
this will automatically add on the -l to the option.
(Alternately, you can add "-lm" to the Miscellaneous->Linker Flags box, but
I highly recommend the previous option).

Tracy

"Warren Tang" <warren.c.tang@gmail.com> wrote in message
news:fu2ftr$bai$1@build.eclipse.org...
>
> I tried to add the "-lm" option for gcc in the project properties:
> -c -fmessage-length=0 -std=c99 -lm
>
> However the eclipse still complains that:
> undefined reference to `sqrt'
>
> In the command line I can compile the code successfully.
>
>
> Here is the code:
>
> #include <stdio.h>
> #include <math.h>
>
> int main(void)
> {
>
> double x = 10;
>
> double y;
>
> y = sqrt(x);
>
> printf("%.4f\n", y);
>
> return 0;
> }
>
> Thanks for any help!
>
> Regards
> Warren
Re: How to use math.h in Eclipse with gcc? [message #214085 is a reply to message #214058] Wed, 16 April 2008 06:49 Go to previous messageGo to next message
Eclipse UserFriend
Hi, Tracy

You are absolutely right! It's so kind of you Tracy to tell me this.

Regards
Warren

Tracy Miranda wrote:
> Hi Warren,
>
> The "-lm" option is a linker option - looks like you've added it to the
> Compiler 'Other flags', where it has no effect.
> Add it to the linker options, Libraries-> add it in as a new library "m",
> this will automatically add on the -l to the option.
> (Alternately, you can add "-lm" to the Miscellaneous->Linker Flags box, but
> I highly recommend the previous option).
>
> Tracy
>
> "Warren Tang" <warren.c.tang@gmail.com> wrote in message
> news:fu2ftr$bai$1@build.eclipse.org...
>> I tried to add the "-lm" option for gcc in the project properties:
>> -c -fmessage-length=0 -std=c99 -lm
>>
>> However the eclipse still complains that:
>> undefined reference to `sqrt'
>>
>> In the command line I can compile the code successfully.
>>
>>
>> Here is the code:
>>
>> #include <stdio.h>
>> #include <math.h>
>>
>> int main(void)
>> {
>>
>> double x = 10;
>>
>> double y;
>>
>> y = sqrt(x);
>>
>> printf("%.4f\n", y);
>>
>> return 0;
>> }
>>
>> Thanks for any help!
>>
>> Regards
>> Warren
>
>
Re: How to use math.h in Eclipse with gcc? [message #232829 is a reply to message #213974] Fri, 10 April 2009 12:58 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

you should add the -lm option to GCC C Linker, NOT TO GCC C Compiler. To
do that, you should go to:

Project -> Properties -> C/C++ Build -> Settings -> Tool Settings (tab) ->
GCC C Linker

and in the "Command" text field type: "gcc -lm" (by default there is only
"gcc").

It worked for me;)

Regards,
Przemek.
Re: How to use math.h in Eclipse with gcc? [message #232836 is a reply to message #213974] Fri, 10 April 2009 13:01 Go to previous messageGo to next message
Eclipse UserFriend
Warren Tang wrote:


> I tried to add the "-lm" option for gcc in the project properties:
> -c -fmessage-length=0 -std=c99 -lm

> However the eclipse still complains that:
> undefined reference to `sqrt'

> In the command line I can compile the code successfully.


> Here is the code:

> #include <stdio.h>
> #include <math.h>

> int main(void)
> {

> double x = 10;

> double y;

> y = sqrt(x);

> printf("%.4fn", y);

> return 0;
> }

> Thanks for any help!

> Regards
> Warren

Hi,

you should add the -lm option to GCC C Linker, NOT TO GCC C Compiler. To
do that, you should go to:

Project -> Properties -> C/C++ Build -> Settings -> Tool Settings (tab) ->
GCC C Linker

and in the "Command" text field type: "gcc -lm" (by default there is only
"gcc").

It worked for me;)

Regards,
Przemek.

P.S. Sory for duplicating answers, but first time I didn't quote the
question;P
Re: How to use math.h in Eclipse with gcc? [message #1849943 is a reply to message #213974] Wed, 09 February 2022 05:46 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
I had the same problem today with math.h in Eclipse.
this post help resolve the issue ... but..

Why in the year 2022!! Eclipse is NOT automatically set the linker parameter when I declared the include of <math.h>?


Kindly ask:
Shouldn't this be an expected behavior ?
Are there any OTHERS (famous) libraries that are NOT linked automatically ?

BR
Giuliano

Eclipse 21-09 (Ubuntu) + CDT 10,4
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

[Updated on: Wed, 09 February 2022 05:47] by Moderator

Re: How to use math.h in Eclipse with gcc? [message #1849965 is a reply to message #1849943] Wed, 09 February 2022 19:12 Go to previous message
Eclipse UserFriend
Why in the year 2022!! Eclipse is NOT automatically
set the linker parameter when I declared the
include of <math.h>?


Because math.h is only loosely correlated to libm.a
You could be referring to your own set of routines.
Also, it would mean Eclipse knowing which headers
should be associated with which library. This can be
problematic as not all vendors are compliant with
the standard.

You are writing the program and should know what you
want to do and how to do it. CDT is not a magic box.

Are there any OTHERS (famous) libraries that are
NOT linked automatically


Most of them. GCC and Clang will tell you what libs are
scanned by default (the builtins). Other compilers maybe not.

[Updated on: Wed, 09 February 2022 21:38] by Moderator

Previous Topic:Project Build Configurations Question
Next Topic:Cross Compile Alsa for Windows
Goto Forum:
  


Current Time: Sun Jun 22 18:37:19 EDT 2025

Powered by FUDForum. Page generated in 0.05172 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top