-->
Page 1 of 1

Use of extern "C" {

PostPosted: Sun Jan 31, 2016 8:22 pm
by rocket_man
When using some of the ESP8266 Arduino libraries, one needs to include them with extern "C" if they are C libraries. For example to include ip_addr.h, one needs to do it like this or it will not compile:

Code: Select allextern "C"{
#include "ip_addr.h"
}


For some of the other libraries, this is not needed (e.g., os_type.h).

My question is: how can one know if a library is a C library or a C++ library so that one knows to place it either inside the extern "C" block or outside? Thanks!

Re: Use of extern "C" {

PostPosted: Mon Feb 01, 2016 10:05 am
by martinayotte
Arduino is in C++, while the Espressif SDK is in plain C.
So, the general rule is that any SDK includes need the "extern C".

BTW, why do you need to include the SDK "ip_addr.h" ?
Is there something you need that Arduino "ESP8266WiFi.h" and "IPAddress.h" don't provide ?

Re: Use of extern "C" {

PostPosted: Tue Feb 02, 2016 12:27 pm
by rocket_man
Thanks for the information, I'll keep that in mind when including any SDK libraries.

To answer your question, I was trying to stitch together some code examples I saw that would allow me to ping other devices with the ESP8266. The only code I found was using the ping.h library from the SDK, so the code snippets used the ip_addr.h include.

I actually wasn't aware of the Arduino IPAddress.h until you pointed it out, thanks! I checked it out, and it seems like that while it's a bit different, the same functionality is there. Namely, with ip_addr.h I can call ip_addr("192.168.0.1") and get a uint32 form that I need. With IPAddress.h I need to create an IPAddress object, use fromString to populate the _address field, and then use the overloaded cast to uint32. So the functionality is definitely there, but it seems like for this particular purpose, it's a few more lines of code. I'll switch over to using IPAddress.h though since it's already included when I include ESP8266WiFi.h. Thanks for the information!