How to compile mbedtls myself instead of using the offical downloaded mbedtls?

I add some functions in the mbedtls and try to compile it to static library, but the gcc always uses the offical mbedtls library in the .platformio directory, as shown below(libsmbedtls is the library I want to use):

xtensa-esp32-elf-g++ -o .pio\build\lolin32\firmware.elf -mlongcalls -Wno-frame-address -Wl,--cref -Wl,--gc-sections -fno-rtti -fno-lto -Wl,--wrap=mbedtls_mpi_exp_mod -Wl,--wrap=longjmp -Wl,--undefined=uxTopUsedPriority -T esp32.rom.redefined.ld -T memory.ld -T sections.ld -T esp32.rom.ld -T esp32.rom.api.ld -T esp32.rom.libgcc.ld -T esp32.rom.newlib-data.ld -T esp32.rom.syscalls.ld -T esp32.peripherals.ld -u ld_include_hli_vectors_bt -u _Z5setupv -u _Z4loopv -u esp_app_desc -u pthread_include_pthread_impl -u pthread_include_pthread_cond_impl -u pthread_include_pthread_local_storage_impl -u ld_include_highint_hdl -u start_app -u start_app_other_cores -u __ubsan_include -u __assert_func -u vfs_include_syscalls_impl -u app_main -u newlib_include_heap_impl -u newlib_include_syscalls_impl -u newlib_include_pthread_impl -u newlib_include_assert_impl -u __cxa_guard_dummy -Wl,-Map="D:\Projectdir\.pio\build\lolin32\" .pio\build\lolin32\src\main.cpp.o -L.pio\build\lolin32 -LC:\Users\xxx\.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\lib -LC:\Users\xxx\.platformio\packages\framework-arduinoespressif32\tools\sdk\esp32\ld -Wl,--start-group .pio\build\lolin32\lib591\libBluetoothSerial.a .pio\build\lolin32\libe30\libsmbedtls.a .pio\build\lolin32\libbe2\libWiFi.a .pio\build\lolin32\libFrameworkArduinoVariant.a .pio\build\lolin32\libFrameworkArduino.a -lesp_ringbuf -lefuse -lesp_ipc -ldriver -lesp_pm -lmbedtls(<---HERE) ......

And then ld will return with error:

/Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/mbedtls/mbedtls/library/md5.c:444: multiple definition of `mbedtls_md5'; .pio\build\lolin32\libe30\libsmbedtls.a(md5.c.o):D:\Projectdir/lib/smbedtls/src/md5.c:320: first defined here

It seems that my own mbedtls library has the same functions and the confict happened. But I don’t know if mbedtls is any other libraries’ dependency. So I want to know if there is any simple solution to this condition. Thanks!

file structure:

|  |
|  |--smbedtls
|  |  |--src
|  |     |- md5.c
|  |     |--mbedtls
|  |        |- md5.h
|  |        |- ...
|  |
|  |- README
|- platformio.ini
   |- main.c

The Arduino-ESP32 builder script adds a predetermined sets of libraries to the build

So you are conflicting with arduino-esp32/libmbedtls.a at master · espressif/arduino-esp32 · GitHub and possibly arduino-esp32/libmbedx509.a at master · espressif/arduino-esp32 · GitHub.

You can manipulate env["LIBS"] using Advanced scripting. That is, you should try and add to the platformio.ini a extra_scripts = with a along the lines of

# no "-lmbedtls"
new_libs = [x for x in env["LIBS"] if x != "-lmbedtls"]
env["LIBS"] = new_libs

Thanks! I will try it

OK… The script can remove the mbedtls, but some other libraries (e.g., wpa_supplicant) also needs it. So I will try to rename all the functions in my own lib, or add a namespace to prevent this condition.

If you want to go “the super correct route”, you would use GitHub - Jason2866/esp32-arduino-lib-builder: Tasmota esp32-arduino-lib-builder to rebuild the ESP-IDF library files used in Arduino-ESP32, but tell the tool to build from your special ESP-IDF branch that has mbedtls (esp-idf/components/mbedtls at master · espressif/esp-idf · GitHub) modified like you want. Then you just have an updated version of libmbedtls.a that you use in your Arduino-ESP32 installation. But that requires a lot of work too and changes you to do to the lib might break other parts / users of that lib which depend on the behavior of a particular function.

So yeah, namespace scoping might be an alternative to that.