COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

Postby mikemoy » Fri Jul 19, 2019 3:36 am

I am trying this subject again but with the latest stable release.
Using CMake with esp-idf-v3.2.2. I am trying to embed a index.html file. Embedding files was so easy using make. Switching over to CMake i have not had any luck in doing so. Following the docs does not work for me.
Here is the doc reference.
https://docs.espressif.com/projects/esp ... inary-data

The main trouble is i get this in the following when compiling.

Code: Select all

(.rodata.embedded+0x0): multiple definition of `index_html'
main/libmain.a(hello_world_main.c.obj):(.data.index_html+0x0): first defined here

Here is the full result of the attempt to compile.

Code: Select all

C:\Users\Mike\ESP32\Source\hello_world>idf.py build size
Note: You are using Python 3.7.3. Python 3 support is new, please report any problems you encounter. Search for 'Setting the Python Interpreter' in the ESP-IDF docs if you want to use Python 2.7.
Checking Python dependencies...
Python requirements from C:\Users\Mike\ESP32\esp-idf\requirements.txt are satisfied.
Running ninja in directory C:\Users\Mike\ESP32\Source\hello_world\build
Executing "ninja all"...
[1/5] Performing build step for 'bootloader'
ninja: no work to do.
[2/3] Linking CXX executable hello-world.elf
FAILED: hello-world.elf
cmd.exe /C "cd . && C:\Users\Mike\.espressif\tools\xtensa-esp32-elf\1.22.0-80-g6c4433a5-5.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe    -nostdlib CMakeFiles/hello-world.elf.dir/dummy_main_src.c.obj  -o hello-world.elf  -Wl,--gc-sections -Wl,--cref -Wl,--Map=hello-world.map -Wl,--start-group soc/libsoc.a log/liblog.a heap/libheap.a xtensa-debug-module/libxtensa-debug-module.a app_trace/libapp_trace.a freertos/libfreertos.a vfs/libvfs.a newlib/libnewlib.a esp_ringbuf/libesp_ringbuf.a driver/libdriver.a esp_event/libesp_event.a ethernet/libethernet.a lwip/liblwip.a tcpip_adapter/libtcpip_adapter.a app_update/libapp_update.a spi_flash/libspi_flash.a mbedtls/libmbedtls.a micro-ecc/libmicro-ecc.a bootloader_support/libbootloader_support.a nvs_flash/libnvs_flash.a pthread/libpthread.a smartconfig_ack/libsmartconfig_ack.a wpa_supplicant/libwpa_supplicant.a esp32/libesp32.a cxx/libcxx.a asio/libasio.a jsmn/libjsmn.a coap/libcoap.a console/libconsole.a nghttp/libnghttp.a esp-tls/libesp-tls.a esp_adc_cal/libesp_adc_cal.a tcp_transport/libtcp_transport.a esp_http_client/libesp_http_client.a esp_http_server/libesp_http_server.a esp_https_ota/libesp_https_ota.a expat/libexpat.a wear_levelling/libwear_levelling.a sdmmc/libsdmmc.a fatfs/libfatfs.a freemodbus/libfreemodbus.a json/libjson.a libsodium/liblibsodium.a mdns/libmdns.a mqtt/libmqtt.a openssl/libopenssl.a protobuf-c/libprotobuf-c.a protocomm/libprotocomm.a spiffs/libspiffs.a ulp/libulp.a wifi_provisioning/libwifi_provisioning.a main/libmain.a -lgcov -Wl,--undefined=uxTopUsedPriority -L C:/Users/Mike/ESP32/esp-idf/components/newlib/lib -lc -lm -L C:/Users/Mike/ESP32/esp-idf/components/esp32/lib -L C:/Users/Mike/ESP32/esp-idf/components/esp32/ld/wifi_iram_opt -lcoexist -lcore -lespnow -lmesh -lnet80211 -lphy -lpp -lrtc -lsmartconfig -lwpa2 -lwpa -lwps -L C:/Users/Mike/ESP32/Source/hello_world/build/esp32 -T esp32_out.ld -L C:/Users/Mike/ESP32/esp-idf/components/esp32/ld -T esp32.common.ld -T esp32.rom.ld -T esp32.peripherals.ld -T esp32.rom.libgcc.ld -T esp32.rom.spiram_incompatible_fns.ld C:/Users/Mike/ESP32/esp-idf/components/esp32/libhal.a -lgcc -u call_user_start_cpu0 -u ld_include_panic_highint_hdl -lstdc++ -u __cxa_guard_dummy -u __cxx_fatal_exception && cd ."
main/libmain.a(index.html.S.obj): In function `index_html':
(.rodata.embedded+0x0): multiple definition of `index_html'
main/libmain.a(hello_world_main.c.obj):(.data.index_html+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

C:\Users\Mike\ESP32\Source\hello_world>
This is my CMakeLists file

Code: Select all

set(COMPONENT_SRCS hello_world_main.c)
set(COMPONENT_ADD_INCLUDEDIRS "")

set(COMPONENT_EMBED_TXTFILES index.html)

register_component()

This is my program.

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <esp_log.h>
#include <esp_http_server.h>
#include "esp_http_client.h"
#include "lwip/apps/sntp.h"
#include "esp_err.h"

extern const uint8_t index_html_start[] asm("_binary_index_html_start");
extern const uint8_t index_html_end[]   asm("_binary_index_html_end");

httpd_handle_t WEB_server = NULL;

// Send index.html Page
esp_err_t index_html_handler(httpd_req_t *req)
{
	httpd_resp_set_type(req, "text/html");

	httpd_resp_send(req, (const char *)index_html_start, index_html_end - index_html_start);

	return ESP_OK;
}
//
httpd_uri_t index_html = {
	.uri = "/",
	.method = HTTP_GET,
	.handler = index_html_handler,
	/* Let's pass response string in user
	 * context to demonstrate it's usage */
	.user_ctx = NULL
 };
//
//
httpd_handle_t start_webserver(void)
{
	httpd_config_t config = HTTPD_DEFAULT_CONFIG();
	
	// Start the httpd server
	ESP_LOGI("OTA", "Starting server on port: '%d'", config.server_port);
	if (httpd_start(&WEB_server, &config) == ESP_OK) 
	{
		// Set URI handlers
		ESP_LOGI("OTA", "Registering URI handlers");
		httpd_register_uri_handler(WEB_server, &index_html);
		
		return WEB_server;
	}

	ESP_LOGI("OTA", "Error starting server!");
	return NULL;
}
//
//
void app_main()
{
    printf("Hello world!\n");

     start_webserver();
}

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

Postby mikemoy » Sat Jul 20, 2019 7:48 am

Hello? Espressif care to comment on why this is ?

ESP_Sprite
Posts: 9040
Joined: Thu Nov 26, 2015 4:08 am

Re: COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

Postby ESP_Sprite » Mon Jul 22, 2019 3:40 am

Interesting - it's almost as cmake defines an index_html symbol when including the binary. Can you see what happens if you rename the index_html variable in your code to something else?

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

Postby mikemoy » Mon Jul 22, 2019 8:05 am

Interesting - it's almost as cmake defines an index_html symbol when including the binary. Can you see what happens if you rename the index_html variable in your code to something else?

Thank you so much ESP_Sprite! I renamed index.html to main_page and it compiled just fine.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

Postby ESP_Angus » Wed Jul 24, 2019 3:50 am

Hi mike,

Sorry, this is a feature that was added in the CMake build system but not documented. Will document ASAP.

In GNU Make build system, we use "objcopy" to embed data and are limited by the way that objcopy generates the _binary_filename_start & _binary_filename_end symbols.

In CMake, we have a little less control over pathnames at build time (which influences the possible values objcopy will name the symbol) so the build system adopted a new approach. This meant we could also choose new names for the symbols which are used when embedding files.

_binary_filename_start & _binary_filename_end are still there (for compatibility with GNU Make) but there are two new symbol names: "filename" and "filename_length". "filename" is the same address as _binary_filename_start, and "filename_length" is the length in bytes not including any terminating NUL byte if the data was included as a string.

The "filename" in both cases is the filename with any non-C-allowed identifiers converted to underscores. This is how there is a global symbol named "index_html".

Somehow this was not documented in the IDF Programming Guide or the list of differences between GNU Make and CMake, sorry about this. Will add this documentation ASAP.

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: COMPONENT_EMBED_FILES & Cmake esp-idf-v3.2.2

Postby mikemoy » Wed Jul 24, 2019 9:35 am

I should probably wait for the docs. But concerning "filename_length", how can we know what the length is ?

Who is online

Users browsing this forum: Bing [Bot] and 124 guests