pow() function does not compile if either parameter not constant

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
PeSi_1583586
Level 3
Level 3
10 replies posted 10 questions asked 5 replies posted

The pow() function does not compile if either the parameters are not constants as shown in the following example

#include <math.h>

double X = 10.0;

double Y = 5.0;

void Test()

{

double Z;

Z = pow(10.0, 5.0); // compiles correctly

Z= pow(X, 5.0); // generates error message 'Undefined reference to pow'

Z= pow(10.0, Y); generates error message 'Undefined reference to pow'

Z = pow(X, Y); // Generates error message 'Undefined reference to pow'

}

Is this intentional to limit code size? If so, is there a setting which I need to change in order to have full implementation of the pow() function?

I am using PSoC Creator 4.2

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

In Project -> Build Settings -> ARM -> Linker -> General ->Additional Libraries you need to add "m" to indicate that you want to use mlib library.

Sounds a bit funny, but will work

Bob

View solution in original post

4 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

In Project -> Build Settings -> ARM -> Linker -> General ->Additional Libraries you need to add "m" to indicate that you want to use mlib library.

Sounds a bit funny, but will work

Bob

odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

BeSi,

Keep in mind that power() function is incredibly slow, so in simple case like pow(X, 5), explicit expression X*X*X*X*X will be much faster.

/odissey1

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

A supererogatory comment of mine 😉

> Is this intentional to limit code size?

I think that

  "z = pow(CONST1, CONST2) ; "

was replaced with

  "z = CONST3 ; "

by the compiler,

so you did not need to link the library,

namely libm.a (math library).

All other lines were actually compiled into a function call to "pow()",

so they required link flag to specify appropriate library which contains pow().

In standard C argument specifying a library to link is "-l<x>", which means link "lib<x>.a",

therefore to link with "libm.a" we need to say "-lm",

so we could write "-lm" in the Build Settings ... > ARM .GCC.. > Comipler > Command Line > Custom Flags, too.

About Bob-san's suggestion,  in the Linker Additional Libraries, there is a comment

"Additional libraries to link to the executable being created. The linker searches a standard list of directories plus additional specified directories for the specified libraries, which are prepended with "lib" and appended with "a". The linker then uses this file as if it had been specified precisely by name."

This means, for "libm.a" just write "m" 😉

moto

0 Likes
PeSi_1583586
Level 3
Level 3
10 replies posted 10 questions asked 5 replies posted

Thank you all for your help. Adding 'm' to additional libraries did the trick!

You are right about it being incredibly slow! In my application using a PSoc 5 device the operation takes in the region of 75uS!!

Thankfully I only need to perform the calculation occasionally.

0 Likes