Problems with function gcvt in stdlib.h library

Hi, i was trying to cast a float variable (temperature) in a char variable using the function gcvt (double,int,char *), but i faced some problems when i call it in the program.

i'm trying to do  this beacause i need to send that temperature value trought a caop message, in a thread network.

when i send a coap request to the server from the client, the first one doesn't respond and, if i open a putty window, it can be seen that the device resets itself when it receive the request.

these are the projects for nRF5340 DK:

coap_rec_myserver.zipcoap_send_myclient.zip

also when i put the mouse cursor over the gcvt function in the stdlib.h library, appear a pop up window over the function saying: 

The function definition for 'gcvt' was not found



this is the path of stdlib.h library:

C:\ncs\toolchains\v2.1.1\opt\zephyr-sdk\arm-zephyr-eabi\arm-zephyr-eabi\sys-include\stdlib.h


how could i solve this?
Parents
  • Hi 

    Probably you just need to enable the standard C library in your project. To do so please add the following line to your project configuration (prj.conf):

    CONFIG_NEWLIB_LIBC=y

    Best regards
    Torbjørn

  • i've just done that, but the same problem occurs...

    did you try that and works fine?

  • Hi 

    It worked fine on my end, yes. 

    I just tried it again in v2.1.2 of the SDK and using the nrf5340dk_nrf5340_cpuapp board, to keep the test closer to your setup, and it still works fine. 

    You will find my test app attached, I simply modified the blinky sample in the SDK:

    304046_blinky_gcvt_test.zip

    Best regards
    Torbjørn

  • i tried your sample and works fine.

    the only thing is that when i open your project it works fine but there is a warning over the gcvt function that say:

    implicit declaration of function 'gcvt' [-Wimplicit-function-declaration]

    if i go to stdlib.h intellisense continue to not find the function saying the function definition was not found.

    i don't know why...

    if i try to implement gcvt in the original project it doesn't work... when i flash and run it resets itself...

    i tried also gcvtf, cause is the more approrpiate function since i work with float values, but the sema problem of reset come out...

    i also tried this in your project, to find out if gcvtf works fine:

    void main(void)
    {
        int ret;

        if (!device_is_ready(led.port)) {
            return;
        }

        ret = gpio_pin_configure_dt(&ledGPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            return;
        }

        printk("Blinky gcvt() test\n");

        double dval = 3.1415;
        float fval = 23.324;
        uint8_t buf[16];
        gcvt(dval6buf);
        //gcvtf(fval,6,buf);
        printk("Float conversion: %s\n"buf);
        usb_enable(NULL);
        while (1) {
            ret = gpio_pin_toggle_dt(&led);
            if (ret < 0) {
                return;
            }
            k_msleep(SLEEP_TIME_MS);
        }
    }
     
    it doesn't... it only resets each time the while cycle is done
    do i need to enable other standard C libraries?

    this is what it looks like the sdtlib.h part where there are gcvt functions:

    tree points that state: function saying the function definition was not found

    i don't know where to look for solutions...

Reply
  • i tried your sample and works fine.

    the only thing is that when i open your project it works fine but there is a warning over the gcvt function that say:

    implicit declaration of function 'gcvt' [-Wimplicit-function-declaration]

    if i go to stdlib.h intellisense continue to not find the function saying the function definition was not found.

    i don't know why...

    if i try to implement gcvt in the original project it doesn't work... when i flash and run it resets itself...

    i tried also gcvtf, cause is the more approrpiate function since i work with float values, but the sema problem of reset come out...

    i also tried this in your project, to find out if gcvtf works fine:

    void main(void)
    {
        int ret;

        if (!device_is_ready(led.port)) {
            return;
        }

        ret = gpio_pin_configure_dt(&ledGPIO_OUTPUT_ACTIVE);
        if (ret < 0) {
            return;
        }

        printk("Blinky gcvt() test\n");

        double dval = 3.1415;
        float fval = 23.324;
        uint8_t buf[16];
        gcvt(dval6buf);
        //gcvtf(fval,6,buf);
        printk("Float conversion: %s\n"buf);
        usb_enable(NULL);
        while (1) {
            ret = gpio_pin_toggle_dt(&led);
            if (ret < 0) {
                return;
            }
            k_msleep(SLEEP_TIME_MS);
        }
    }
     
    it doesn't... it only resets each time the while cycle is done
    do i need to enable other standard C libraries?

    this is what it looks like the sdtlib.h part where there are gcvt functions:

    tree points that state: function saying the function definition was not found

    i don't know where to look for solutions...

Children
  • Hi 

    I must have missed that warning when I tested my example, I now see it here as well. 

    Discussing this with the developers it seems that this function has been deprecated, and now the recommended alternative is to use the sprintf(..) function instead. 

    I modified my own test example to use sprintf(..) instead, and you will find the example attached:

    304046_blinky_sprintf_test.zip

    Please note that some additional configs are needed in order to support floating point operations with sprintf(..). 

    Also, if you want to convert float rather than double just replace "%.*g" with "%.*f"

    One odd thing I noticed is that for double conversions the number of digits is one less than the number provided in the num_digits variable, but in your application I assume you can just hard code this number anyway. 

    Best regards
    Torbjørn

Related