Page 1 of 1

Undefined reference to f_gets/f_puts error

Posted: Mon Aug 31, 2020 11:19 am
by olegboicenco
Hi to All!
I have an ESP-IDF project contained f_open(..) and f_puts(..)/f_gets(..).
Here is the part of my code:

if(f_open(&fil, u8GlobalNameOfFileName, FA_READ) == FR_OK)
{
f_gets(fileText, sizeof(FirstTextInCsv), &fil);
if(strcmp(fileText,FirstTextInCsv) == false)
{
PRINTF("The flash was populated\n");
ret = true;
}
}
else
{
PRINTF("Failed to open file for reading\n");
}
f_close(&fil);


After the compilation step, I get a linker errors:

c:/.espressif/tools/xtensa-esp32-elf/esp-2020r2-8.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(sd_card.c.obj):(.literal.SDHC_CheckFirstWrite+0x8): undefined reference to `f_gets'

c:/.espressif/tools/xtensa-esp32-elf/esp-2020r2-8.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(sd_card.c.obj):(.literal.SDHC_WriteFirstRowInCsv+0x0): undefined reference to `f_puts'

It is very strange that there are no errors for f_open (..) or f_close(..)!

I noticed that the functions f_open(), f_puts(), f_gets() are declared in the file {ESP-IDF}\components\fatfs\src\ff.h. And there are no “ifdefs” “separating” them. I also noticed that in the same directory there is a file ffconf.h, in which "FF_USE_STRFUNC 0" is defined with the following comments:

/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
/
/ 0: Disable string functions.
/ 1: Enable without LF-CRLF conversion.
/ 2: Enable with LF-CRLF conversion. */


It is logical to assume that FF_USE_STRFUNC must be defined as 1 or 2 to be able to use f_puts() and f_gets(). But is it right for the user to make changes to the framework file?
Can you please tell me how to solve or "work around" this problem?

Re: Undefined reference to f_gets/f_puts error

Posted: Tue Sep 01, 2020 12:24 am
by ESP_igrr
Hi olegboicenco,
Indeed we hadn't enabled FF_USE_STRFUNC option, as we assume that FATFS library will be used together with the VFS mechanism. That way you can use C standard library functions (fgets, fputs) to work with files.
If you have some existing code which uses FATFS APIs, you may enable FF_USE_STRFUNC. I don't think it will cause any side effects.

Re: Undefined reference to f_gets/f_puts error

Posted: Fri Oct 23, 2020 1:33 pm
by olegboicenco
Now it's clear! Thank you, ESP_Igrr!!

Re: Undefined reference to f_gets/f_puts error

Posted: Sun Dec 13, 2020 10:53 pm
by tyethgundry
Thanks from a troubled Keil + nordic semiconductor nrf52840-dk / nrf5 sdk user, and esp32 customer :D