Undefined references to WiFi classes at link-time

olney_one
Posts: 5
Joined: Mon Sep 16, 2024 9:28 pm

Undefined references to WiFi classes at link-time

Postby olney_one » Tue Oct 01, 2024 11:06 am

I am building an embedded swift application with interoperability to Arduino ESP32 libraries. I am fairly sure this problem relates to how I am building the library and not the swift bit! It runs with a (heavily) modified CMakeLists.txt, so it may be that I have messed something up here.

If I use `WiFi.begin(said, password)` then I end up with:
```
undefined reference to `WiFiSTAClass::begin(char const*, char const*, int, unsigned char const*, bool)'
```
I get the equivalent error trying `WiFi.scanNetworks(...)`. Reading dozens of threads seems to point to it using the generic version of WiFi rather than the specific one. Reading through `WiFi.h` and dependent headers/source, it seemed likely that SOC_WIFI_SUPPORTED needed to be defined, but this does no good.

This is my CMakeLists.txt:
```
idf_component_register(
SRCS /dev/null
INCLUDE_DIRS "../../include" "." "/Library/Developer/CommandLineTools/usr/include"
LDFRAGMENTS "linker.lf"
REQUIRES arduino nvs_flash esp_system
)

idf_build_get_property(target IDF_TARGET)
idf_build_get_property(arch IDF_TARGET_ARCH)

if("${arch}" STREQUAL "xtensa")
message(FATAL_ERROR "Not supported target: ${target}")
endif()

if(${target} STREQUAL "esp32c2" OR ${target} STREQUAL "esp32c3")
set(march_flag "rv32imc_zicsr_zifencei")
set(mabi_flag "ilp32")
elseif(${target} STREQUAL "esp32p4")
set(march_flag "rv32imafc_zicsr_zifencei")
set(mabi_flag "ilp32f")
else()
set(march_flag "rv32imac_zicsr_zifencei")
set(mabi_flag "ilp32")
endif()

# Clear the default COMPILE_OPTIONS which include a lot of C/C++ specific compiler flags that the Swift compiler will not accept
get_target_property(var ${COMPONENT_LIB} COMPILE_OPTIONS)
set_target_properties(${COMPONENT_LIB} PROPERTIES COMPILE_OPTIONS "")

# Compute -Xcc flags to set up the C and C++ header search paths for Swift (for bridging header).
set(SWIFT_INCLUDES)
foreach(dir ${CMAKE_C_IMPLICIT_INCLUDE_DIRECTORIES})
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ")
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
endforeach()
foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-Xcc ")
string(CONCAT SWIFT_INCLUDES ${SWIFT_INCLUDES} "-I${dir} ")
endforeach()

# Swift compiler flags to build in Embedded Swift mode, optimize for size, choose the right ISA, ABI, etc.
target_compile_options(${COMPONENT_LIB} PUBLIC "$<$<COMPILE_LANGUAGE:Swift>:SHELL:
-target riscv32-none-none-eabi
-Xfrontend -function-sections -enable-experimental-feature Embedded -wmo -parse-as-library -Osize
-Xcc -march=${march_flag} -Xcc -mabi=${mabi_flag} -Xcc -std=gnu++11
-cxx-interoperability-mode=default
-Xcc -fno-exceptions
-Xcc -fno-rtti

-pch-output-dir /tmp
-Xfrontend -enable-single-module-llvm-emission

${SWIFT_INCLUDES}

-import-bridging-header ${CMAKE_CURRENT_LIST_DIR}/BridgingHeader.h
>")

# Enable Swift support in CMake, force Whole Module builds (required by Embedded Swift), and use "CMAKE_Swift_COMPILER_WORKS" to
# skip the trial compilations which don't (yet) correctly work when cross-compiling.
set(CMAKE_Swift_COMPILER_WORKS YES)
set(CMAKE_Swift_COMPILATION_MODE_DEFAULT wholemodule)
set(CMAKE_Swift_COMPILATION_MODE wholemodule)
enable_language(Swift)

file(GLOB SWIFT_SRCS ${CMAKE_CURRENT_LIST_DIR}/src/*.swift)

# List of Swift source files to build.
target_sources(${COMPONENT_LIB}
PRIVATE
${SWIFT_SRCS}
)

get_target_property(result ${COMPONENT_TARGET} COMPILE_OPTIONS)
include(CMakePrintHelpers)
cmake_print_variables(result)
```
I have tried putting arduino-esp32 as the library and various combinations of esp_cpu, esp_system, etc. but nothing helps.

greycon
Posts: 33
Joined: Fri Nov 03, 2023 9:59 pm

Re: Undefined references to WiFi classes at link-time

Postby greycon » Tue Oct 01, 2024 9:19 pm

Hi, not a swift guy, but I note you refer to "WiFi.begin(said, password)" - is that in your Swift code? I thought that Swift worked with C as opposed to C++. Or can Swift do both?

olney_one
Posts: 5
Joined: Mon Sep 16, 2024 9:28 pm

Re: Undefined references to WiFi classes at link-time

Postby olney_one » Wed Oct 02, 2024 6:06 am

Hi. It can do both.

Who is online

Users browsing this forum: No registered users and 62 guests