Home > Net >  How to add an outside library to an ESP-IDF Project
How to add an outside library to an ESP-IDF Project

Time:06-12

I've been trying to fix this for a couple days so any insight would be greatly appreciated. I am building a project with an ESP32 board and VSCode's esp-idf framework. I am having trouble getting access to an outside library's functions. For example, I have implemented an FFT-noise-filter program in c, and now i want to bring it into the esp-idf framework. I think it has something to do with my unfamiliarity with CMake, and I have tried all sorts of different "CMakeLists.txt", but not sure what it should look like. I've been through cmake tutorials, but I just can't figure it out. Here's my current 'CMakeLists' inside main folder

idf_component_register(SRCS "hello_world_main.c"
                    INCLUDE_DIRS ".")

I took an example project 'hello_world' from esp-idf's examples, and wrote my own code inside of the 'hello_world_main.c'. It is weird because in my "hello_world_main.c" the complier seems to know some data types such as 'FFTW_Complex', which are only found in the library I'm trying to use. However, when I call any functions like FFTW's 'malloc' from that same library, I get an error "undefined reference to fftw_malloc()"

excerpt from hello_world_main.c's 'app_main():

//complex: double[2] = {real_part,imag_part} 
fftw_complex *in, *out;  //no errors here for some reason
fftw_plan p;

//initialize the arrays-> "in" is an array of fftw_complex type (basically a pair of doubles)
//in is f (set of points we know) -> out is fhat (complex fourier coefficents) with magnitude and phase
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); //'undefined reference to fftw_malloc'
Error message:

[5/7] Linking CXX executable hello_world_2.elf FAILED: hello_world_2.elf cmd.exe /C "cd . && C:\Users\bgreenwood.espressif\tools\xtensa-esp32-elf\esp-2021r2-patch3-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g .exe -mlongcalls -Wno-frame-address @CMakeFiles\hello_world_2.elf.rsp -o hello_world_2.elf && cd ." c:/users/bgreenwood/.espressif/tools/xtensa-esp32-elf/esp-2021r2-patch3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/main/libmain.a(hello_world_main.c.obj):(.literal.app_main 0x1c): undefined reference to `fftw_malloc'

so my question is, how can I get my main to recognize the function calls I am making?

CodePudding user response:

Add your libcode.c to:

idf_component_register(SRCS "hello_world_main.c" “libcode.c”
INCLUDE_DIRS ".")

And add a reference to libcode.h:

#include “libcode.h”

Libcode is an artificial name, change it by the correct one and you could also add a directory if needed like “libcode/libcode.h”

Hope this answers your question; with more code it’s easier to understand your problem.

CodePudding user response:

To check what the minimal component would look like, you can create one using idf.py create-component NAME. The component named NAME will be made, and you can check what is missing in your external library.

  • Related