0

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.
2
  • 2
    The information you provide in the question post - fragments of CMake code and vague description of the error message - is not sufficient to locate the core of your problem. Please, provide minimal reproducible example: the code (CMake and C) which reproduces your problem and the exact error message. Commented Feb 27 at 10:32
  • I was able to prove that the issue is circular dependencies without doing that. In this case it was possible to avoid the dependency - if I had really needed it I would have been digging further in the ESP_IDF documentation which does have some other possible workarounds for this. Really what is needed is to create a linker group so that the order the dependencies are declared is not a problem - but I have no idea how to make CMake / ESP-IDF do that. (If anyone knows how to do that, it might be very useful for the future.) Thanks anyway. Commented Feb 27 at 11:29

1 Answer 1

0

OK, I have found a workaround for my issue.

As my libraries only need to see the .h file of the top level project "main", there was no need in fact to make these modules REQUIRE or PRIVE_REQUIRE main.

All that was needed was to make sure that the path to common.h was in their include paths.

This does prove that the linking issue was caused by the circular dependency between the parts of the project.

I am not sure what the solution might have been had my library functions needed to actually use functions or variables in the top level main code. In my case this was not needed and I was able to avoid the circular dependency.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.