Library Type Conflicts / Include delay

10 views
Skip to first unread message

Victor Aprea

unread,
Apr 15, 2017, 2:39:46 PM4/15/17
to Arduino Developers
Get all,

I'm working on porting a library, and have run into a problem I'm hoping someone on here can point me in the right direction.

if I #include <Arduino.h> in my cpp class file, I end up with:

/home/vic/arduino-1.6.9/hardware/arduino/avr/cores/arduino/USBAPI.h:30:24: note: in expansion of macro 'u16' typedef unsigned short u16;

Note: I don't have any _actual_ dependencies on USBAPI.h in this library. Mind you, literally the only reason that I actually think I need to include Arduino.h is that I'd like to be able to call delay(ms) from within the library. I already patched around vendor type definitions by doing: 

#if defined(ARDUINO_ARCH_AVR)
#define s8 int8_t
#define s16 int16_t
#define s32 int32_t
#define s64 int64_t
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
        #define u64 uint64_t
#include <stdint.h>
...

... because otherwise they typedef these in their source code, creating the same conflict. I tried just putting in a forward declaration of delay in my c++ file and leaving out Arduino.h, but then I end up with a linker error when I try to compile my sketch.

undefined reference to `delay(unsigned long)'

So... how does one use delay in an Arduino library without including Arduino.h, or what should I do differently to resolve the conflicting typedef quandary between the vendor libs and USBAPI? It would be nice to not have to broadly alter the vendor source to use proper stdint types (as tempting as that is), for fear of subsequent maintainability if/when the vendor updates their source. I'll do it if I have to...

Kind Regards,
Vic

Victor Aprea // Wicked Device

Victor Aprea

unread,
Apr 15, 2017, 2:56:23 PM4/15/17
to Arduino Developers
FWIW, I just did the wholesale replacement of s8 -> int8_t, u8 ->uint8_t  and so forth across all the vendor source files and then, as expected, I can include Arduino.h in my cpp wrapper and all is well with the world. That felt like a drastic measure though. Still interested in other perspectives.

Kind Regards,
Vic

Victor Aprea // Wicked Device

bob cousins

unread,
May 7, 2017, 9:09:42 AM5/7/17
to Developers, devel...@arduino.cc
Try declaring as:

extern "C" void delay(unsigned long ms);


Álvaro Lopes

unread,
May 7, 2017, 9:57:52 AM5/7/17
to devel...@bcmi-labs.cc, devel...@arduino.cc
If you are required to do so, and if you are to place this in a header file, use the "standard" approach for C/C++ compatibility:

#ifdef __cplusplus
extern "C" {
#endif

void delay(unsigned long ms);

#ifdef __cplusplus
}
#endif

This will ensure it can be used both from C++ and C (even with old ANSI compilers)

Alvaro

On 07/05/17 14:09, 'bob cousins' via Developers wrote:
> Try declaring as:
>
> extern "C" void delay(unsigned long ms);
>
>
> --
> You received this message because you are subscribed to the Google Groups "Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@bcmi-labs.cc <mailto:developers+...@bcmi-labs.cc>.
Reply all
Reply to author
Forward
0 new messages