I've started a project based on the ESP-IDF example 'blink' and am trying to reorganize it into a more structured format. My goal is to have a project structure like this:
BLINK/
├── components/
│ ├── arduino/
│ ├── lib1/
│ │ ├── include/
│ │ │ └── foo.h
│ │ ├── src/
│ │ │ └── foo.c
│ │ └── CMakeLists.txt
│ └── ...
├── main/
│ └── main.c
├── CMakeLists.txt
├── sdkconfig
└── README.md
I modified main.c in the main folder as follows:
#include <stdio.h>
#include "sdkconfig.h"
#include "../components/lib1/include/foo.h"
void app_main() {
printfoo();
while (1) {
}
}
Here are the contents of the files in my lib1 component:
foo.h:#include <stdio.h> void printfoo();foo.c:#include "foo.h" void printfoo() { printf("Hello from Foo\r\n"); }CMakeLists.txtinBLINK/components/lib1/:set(COMPONENT_SRCS "foo.c") set(COMPONENT_ADD_INCLUDEDIRS "." "include") register_component()
When I compile the project, I get the following error:
main.c:6: undefined reference to `printfoo'
I know that this error output means that linker couldn't find definition of the function printfoo which located in foo.c
Any suggestions on what might be causing this error and how to resolve it?
update: I am using ESP-IDF version v3.3.6.
I have followed the ESP32 guide on Component CMakeLists Files but am still encountering this issue.
General
I think the problem is that you set you're source to
foo.c, but the that file is atsrc/foo.c. Thus, the source file isn't added and as a consequence, the function isn't implemented.ESP-IDF v3.3.6
In ESP-IDF v3.3.6, the following CMakeLists.txt should work:
More information/documentatio v3.3.6
ESP-IDF v5.x
In version v5.x,
SRCSandINCLUDE_DIRSare commonly used instead ofCOMPONENT_SRCSandCOMPONENT_ADD_INCLUDEDIRSrespectively. In ESP-IDF v5.x, the following CMakeLists.txt should work:More information/documentatio v5.x
General
Then, in main, you should be able to include
foo.hin the following way: