Sara Chipps Sara Chipps - 10 months ago 63
C++ Question

Arduino IDE not recognizing that .c file is .cpp

I am making a library for a specific board for the Arduino IDE. The library works great and now I'm taking a step back to add OO. The Library is a mix of .c and .cpp files. I know in order to add classes I need only use .cpp.

This is the LED.h file.

Here is the LED.cpp file.

The error I get is:

In file included from /Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/Arduino.h:54:0,
from /Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/ble-nrf51822-master/source/main.c:49:
/Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/LED.h:12:1: error: unknown type name 'class'
class LED {
/Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/LED.h:12:11: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
class LED {
exit status 1
Error compiling for board JWB nRF51822(V1.0 32KB).

I'm guessing that the Arduino is seeing the .cpp file as .c, is there a compiler flag I need to set? Thank you.

Answer Source

So, the problem is that the C compiler for main.c doesn‘t understand the "class" keyword in the C++ header file LED.h. Can you change main.c to main.cpp and see if that works?

(You may also need to add

#ifdef __cplusplus
extern "C" {

at the top, and

#ifdef __cplusplus

at the bottom of the main.h file (or maybe the main.cpp file?) so that C++ doesn‘t try to mangle the names of some of your functions, so that the linker can find them…