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

User avatar
By igrr
#6712 Looks like your user_init is defined in a cpp file. It has to be declared as extern "C", because it is referenced from libraries which expect it to be a C function.

Code: Select allextern "C" void user_init(){ ... }


Also all the SDK headers have to be put into extern "C" block:

Code: Select allextern "C" {
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "user_config.h"
}
User avatar
By mog
#6725 Thank you @igrr
After doing what you posted I've moved forward but then I've bumped into what you posted before:
Code: Select allWhen you succeed with that part you may get a bunch of errors regarding missing _sbrk_r function. To fix those you will likely have to override new, new[], delete, and delete[] operators to use os_malloc & os_free provided in the Espressif SDK.


With the following compiler errors:
Code: Select all/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin/xtensa-lx106-elf-g++ -Idriver -Iuser -Idriver/include -Iuser/include -Iinclude -I/opt/Espressif/include/ -I/opt/Espressif/ESP8266_SDK/include -I/opt/Espressif/ESP8266_SDK/include/json -Os -g -O2 -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals  -D__ets__ -DICACHE_FLASH -fno-rtti -fno-exceptions  -c user/user_main.cpp -o build/user/user_main.o
/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin/xtensa-lx106-elf-ar cru build/app_app.a build/user/user_main.o
/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin/xtensa-lx106-elf-gcc -L/opt/Espressif/ESP8266_SDK/lib -T/opt/Espressif/ESP8266_SDK/ld/eagle.app.v6.ld -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static -Wl,--start-group -lc -lgcc -lhal -lphy -lnet80211 -llwip -lwpa -lupgrade -lmain -lpp build/app_app.a -Wl,--end-group -o build/app.out
build/app_app.a(user_main.o):(.text+0x0): undefined reference to `operator delete(void*)'
build/app_app.a(user_main.o): In function `user_init':
/home/user/Documents/dev/hardware/wifinoise/esp8266/tests/c_vector_test/user/user_main.cpp:19: undefined reference to `operator delete(void*)'
/home/user/Documents/dev/hardware/wifinoise/esp8266/tests/c_vector_test/user/user_main.cpp:23: undefined reference to `operator new(unsigned int)'
build/app_app.a(user_main.o): In function `operator-<int*, std::vector<int> >':
/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/xtensa-lx106-elf/include/c++/4.8.2/bits/stl_iterator.h:898: undefined reference to `operator new(unsigned int)'
build/app_app.a(user_main.o): In function `__copy_m<int>':
/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/xtensa-lx106-elf/include/c++/4.8.2/bits/stl_algobase.h:371: undefined reference to `operator delete(void*)'
collect2: error: ld returned 1 exit status
make: *** [build/app.out] Error 1


I don't want to mess with new/delete operators while learning on working with SPI which is my initial goal. It seems like asking for trouble just from start :)
User avatar
By igrr
#6789 There's no need to "mess" with them, just define them somewhere in your project (inside a .cpp file):

Code: Select allvoid *operator new(size_t size) {
  return os_malloc(size);
}

void *operator new[](size_t size) {
  return os_malloc(size);
}

void operator delete(void * ptr) {
  os_free(ptr);
}

void operator delete[](void * ptr) {
  os_free(ptr);
}

extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
extern "C" void abort() {
  while(true); // enter an infinite loop and get reset by the WDT
}

void __cxa_pure_virtual(void) {
  abort();
}

void __cxa_deleted_virtual(void) {
  abort();
}
User avatar
By mog
#6830 Thank you @igrr

After some trial and error I was able to compile the source which is attached to this post. I've had to remove upgrade_ssl from LIBS in Makefile since it's missing and I don't know where to get it from nor I don't yet know what I'd need it for.
One thing I've found strange is that after pasting your code I've had problems with pvPortMalloc and vPortFree not being defined. After some search I've found that they were supposed to be defined in mem.h but were commented out:

Code: Select all#ifndef __MEM_H__
#define __MEM_H__

//void *pvPortMalloc( size_t xWantedSize );
//void vPortFree( void *pv );
//void *pvPortZalloc(size_t size);

#define os_malloc   pvPortMalloc
#define os_free     vPortFree
#define os_zalloc   pvPortZalloc

#endif


After uncommenting I've compiled the code so I don't know why they were actually removed. Because of that I've also added source from mem.h into my cpp file.

I still haven't tested if the code executes properly but thanks to your help I was able to compile it! :)
Regarding the attached code I've added an additional other_functions.c file to test if I'd be able to call os_printf which I'd really like to use for debugging purposes.
You do not have the required permissions to view the files attached to this post.