Amir Gonnen Amir Gonnen - 8 months ago 53
C Question

Finding the root cause of `undefined reference` error

I'm trying to understand why I'm getting an

undefined reference
error during linking:

/home/amirgon/projects/esp8266/esp-open-sdk/xtensa-lx106-elf/bin/xtensa-lx106-elf-gcc -L/home/amirgon/projects/esp8266/esp-open-sdk/sdk/lib -T/home/amirgon/projects/esp8266/esp-open-sdk/sdk/ld/ -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static -Wl,--start-group -lc -lgcc -lhal -lphy -lpp -lnet80211 -llwip -lwpa -lmain build/app_app.a -Wl,--end-group -o build/app.out
build/app_app.a(routines.o):(.text+0x4): undefined reference to `pvPortMalloc(unsigned int, char const*, int)'

gcc complains it could not find the function

However, I can confirm this function exists in

In the command line above, libmain is referenced by
and library path is set to
When I dump symbols from
on that path I can find
marked as
, which means that the symbol is in the text (code) section:

/home/amirgon/projects/esp8266/esp-open-sdk/xtensa-lx106-elf/bin/xtensa-lx106-elf-nm -g /home/amirgon/projects/esp8266/esp-open-sdk/sdk/lib/libmain.a | grep pvPortMalloc
U pvPortMalloc
0000014c T pvPortMalloc
U pvPortMalloc

So, did I miss something?
what could be the reason that gcc does not find the function although it exists in libmain.a?

How can I further debug this error?

nos nos

Mixing of C++ and C code causes your issue.

This error:

undefined reference to `pvPortMalloc(unsigned int, char const*, int)'

Does not say that the symbol pvPortMalloc cannot be found. It says that the symbol pvPortMalloc(unsigned int, char const*, int) cannot be found, and that is a C++ symbol.

This means that somewhere you are compiling C++ code which thinks there is a C++ pvPortMalloc function, whose symbol also includes its signature, but you only have a pvPortMalloc C function.

Likely your C++ code is including a header file that is not C++ clean, and you will need to do something like this:

extern "C" {
#include "some_header.h"

Where some_header.h is the header file declaring the pvPortMalloc function.