I have a project under ESP32 using the ESP-IDF framework, version 5.4
My project has several submodules which are built as static libraries and linked. Until now, things have been working very well.
Now I tried to have all libraries include a single header file which is in the top level project, like this:
-- main/inc/common.h
-- components/MyModule/inc/my_module.h
and my_module.h includes common.h
This requires that I make main a required module for MyModule, so I update its CMakeLists.txt like this:
idf_component_register(
SRCS
INCLUDE_DIRS
inc
path/to/common.h
REQUIRES
main
SomeOtherLib
)
or
idf_component_register(
SRCS
INCLUDE_DIRS
inc
path/to/common.h
REQUIRES
SomeOtherLib
PRIV_REQUIRES
main
)
but regardless of which way I do this, I start getting odd linking errors. The linker starts complaining that some totally unrelated function cannot be resolved - even though the lib it belongs in HAS compiled and I can even verify that the function it wants is in the .a file by using nm.
What is going on? How can I fix this?
EDIT : the specific error message that I was getting was "undefined reference" at the linking stage. The actual function that was being linked to actually IS in the library file libMyModule.a - so it seems that the linker was being confused or broken by the order of dependencies. I was able to prove this as discussed below, in this case I was able to avoid the circular dependency (which I actually introduced by following a suggestion given by ESP-IDF as below:
Compilation failed because my_module.h (in "MyModule" component) includes common.h, provided by main component(s).
However, main component(s) is not in the requirements list of "MyModule".
To fix this, add main to REQUIRES list of idf_component_register call in /home/user/ ... /components/my_module/CMakeLists.txt.