Arduino IDE not recognizing that .c file is .cpp
Asked Answered
I

1

7

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.

https://gist.github.com/SaraJo/182220fda82cbe30255fe95f59d4a6b4

Here is the LED.cpp file.

https://gist.github.com/SaraJo/1b3d6967d7bc2ef2e70d79025b755eb9

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.

Isabelisabelita answered 14/11, 2016 at 2:28 Comment(0)
I
9

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" {
#endif

at the top, and

#ifdef __cplusplus
}
#endif

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…

Incompletion answered 14/11, 2016 at 2:33 Comment(1)
You'll have to do the extern "C" on both declaration (aka prototype) and definition (if it is necessary, i.e. if your main.cpp declares anything that someone else calls (which it usually shouldn't). The main() function itself should not need it, the C++ compiler knows to do that implicitly.Niko

© 2022 - 2024 — McMap. All rights reserved.