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 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
Previous Topic:Including Headers, help needed (dshow)
Next Topic:More on inactive ifdef's
Goto Forum:
  


Current Time: Fri Dec 19 00:48:51 GMT 2014

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

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