Discuss here different C compiler set ups, and compiling executables for the ESP8266

User avatar
By raulMrello
#12174 Hi, I'm getting crazy with this issue, I've read the forums several times but didn't found a solution.
I'm compiling the basic example "blinky" without problem, but if I add a reference to malloc, I get several errors I don't understand.

This is my code:
Code: Select all/*
   The obligatory blinky demo
   Blink an LED on GPIO pin 2
*/

#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <stdlib.h>                                    // <--- ADDED BY ME

// see eagle_soc.h for these definitions
#define LED_GPIO 2
#define LED_GPIO_MUX PERIPHS_IO_MUX_GPIO2_U
#define LED_GPIO_FUNC FUNC_GPIO2

#define DELAY 1000000 /* microseconds */

extern void ets_wdt_enable (void);
extern void ets_wdt_disable (void);
extern void ets_delay_us(int delay);

extern void wdt_feed (void);

void user_init(void)
{
   uint8_t * pstate = (uint8_t*)malloc(sizeof(uint8_t));        // <--- ADDED BY ME
   *pstate = 0;                                    // <--- ADDED BY ME
   uint8_t state=0;
   ets_wdt_enable();
   ets_wdt_disable();
   // Configure pin as a GPIO
   PIN_FUNC_SELECT(LED_GPIO_MUX, LED_GPIO_FUNC);
   for(;;)
   {
      GPIO_OUTPUT_SET(LED_GPIO, *pstate);                  // <--- ADDED BY ME
         //GPIO_OUTPUT_SET(LED_GPIO, state);               // <--- COMMENTED BY ME
      os_delay_us(DELAY);
      *pstate ^=1;                                 // <--- ADDED BY ME
         //state ^=1;                                 // <--- COMMENTED BY ME
      wdt_feed();
   }
}


As you can see i've ADDED a bit of code, using "malloc" (I also include stdlib.h). But I get these compiler result:

Code: Select allc:/espressif/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib\libc.a(lib_a-mallocr.o):(.literal+0x24): undefined reference to `_sbrk_r'
c:/espressif/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib\libc.a(lib_a-mallocr.o): In function `malloc_extend_top':
d:\Neo\Project\ESP8266\DevKit\build\compiler\dl\esp-newlib\build\xtensa-lx106-elf\newlib\libc\stdlib/../../../../../newlib/libc/stdlib/mallocr.c:2165: undefined reference to `_sbrk_r'
d:\Neo\Project\ESP8266\DevKit\build\compiler\dl\esp-newlib\build\xtensa-lx106-elf\newlib\libc\stdlib/../../../../../newlib/libc/stdlib/mallocr.c:2202: undefined reference to `_sbrk_r'
c:/espressif/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib\libc.a(lib_a-freer.o): In function `_malloc_trim_r':
d:\Neo\Project\ESP8266\DevKit\build\compiler\dl\esp-newlib\build\xtensa-lx106-elf\newlib\libc\stdlib/../../../../../newlib/libc/stdlib/mallocr.c:3325: undefined reference to `_sbrk_r'
d:\Neo\Project\ESP8266\DevKit\build\compiler\dl\esp-newlib\build\xtensa-lx106-elf\newlib\libc\stdlib/../../../../../newlib/libc/stdlib/mallocr.c:3332: undefined reference to `_sbrk_r'
c:/_workspace/minimal_multithreaded_framework/project/eclipse_windows_esp8266/espressif/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib\libc.a(lib_a-freer.o):d:\Neo\Project\ESP8266\DevKit\build\compiler\dl\esp-newlib\build\xtensa-lx106-elf\newlib\libc\stdlib/../../../../../newlib/libc/stdlib/mallocr.c:3340: more undefined references to `_sbrk_r' follow
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe: *** [build/app.out] Error 1
C:/Project/blinky/Makefile:216: recipe for target 'build/app.out' failed


First of all I don't really know why appears path "d:\Neo\Project\ESP8266\DevKit..." during compilation (it isn't in my computer).

I think it doesn't found library libc.a (and hence it is failing during compilation). But Makefile is the original one, I didn't change anything so I supposed I could use standard library function calls like malloc, free, etc....

Anybody can help?? Thanks.
User avatar
By raulMrello
#12457 Hi Byjcmvbkbc .
Even doing that:
Code: Select all#include <mem.h>
void user_init(void)
{
   char* p = (char*)os_malloc(10);
   *p=3;


Now throws other errors:
Code: Select alluser/user_main.c:25:2: error: implicit declaration of function 'pvPortMalloc' [-Werror=implicit-function-declaration]
  char* p = (char*)os_malloc(10);
  ^
cc1.exe: all warnings being treated as errors


I've been looking for that macro (pvPortMalloc) and there is no implementation, well I've only found that there is only a implementation on the FreeRTOS source code, but no more.

I suppose I could do something like this:
Code: Select all#include <stdlib.h>
void *pvPortMalloc( size_t xWantedSize ){
   return malloc(xWantedSize);
}


But it is the same original problem. Any other possibility???

thanks.
User avatar
By jcmvbkbc
#12472
raulMrello wrote:Hi Byjcmvbkbc .

Please, somebody, fix this gluing of "By" with nick?

raulMrello wrote:Even doing that:
Code: Select all#include <mem.h>
void user_init(void)
{
   char* p = (char*)os_malloc(10);
   *p=3;


Now throws other errors:
Code: Select alluser/user_main.c:25:2: error: implicit declaration of function 'pvPortMalloc' [-Werror=implicit-function-declaration]
  char* p = (char*)os_malloc(10);
  ^
cc1.exe: all warnings being treated as errors

...
But it is the same original problem. Any other possibility???

No it's not. SDK from Espressif has some significant issues, e.g. missing prototypes for many of its functions. But it was you who chose to treat warnings as errors.
You have the following options here: either don't treat warnings as errors (easiest, though error-prone, but apparently "the official Espressif way"), or write your own prototypes (or maybe find others who've done that already).