This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to use Math.h in nrf connect sdk

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

Parents
  • 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 Andreas,

    Thanks for your reply, I have got the below error, Kindly check and revert back.

    Thanks ,

    Navin 

  • 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,

    If I add the above command. I have facing the below error 

    But, the log is not generated

    Thanks & Regards

    navin

  • 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

  • Hi,

    I have attached the error log below. Kindly check and revert back

    Creating solution MLX90632.emProject
    D:/v1.7.1/toolchain/opt/bin/cmake.exe -GNinja -DBOARD=nrf5340dk_nrf5340_cpuapp -DBOARD_DIR=D:\v1.7.1\zephyr\boards\arm\nrf5340dk_nrf5340 -BD:\v1.7.1\zephyr\samples\My_Projects\MLX90632\build_nrf5340dk_nrf5340_cpuapp -SD:\v1.7.1\zephyr\samples\My_Projects\MLX90632 -DNCS_TOOLCHAIN_VERSION=1.7.1 -DDTC_OVERLAY_FILE=D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/nrf5340dk_nrf5340_cpuapp.overlay -DEXTRA_KCONFIG_TARGETS=menuconfig_ses -DEXTRA_KCONFIG_TARGET_COMMAND_FOR_menuconfig_ses=D:\v1.7.1\toolchain\segger_embedded_studio/html/configure_nordic_project_menuconfig.py
    -- Application: D:/v1.7.1/zephyr/samples/My_Projects/MLX90632
    -- Zephyr version: 2.6.99 (D:/v1.7.1/zephyr), build: v2.6.99-ncs1-1
    -- Found Python3: D:/v1.7.1/toolchain/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter 
    -- Found west (found suitable version "0.11.1", minimum required is "0.7.1")
    -- Board: nrf5340dk_nrf5340_cpuapp
    -- Cache files will be written to: D:/v1.7.1/zephyr/.cache
    -- Found dtc: D:/v1.7.1/toolchain/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6")
    -- Found toolchain: gnuarmemb (D:/v1.7.1/toolchain/opt)
    -- Found BOARD.dts: D:/v1.7.1/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp.dts
    -- Found devicetree overlay: D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/nrf5340dk_nrf5340_cpuapp.overlay
    -- Generated zephyr.dts: D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/build_nrf5340dk_nrf5340_cpuapp/zephyr/zephyr.dts
    -- Generated devicetree_unfixed.h: D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/build_nrf5340dk_nrf5340_cpuapp/zephyr/include/generated/devicetree_unfixed.h
    -- Generated device_extern.h: D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/build_nrf5340dk_nrf5340_cpuapp/zephyr/include/generated/device_extern.h
    -- Including generated dts.cmake file: D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/build_nrf5340dk_nrf5340_cpuapp/zephyr/dts.cmake
    Parsing D:/v1.7.1/zephyr/Kconfig
    Loaded configuration 'D:/v1.7.1/zephyr/boards/arm/nrf5340dk_nrf5340/nrf5340dk_nrf5340_cpuapp_defconfig'
    Merged configuration 'D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/prj.conf'
    -- Configuring incomplete, errors occurred!
    Including boilerplate (Zephyr base): D:/v1.7.1/zephyr/cmake/app/boilerplate.cmake
    -- Using NCS Toolchain 1.7.1 for building. (D:/v1.7.1/toolchain/cmake)
    
    D:/v1.7.1/zephyr/samples/My_Projects/MLX90632/prj.conf:16: warning: ignoring malformed line ' CONFIG_NEWLIB_LIBC=y'
    
    error: Aborting due to Kconfig warnings
    
    CMake Error at D:/v1.7.1/zephyr/cmake/kconfig.cmake:268 (message):
      command failed with return code: 1
    Call Stack (most recent call first):
      D:/v1.7.1/zephyr/cmake/app/boilerplate.cmake:554 (include)
      D:/v1.7.1/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:24 (include)
      D:/v1.7.1/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:35 (include_boilerplate)
      CMakeLists.txt:9 (find_package)
    
    
    
    error: cmake failed
    create_nordic_project.py failed (1)

    Thanks,

    Navin

  • Hi,

    The error you are getting could be due to the malformed line in your prj.conf (line 24), where there is a whitespace in line 24 where you've written

    ' CONFIG_NEWLIB_LIBC=y', instead of 'CONFIG_NEWLIB_LIBC=y'.

    Let me know if this worked out.

    Kind regards,
    Andreas

Reply
  • Hi,

    The error you are getting could be due to the malformed line in your prj.conf (line 24), where there is a whitespace in line 24 where you've written

    ' CONFIG_NEWLIB_LIBC=y', instead of 'CONFIG_NEWLIB_LIBC=y'.

    Let me know if this worked out.

    Kind regards,
    Andreas

Children
Related