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.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Sara Chipps
  • 9,322
  • 11
  • 58
  • 103

2 Answers2

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…

bwinton
  • 1,978
  • 2
  • 19
  • 21
  • 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. – uliwitness Nov 14 '16 at 11:04
2

You can't include C++ declarations in header files in C files. If you need to mix C and C++ declarations in the same header file, wrap the C++ ones in

#ifdef __cplusplus
class MyClass {
  // ...
};
#endif
  • 2
    But then you won't be able to access the LED class from main.c… – bwinton Nov 14 '16 at 02:45
  • Usually what people do in cases like this is write a C-only wrapper around the C++ class that can be called from C. I.e. just structs (without inheritance or methods) and `extern "C"` functions. – uliwitness Nov 14 '16 at 11:07
  • For anyone curious why this is an issue, it's mostly about _compilation units_. For a more detailed intro (that doesn't cover the details of C++ though) see http://masters-of-the-void.com/book10.htm – uliwitness Nov 14 '16 at 11:17