Including C++ Precompiled Library in ESP-IDF C Project

eagi223
Posts: 16
Joined: Fri Mar 02, 2018 4:46 pm

Including C++ Precompiled Library in ESP-IDF C Project

Postby eagi223 » Tue Apr 30, 2019 8:44 pm

I've recently tried to incorporate some C++ precompiled libraries into my IDF project, but haven't been able to get them working correctly. I've scoured this forum and StackOverflow, and seen this question asked before, but never really answered well.

My specific case is that I would like to use the C++ Protobuffer Lib. This is installed locally on my system, but I could also copy the .a file into my project if that made it easier. In the long run, having the library inside the project would probably be better anyway since it wouldn't require other developers on my team to have Protobuffers installed. I thought I would be able to do the following:
1)Write a C++ wrapper that interacted with the library through pure function calls (no public C++ specific features).
2)Link the Library I wanted in the main component.mk file using:

Code: Select all

CXXFLAGS += -I/usr/local/include 

Code: Select all

 COMPONENT_ADD_LDFLAGS += -lthislibraryiwant
3)Have my C code call the functions from the C++ wrapper when they need to interact with that library (the wrapper keeps all the C++ separated from the C).

This didn't work at all and I'm sure that it is just me not knowing the correct way to go about it. Adding the CXXFLAGS made it so that headers were being found, but I was getting linking issues with errors saying "Undefined reference to function_name" about a thousand times...

Does anyone have a good example of how you would use a C++ library with C code in the IDF including how to get the component.mk file correct for it?

Thanks in advance!
Ethan

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Including C++ Precompiled Library in ESP-IDF C Project

Postby ESP_Sprite » Thu May 02, 2019 2:43 am

Do I understand it correctly and are you trying to use a protobuf library that is distributed with your (presumably Linux) system? If so, that is most likely not going to work, as the ESP32 has an Xtensa architecture, while your host machine most likely does not and precompiled libraries are architecture-specific. Note that ESP-IDF comes with its own protobuf component nowadays, is there a reason you cannot use that?

eagi223
Posts: 16
Joined: Fri Mar 02, 2018 4:46 pm

Re: Including C++ Precompiled Library in ESP-IDF C Project

Postby eagi223 » Thu May 02, 2019 4:20 pm

Yeah, that was dumb of me, sorry about that. I was following the Protobuffer C++ documentation thinking it would be more or less the same since I'm so used to using C for embedded firmware, but I guess their docs are geared towards using the library for x86/64. I'll try compiling the Protobuffer C++ source as a component again (though I never got that to work either when I tried before).

I'm using the esp included protobuf library currently, but the documnetation for protobuf-c seems pretty lacking... They've got "TODOs" from 2014, large chunks of missing or incomplete documentation and the last commit was nearly a year ago. It seems like the repo is dying honestly, but maybe now that it is included with the IDF it will pick back up.

The main Google repo for protobuf (that has C++ instead of C codegen) is way more active and much better documented, though not geared towards embedded systems. It also looks like protobuf-c is lacking some of the nice utilities that the full protobuf has like deserializing directly to json. Also, there appears to be a bug in protobuf-c's code generation where it will create macros for initializing protobuf messages that contain {0} initializations for structs which causing an error and fails to compile. Ex:

Code: Select all

#define ACCELERATION__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&acceleration__descriptor) \
    , ACCELERATION__DATA__NOT_SET, {0} }
This means any time I compile the .proto file, I've got to go back into the generated .pb-c.c file and change anything that has {0} to {{0}} which can be frustrating, but I'm on a pretty tight timeframe so I'll just have to live with protobuf-c for now.

Who is online

Users browsing this forum: Bing [Bot], VinayDand and 425 guests