espressif / ESP8266_NONOS_SDK

ESP8266 nonOS SDK
Other
919 stars 533 forks source link

compile issues with g++ #73

Open hmueller01 opened 6 years ago

hmueller01 commented 6 years ago

I had some trouble to compile c++ code with the SDK. Firstly all prototypes in all header files (e.g. ets_sys.h, c_types.h, ...) must be capsuled by extern "C":

#ifdef __cplusplus
extern "C" {
#endif
// put prototypes here 
#ifdef __cplusplus
}
#endif

Otherwise linker fails with undefined reference.

Also c_types.h should be patched, because bool, true and false are defined in c++ (which was correctly excluded for c++), but BOOL, TRUE and FALSE are not:

#ifndef __cplusplus
 typedef unsigned char   bool;
-#define BOOL            bool
 #define true            (1)
 #define false           (0)
+#endif /* !__cplusplus */
+
+#define BOOL            bool
 #define TRUE            true
 #define FALSE           false

-
-#endif /* !__cplusplus */
-
 #endif /* _C_TYPES_H_ */
hmueller01 commented 6 years ago

Instead of changing the SDK headers, one could also add the extern "C" while including C headers in cpp code. e.g.

// put C includes inside here to avoid undefined references by linker.
extern "C" {
#include <ets_sys.h>
}
hmueller01 commented 6 years ago
#define BOOL            bool
#define TRUE            true
#define FALSE           false

still needs to be fixed (outside #ifndef __cplusplus). E.g. the mqtt example won't compile with c++ otherwise.