3

I've written 6 C++ classes on the CLion IDE to be used on Arduino. One of these classes uses a C++ library called "Adafruit_PWMServoDriver.cpp" provided by Adafruit to use the PWM driver they sell. I'm trying to test my code in a main.cpp, but upon building my code, I noticed that "Adafruit_PWMServoDriver.cpp" has the following code:

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

Naturally, I copied Arduino.h from the Arduino installation folder to my C++ project header directory. But for reasons I don't know, ARDUINO is apparently not greater than or equal to 100, so it attempts to include "WProgram.h." Because I cannot find this header file, I changed my code to only attempt to include Arduino.h.

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "Arduino.h"
#endif

However, Arduino.h itself includes several header files, and I am not sure where I can find all the find them, or if it is even necessary to try to do so. I need to get my test code running, so I don't want to directly test it in the Arduino IDE yet. What can I do to overcome #include Arduino.h and #include WProgram.h?

  • I guess that you are not using the Arduino build. Could you describe your IDE and compiler setup. Looks like you need to configure CLion with a path to the Arduino core files. – Mikael Patel Mar 29 '17 at 5:48
  • I am using CLion. Environment: MinGW-w64 (w64 5.0), CMake: 3.6.3, C++ compiler: C:\msys64\mingw64\bin\g++.exe. I've actually managed to find all the header files from the avr library and placed them in my project directory. However, now I am running into compatibility issues in the avr library files. As of now, the compiler can find all 3 avr header files that I've placed in my \include directory, which is correctly imported in my CMake. However, in my io.h file, there is a long list of #if defined (AVR_<various_names>). – Skipher Mar 29 '17 at 6:10
  • I assume this is to identify the type of device I am using, and it only expects different types of Arduino. However, I am currently working in CLion; therefore, it reaches the end of this list and the following code executes: #else # if !defined(COMPILING_AVR_LIBC) # warning "device type not defined" # endif #endif How can I make my coding environment compatible with the Arduino setup? Or can I avoid all this and somehow create C++ classes independently? – Skipher Mar 29 '17 at 6:15
  • You need to set the correct MCU define. Please see the Arduino-Makefile for an example. Or run the Arduino IDE and look at the compiler parameters. – Mikael Patel Mar 29 '17 at 7:43
1

The libraries from Adafruit and elsewhere assume you are using the Arduino IDE. If you choose not to use that you will have difficulties compiling your own code. The header files you ask about are in the IDE download. You can always grab a copy of that and put the appropriate .h files into your "include" search path.

However, in my io.h file, there is a long list of #if defined (AVR_various_names).

Well, do a test compile using the Arduino IDE, using "verbose compiling" and see what defines are generated for your particular board (which you haven't mentioned). There won't be that many of them. Then you can put that on the build commands for your IDE.

Or can I avoid all this and somehow create C++ classes independently?

You can create C++ classes inside the Arduino IDE. I don't see how this relates to your other issues.


The Arduino IDE basically generates build commands for avr-g++ - it isn't some horrible beast you should avoid using.

1

The #if defined (AVR_<various_names>) you see here have nothing to do with Arduino: this is the avr-libc trying to import the IO register definitions for the AVR chip you are using. Whenever you compile an AVR program, you pass the option -mmcu=<mcu-name> to avr-gcc, and it automatically defines the proper macro. For example, the Arduino Uno is built around an ATmega328P. Programs for the Uno must then be compiled with -mmcu=atmega328p, which makes the compiler define __AVR_ATmega328P__, which directs the avr-libc to #include <avr/iom328p.h>, which contains all the IO register definitions for that chip.

As for the ARDUINO macro, this one is defined by the Arduino IDE to be the version of you Arduino installation, without the dots. For example, if you downloaded the core files from Arduino 1.8.2, then you should compile with -DARDUINO=182.

Also, you should avoid copying all the library headers to your project, and instead use the proper -I options. And don't forget to tell the compiler your clock frequency with -DF_CPU=..., as it is needed by some avr-libc and Arduino stuff.

In the end, you will see you need quite a lot of compiler options to properly compile Arduino code. The easiest way to do it is definitely to use the Arduino IDE, as it handles all this for you. If you don't like the IDE (understandable...), you could try Arduino Builder, which should be entirely compatible. If you want to compile from just a Makefile, I recommend you take a look at Sudar Muthu’s Arduino Makefile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.