In your post you named your method in the header file like this
but in your setup()-Method you’re calling
That ends in a compiler (not linker ) error like that
src/main.cpp: In function 'void setup()':
src/main.cpp:7:23: error: 'test_static' was not declared in this scope
int number_test = test_static(30);
^~~~~~~~~~~
src/main.cpp:7:23: note: suggested alternative: 'setstate'
int number_test = test_static(30);
^~~~~~~~~~~
setstate
*** [.pio/build/esp32dev/src/main.cpp.o] Error 1
I assume that this is a typo in your posting. After setting both names to the same, I got your error message.
To fix that you have to add
#include "libtest.h"
to your libtest.cpp file too. Without that the compiler generates two different symbols for your method
nm .pio/build/esp32dev/src/main.cpp.o
U test_static
U yield
00000000 T _Z4loopv
00000000 T _Z5setupv
vs
nm .pio/build/esp32dev/lib525/libmylib.a
libtest.cpp.o:
00000000 T _Z11test_statici
that do not match. After adding the include nm shows that
nm .pio/build/esp32dev/lib525/libmylib.a
libtest.cpp.o:
00000000 T test_static
and linking succeeds.
And then there is no need for additional build_flags - platformio manages your private lib without that
Oh, My translator missed the problem. In libtest.h, it is written as test_static(int i); instead of int test(int i);.
However, I’m still getting the /src/main.cpp:12: undefined reference to 'test_static' error.
Also, I can check the results with the nm .pio/build/esp32dev/src/main.cpp.o command, but in the command nm .pio/build/esp32dev/lib525/libmylib.a, the lib525 folder does not exist in my build folder.
I was able to find similar results in the following path.
#include "testlib.h"
int func_from_lib(void) {
return 4;
}
Now, instead of system clang++ (which I assume you are on an Apple Silicon, me too) which is an aarch64 architecture compiler, esp32 is an Xtensa architecture chip, so aarch64 libraries won’t link to that. You need to:
pio pkg exec -p toolchain-xtensa-esp32 -- xtensa-esp32-elf-g++ libtest.cpp -Iinclude -c
pio pkg exec -p toolchain-xtensa-esp32 -- xtensa-esp32-elf-ar rs libtest.a libtest.o
This instructs PIO CLI to find the correct toolchain for esp32, then compile using compilers from there. Replace -Iinclude with path to your project include library, and also take a look at $HOME/.platformio/packages for the correct package name if anything goes wrong.
So, I compiled it in a Windows environment, inserted it into the project, and verified with the nm command that it was included correctly. However, the error still persists.
(In the image below, I replaced the actual function name with test_static to aid understanding through image modification.)
Can you confirm that your libtest.cpp has a #include "libtest.h" directive? Name mangling happens only if the source file doesn’t recognize that a symbol is declared in extern "C" block
The numbering for dependencies libs inside .pio/build varies (don’t known how pio exactly decides which number is used. As I deleted my small reproducer in the meantime since my last post and had to reconstruct it and now the private lib resides in lib4bd/ ).