Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 15:00 Go to next message
Warren Tang is currently offline Warren TangFriend
Messages: 32
Registered: July 2009
Member
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 08:50 Go to previous messageGo to next message
Tracy Miranda is currently offline Tracy MirandaFriend
Messages: 16
Registered: July 2009
Junior Member
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 10:49 Go to previous messageGo to next message
Warren Tang is currently offline Warren TangFriend
Messages: 32
Registered: July 2009
Member
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 16:58 Go to previous messageGo to next message
Przemek is currently offline PrzemekFriend
Messages: 2
Registered: July 2009
Junior Member
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 17:01 Go to previous messageGo to next message
Przemek is currently offline PrzemekFriend
Messages: 2
Registered: July 2009
Junior Member
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 10:46 Go to previous messageGo to next message
Giuliano Lotta is currently offline Giuliano LottaFriend
Messages: 9
Registered: June 2016
Junior Member
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 10:47]

Report message to a moderator

Re: How to use math.h in Eclipse with gcc? [message #1849965 is a reply to message #1849943] Thu, 10 February 2022 00:12 Go to previous message
David VavraFriend
Messages: 1427
Registered: October 2012
Senior Member
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: Thu, 10 February 2022 02:38]

Report message to a moderator

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


Current Time: Sat Feb 04 01:58:21 GMT 2023

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

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

Back to the top