When a basic sketch is compiled, the code is by default interpreted as C++ code. If you prefer to use C code or have a library written in C, there are a few things nessecary to ensure the code is compiled using C language rules.
When using C code in source files, in addition to a header file; you need to use a .c
extension instead of the Arduino .ino
, or C++ .cpp
extension. Header files for both C and C++ use the extension .h
.
If you have a header file with only C declarations inside, you can use it in C++ code by wrapping the include in an extern
declaration block. For a full example see the attached file 01-Separate_header.zip.
extern "C"{ #include "My_C_Header.h" }
If you do not wish to have a separate header, or if you want to mix C and C++ declarations in the same header, you can wrap the appropriate C declarations in an extern
block. You can have multiple declarations inside an extern block.
extern "C"{ int MyCFunction( void ); }
This example can be found complete in 02-Mixed_header.zip.
Attached files: 01-Separate_header.zip, 02-Mixed_Header.zip
Tags: C, C++