Hi
I want to use Sqrt() function from math.h but in my application it triggers some errors. Kindly guide me on how to properly include the math.h library into my application
Thanks & Regards,
Navin
Hi,
Could you please supply some more information regarding the errors you get? Either in image-form or copy paste the error code you get
If you've set up nRF Connect SDK correct, it should be straight forward to include the header to use the math functions. The following code should work if you'veset up the SDK similarly to this tutorial.
#include<stdio.h> #include<math.h> void main(void) { printf("Hello World! \n"); int a = 4; int b = sqrt(4); printf("b = %i \n", b); }
I hope this helps!
Kind regards,
Andreas
Hi Navin,
Thank you for supplying the error image!
To fix this this issue you need to add the following line to your prj.conf: CONFIG_NEWLIB_LIBC=y
This should enable and allow you to use libraries such as math.h and similar libraries that are not automatically included for use on the chips.
Let me know if this fixes the issue!
Kind regards,
Andreas
Hi,
1) Have you managed to build any other projects with your SES setup, or has this "failed to create project" error been present since the start?
2) I could not recreate your issue when I tried to create a new project and perform the same steps as I've told you to follow, but you could compare your steps to what I did:
a) Create the new project from a sample you want to build upon.
b) Add the math.h header to main.c and CONFIG_NEWLIB_LIBC=y to prj.conf
AHaug said:To fix this this issue you need to add the following line to your prj.conf: CONFIG_NEWLIB_LIBC=y
In SES you will have to reconfigure your project after adding items to the prj.conf file. If you've added this line via notepad or similar you will have to reopen the project for the changes to take effect. In my case when recreating a hello_world sample through SES with the math.h library included I did the following:
c) Restart SES and open the project again (to ensure that SES uses the changes in prj.conf)
d) Reconfigure the project as shown in the image: project -> Configure nRF Connect SDK Projeckt... and press "Configure"
e) rebuild and run the target.
f) Observe if the output in the terminal is correct.
3) A reason for this error could be the path of directory you're working from. In this similar case, the error was fixed by re-installing NCS closer to root level and shortening the path. If 2) does not work, try this.
4) I also strongly recommend that you consider to move over to use the VSCode extension instead of SES. In the future we will stop supporting SES in newer versions of NCS, and by moving over now you may get a head start on learning how to use the VS Code extension as well as keeping up to date on how to develop with Nordic products through newer and far better tutorials and guides. I personally also find it more intuitive to use than SES.
Let me know if this helps,
Kind regards,
Andreas