Setting up C++ code on the ESP8266_RTOS_SDK

grepson
Posts: 1
Joined: Wed Jan 24, 2024 5:22 pm

Setting up C++ code on the ESP8266_RTOS_SDK

Postby grepson » Wed Jan 24, 2024 5:29 pm

Hello ! I'm unable to make hello_world project work as .cpp file. The rename itself was enough for compiler to re-compile but with a error: startup.c:86: undefined reference to `app_main'. I've added to the main file extern to successfully compile the project:

Code: Select all

#ifdef __cplusplus
extern "C"{
#endif  

void app_main()
{
    printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    esp_chip_info(&chip_info);
    printf("This is ESP8266 chip with %d CPU cores, WiFi, ",
            chip_info.cores);

    printf("silicon revision %d, ", chip_info.revision);

    printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
            (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    // Create a task for the LED blinking
    // xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);

    initializeScreen();

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

#ifdef __cplusplus
}
#endif  
But It was not enough. The initializeScreen(); function uses my crude driver for 1602 lcd that was working without problem when the file was simple C. It is included as component in components folder of the project. Previously when this was C project I didn't have any problems with compiling the code, but now since the C main file was made CPP one, the project fails with following errors:

Code: Select all

/Users/macbookpro/esp/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/8.4.0/../../../../xtensa-lx106-elf/bin/ld: /Users/macbookpro/embedded_development/RTOS/ESP8266/lcd_test/build/main/libmain.a(hello_world_main.o):(.literal._Z16initializeScreenv+0x4): undefined reference to `initializeLCD(lcd_t*)'
/Users/macbookpro/esp/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/8.4.0/../../../../xtensa-lx106-elf/bin/ld: /Users/macbookpro/embedded_development/RTOS/ESP8266/lcd_test/build/main/libmain.a(hello_world_main.o):(.literal._Z16initializeScreenv+0x8): undefined reference to `sendString(lcd_t*, char*)'
/Users/macbookpro/esp/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/8.4.0/../../../../xtensa-lx106-elf/bin/ld: /Users/macbookpro/embedded_development/RTOS/ESP8266/lcd_test/build/main/libmain.a(hello_world_main.o): in function `initializeScreen()':
/Users/macbookpro/embedded_development/RTOS/ESP8266/lcd_test/main/hello_world_main.cpp:41: undefined reference to `initializeLCD(lcd_t*)'
/Users/macbookpro/esp/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/8.4.0/../../../../xtensa-lx106-elf/bin/ld: /Users/macbookpro/embedded_development/RTOS/ESP8266/lcd_test/main/hello_world_main.cpp:43: undefined reference to `sendString(lcd_t*, char*)'
collect2: error: ld returned 1 exit status
I know that those functions are present and can be used, since before changing the .c file to .cpp there were no problems. The driver itself is written in C, not in C++.

How should I resolve this problem?

MicroController
Posts: 1688
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Setting up C++ code on the ESP8266_RTOS_SDK

Postby MicroController » Thu Feb 29, 2024 11:22 pm

The C-header files you #include in your C++ code need

Code: Select all

#ifdef __cplusplus
extern "C"{
#endif  
too so that the compiler knows to treat the declarations therein like C (i.e. no name mangling).

Who is online

Users browsing this forum: No registered users and 10 guests