How to fix arduino-esp32 error: 'spi_host_device_t' has not been declared

Problem

While trying to compile your arduino-esp32 project, you get an error message like this:

In file included from /home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp:24:
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:161:70: error: 'spi_host_device_t' has not been declared
  161 |     eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, spi_host_device_t spi_host, int sck = -1, int miso = -1, int mosi = -1,
      |                                                                      ^~~~~~~~~~~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:246:34: error: 'spi_host_device_t' has not been declared
  246 |     int sck, int miso, int mosi, spi_host_device_t spi_host, uint8_t spi_freq_mhz
      |                                  ^~~~~~~~~~~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp:516:6: error: no declaration matches 'bool ETHClass::beginSPI(eth_phy_type_t, int32_t, uint8_t*, int, int, int, SPIClass*, int, int, int, spi_host_device_t, uint8_t)'
  516 | bool ETHClass::beginSPI(
      |      ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:241:8: note: candidate is: 'bool ETHClass::beginSPI(eth_phy_type_t, int32_t, uint8_t*, int, int, int, SPIClass*, int, int, int, int, uint8_t)'
  241 |   bool beginSPI(
      |        ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:149:7: note: 'class ETHClass' defined here
  149 | class ETHClass : public NetworkInterface {
      |       ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp:867:6: error: no declaration matches 'bool ETHClass::begin(eth_phy_type_t, int32_t, int, int, int, spi_host_device_t, int, int, int, uint8_t)'
  867 | bool ETHClass::begin(
      |      ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:165:8: note: candidates are: 'bool ETHClass::begin()'
  165 |   bool begin() {
      |        ^~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:160:8: note:                 'bool ETHClass::begin(eth_phy_type_t, int32_t, int, int, int, int, int, int, int, uint8_t)'
  160 |   bool begin(
      |        ^~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp:861:6: note:                 'bool ETHClass::begin(eth_phy_type_t, int32_t, int, int, int, SPIClass&, uint8_t)'
  861 | bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int cs, int irq, int rst, SPIClass &spi, uint8_t spi_freq_mhz) {
      |      ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp:191:6: note:                 'bool ETHClass::begin(eth_phy_type_t, int32_t, int, int, int, eth_clock_mode_t)'
  191 | bool ETHClass::begin(eth_phy_type_t type, int32_t phy_addr, int mdc, int mdio, int power, eth_clock_mode_t clock_mode) {
      |      ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:149:7: note: 'class ETHClass' defined here
  149 | class ETHClass : public NetworkInterface {
      |       ^~~~~~~~
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp: In member function 'void ETHClass::end()':
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.cpp:886:22: warning: 'void NetworkEvents::removeEvent(NetworkEventFuncCb, arduino_event_id_t)' is deprecated: removing functional callbacks via pointer is deprecated, use removeEvent(network_event_handle_t) instead [-Wdeprecated-declarations]
  886 |   Network.removeEvent(onEthConnected, ARDUINO_EVENT_ETH_CONNECTED);
      |   ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Network/src/Network.h:9,
                 from /home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Ethernet/src/ETH.h:70:
/home/user/my-project/managed_components/espressif__arduino-esp32/libraries/Network/src/NetworkEvents.h:204:8: note: declared here
  204 |   void removeEvent(NetworkEventFuncCb cbEvent, arduino_event_id_t event = ARDUINO_EVENT_MAX)
      |        ^~~~~~~~~~~
make[2]: *** [esp-idf/espressif__arduino-esp32/CMakeFiles/__idf_espressif__arduino-esp32.dir/build.make:1434: esp-idf/espressif__arduino-esp32/CMakeFiles/__idf_espressif__

Solution

This error occurs because you have disabled SPI-Ethernet in the sdkconfig, but even though you are not using SPI-Ethernet in your project, the arduino-esp32 library still tries to include it.

To fix this, you need to enable SPI-Ethernet in your sdkconfig file.

CONFIG_ETH_USE_SPI_ETHERNET=y

After that, recompile and the error will be gone.