Page 1 of 1

Native CMake library not being linked

Posted: Wed Oct 11, 2023 12:03 pm
by RobMeades
I have a native [.cpp] CMake library, which is already successfully built and used under native CMake for Linux and Windows in the usual way (i.e. add_library(), target_include_directories() and then a call to target_link_libraries() following add_executable()) which I would like to use within the ESP-IDF world.

From the instructions here:

https://docs.espressif.com/projects/esp ... components

...I believe that, provided that the library (named geodesic) is created with:

Code: Select all

add_library(geodesic STATIC ${GEODESIC_SRC})
target_include_directories(geodesic PRIVATE ${GEODESIC_INC})
...[which it is] then it should be linked with my application if I include geodesic in my list of required components, so:

Code: Select all

COMPONENT_REQUIRES(... geodesic)
I can see that ESP-IDF is compiling all of the files that comprise geodesic and that a library of name libgeodesic.a is being created. If I print COMPONENT_REQUIRES from within CMakeLists.txt, geodesic is definitely in the list:

Code: Select all

COMPONENT_REQUIRES is driver;esp_timer;mbedtls;esp_system;geodesic
However, linking the final target fails because the functions that my application calls which are inside the geodesic library aren't present, e.g.:

Code: Select all

[653/658] Linking CXX executable final.elf
FAILED: final.elf
cmd.exe /C "cd . && C:\Users\rmea\.ubxlibpkg\esp_idf_tools-v5.0.3\tools\xtensa-esp32-elf\esp-2022r1-11.2.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address  @CMakeFiles\final.elf.rsp -o final.elf  && cd ."
c:/users/rmea/.ubxlibpkg/esp_idf_tools-v5.0.3/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/final/libmain.a(geodesic_user.cpp.obj):(.literal.uGnssFenceWgs84GeodDirect+0x0): undefined reference to `GeographicLib::Geodesic::WGS84()'
c:/users/rmea/.ubxlibpkg/esp_idf_tools-v5.0.3/tools/xtensa-esp32-elf/esp-2022r1-11.2.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/11.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/final/libmain.a(geodesic_user.cpp.obj):(.literal.uGnssFenceWgs84GeodDirect+0x4): undefined reference to `GeographicLib::Geodesic::GenDirect(double, double, double, bool, double, unsigned int, double&, double&, double&, double&, double&, double&, double&, double&) const'
...
What might I be doing wrong?

I've tried making the target_include_directories() of type PUBLIC, in case that is upsetting C++ somehow (all of the other files built into the application are only .c code), but that doesn't help. I don't believe there is anything wrong with the code or header files themselves since, with exactly the same code and CMake files, GCC under Linux and MSVC under Windows build (and run) to completion.

I've also tried adding a plain-old .c file to the geodesic library and calling a function in that from my main code: that function also seems to be unavailable, so this is not just a C++ thing, the entire libgeodesic.a is not being given to the link stage for some reason.

Re: Native CMake library not being linked

Posted: Wed Oct 11, 2023 2:23 pm
by bidrohini
The same error message has been discussed here. You can take a look for clues.
https://esp32.com/viewtopic.php?t=33058

Re: Native CMake library not being linked

Posted: Wed Oct 11, 2023 2:43 pm
by RobMeades
Hi, and thanks for that. Checking, in that case it was something to do with "how many HID devices should show up", a setting in menuconfig, which unfortunately doesn't apply in my case; my code is pure C/C++, no HW involved.