Using C++ library in esp-idf

DylanWork
Posts: 32
Joined: Wed Jun 02, 2021 4:27 am

Using C++ library in esp-idf

Postby DylanWork » Mon Jun 28, 2021 11:29 pm

Hi all,

I have found a LoRaWAN library that works in the esp-idf and I am trying to integrate it into the ulp-riscv example, however I am having trouble figuring out how to handle the c++ components of the library.

I have seen other posts trying to get C++ to work like this:
https://www.esp32.com/viewtopic.php?f=1 ... a51990bc02

There is a few examples of how to use C in C++, but not the other way around.

The error I'm getting is:

Code: Select all

P_PLATFORM -MD -MT esp-idf/main/CMakeFiles/__idf_main.dir/ulp_riscv_example_main.c.obj -MF esp-idf\main\CMakeFiles\__idf_main.dir\ulp_riscv_example_main.c.obj.d -o esp-idf/main/CMakeFiles/__idf_main.dir/ulp_riscv_example_main.c.obj   -c ../main/ulp_riscv_example_main.c
In file included from ../main/ulp_riscv_example_main.c:22:
../main/include/TheThingsNetwork.h:139:5: error: unknown type name 'TTNSpreadingFactor'
     TTNSpreadingFactor spreadingFactor;
     ^~~~~~~~~~~~~~~~~~
../main/include/TheThingsNetwork.h:143:5: error: unknown type name 'TTNBandwidth'
     TTNBandwidth bandwidth;
     ^~~~~~~~~~~~
../main/include/TheThingsNetwork.h:168:1: error: unknown type name 'class'
 class TheThingsNetwork
 ^~~~~
../main/include/TheThingsNetwork.h:169:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 {
 ^
ninja: build stopped: subcommand failed.
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command cmake --build ." terminated with exit code: 1.
I have tried using some extern "C" as shown here:
https://isocpp.org/wiki/faq/mixing-c-and-cpp

But yet to have much success, I am likely just approaching it wrong...

Has anyone had success with a similar problem?

Cheers

davidzuhn
Posts: 17
Joined: Fri Sep 20, 2019 1:50 am

Re: Using C++ library in esp-idf

Postby davidzuhn » Tue Jun 29, 2021 12:28 am

You can't include C++ header files into a C program -- your C compiler does not know how to parse C++.

You can call C++ functions (which have C linkage via 'extern "C"') from C, although you probably have to create your own header files (or provide suitable declarations for the functions) unless the library was designed to be used from C, which is possible but unlikely.

You can't create C++ objects in C code, nor call object methods. For that, you need to write your code in C++. (Okay, there are some terrible hacks to do these things from C, but don't do it. It's not worth it. Just write your main code in C++ as well, and then it's easy to use a C++ library.

I haven't done so for the ULP, but for the main program this is easy and straightforward. Just declare app_main with extern "C", and rename your main source file to something .cpp (and make the appropriate CMake/Makefile changes). Most C code compiles right out of the box as C++, although some structure initialization patterns that are used in the IDF require a bit of munging to work properly.

Who is online

Users browsing this forum: No registered users and 384 guests