Close

Arduino IDE "includes" solved

A project log for 1-Pixel Pac-Man

Retro-gaming just got fewer pixels

Mike SzczysMike Szczys 05/31/2015 at 16:130 Comments

I wrote most of the code for this before I had the hardware. To do so I wrote a "hardware emulator" using the SDL2 library. This is great because I could work on it when all I with me was a computer.

The problem came when I went to load it onto the Teensy 3.1 using the Arduino IDE. I had abstracted out all of the display and control specific stuff so that portability would be easy. But when I tried to compile, there were "undefined" errors for every function.

I had correctly included the prototypes for each function in the header files, so I assumed the issue was that the linker wasn't pulling in the .c files. When I changed the name of the .c files to .cpp it work. How frustrating!

Turns out the issue is that Arduino is compiling the .ino files as C++. This causes two issues:

C Functions must be declared with EXTERN

The .c files were getting included, but I need to tell the compiler to treat my C functions as C and not as C++. There is a helpful thread on the Arduino forums that shares the answer. You need to wrap function definitions within header files in some magic:

#ifdef __cplusplus
extern "C"{
#endif

void cFunctionPrototype1(void);
void cFunctionPrototype2(void);

#ifdef __cplusplus
} // extern "C"
#endif

C99 won't work with C++

I also like to define local variable in "for" loops using the C99 standard. That won't work at all with a C++ compile (apparently):

for (uint8_t i = 0; i<7; i++) { }

needs to be:

uint8_t i;
for (i = 0; i<7; i++) { }

Discussions