-->
Page 1 of 1

Undefined reference to a math function

PostPosted: Mon Oct 26, 2015 4:54 am
by dimesp
I am new to C programming and I 've started a simple project in Eclipse.
I can't compile my project that uses a atan2 function.

my code looks like this (I 've added #include <math.h>) :
Code: Select all  double avgWindDir;
  double a=sumEastWest;
  double b=sumNorthSouth;
  avgWindDir = atan2(a, b);

The error is
/user_main.c:70: undefined reference to `atan2'

If for example I set :
Code: Select all  double a=1;
  double b=2;

it compiles ok.

I 've searched some other forums about this and they say it's a linker error and the solution is to add -lm to linker options.
Where is the correct place to add this option ?

Re: Undefined reference to a math function

PostPosted: Mon Oct 26, 2015 4:38 pm
by jcmvbkbc
dimesp wrote:the solution is to add -lm to linker options.
Where is the correct place to add this option ?

Depends on the build system you use, in case of Makefiles add it to the LDFLAGS, LIBS or other similarly named variable that holds names of other libraries to link with.

Re: Undefined reference to a math function

PostPosted: Tue Oct 27, 2015 1:14 am
by dimesp
Thank you
Finally it compiles adding m to LIBS
Code: Select all# libraries used in this project, mainly provided by the SDK
LIBS = c gcc hal phy pp net80211 lwip wpa main m


Thank you