- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am using PSoC Creator 3.0 with PSoC4 PIONEER KIT.
this sentence:
a=atan2f(b,c);
where a, b and c are floats, gives me this error:
Build error: undefined reference to `atan2f'
However, the same sentence with constant parameters:
a=atan2f(1.0,2.0);
compiles without problems.
Of course I have #include <math.h> at the begining of my main.c
Any clue please?? I'm desperate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
So I cannot tell you why this happens (it actually should not, since both versions are functional identical. maybe there is some strange optimization happening with the first version, which inlines some code).
But I can tell you how to get it working:
- go to "Project" / "Build Settings..."
- select the "Linker" settings
- add "m" to the "Additional libraries" line (just that one character)
- Apply / OK
This pulls in the math library for linking - this is where the atan2f function really comes from.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I did some further tests, and it looks like the code
float f=atan2f(1.0,2.0);
gets inlined indeed (I tested with a dummy printf as output to avoid further optimization). The assembly output for this particular line looks like this:
22:.\main.c **** float f=atan2f(1.0,2.0); 53 .loc 1 22 0 54 0012 074B ldr r3, .L3 55 0014 7B60 str r3, [r7, #4] 23:.\main.c **** printf("%f",f); 71 .L3: 72 0030 3863ED3E .word 1055744824
So the compiler calculates this on compile time, and never needs the atan2f function during runtime. Thats why its need pulled in by the linker, thus no error happens.
I didn't know that gcc is _that_ smart 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you very much, hli for your help.
Writting "m" in the additional libraries field worked at once!!
Can you please explain how you arrived to that conclusion?
Is there any documentation I should read apart from PSOC creator help?
I have the impression that PSOC's modules datasheets are great, but when it comes to how standard c works in a particular system, too many things are taken for granted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's basic gcc knowledge.
atan2f is a math function, and they are not part of the normal libraries. The error message you get during compile hints at that ('undefined reference' always means that function is missing, and this is usually a missing library).
And since I happen to know that 'libm' is the math library, its just a matter of knowing where to add it (and that the 'lib' part doesn't need to be given, so its just 'm').