I realize this may be a basic question, but my poking about the forum only made it more confusing to me.
Consider this very basic example code:
Servo myservo;
void setup() {
myservo.attach(D0);
}
void loop() {
}
compiles perfectly in web IDE. Now I place this code in a .cpp file, compile in the CLI and I get an error saying ‘Servo does not name a type’.
I add ‘#include “application.h”’ and BAM! all is good with the world.
Why is that? What does application.h do exactly, and is this the correct way to get my code to compile in the CLI? When do I need to include application.h?
I tried importing/adding Servo via the CLI
particle library add servo
but it cant find that library, plus i read that Servo.h is a system library and should already be included (which I guess is why the Web IDE works).
I just want to make sure I am using the tools in the most efficient way.
1 Like
When you are building in the Web IDE the pre-processor is used before compilation to automatically include application.h, create function prototypes and other things to provide a more user-friendly interface that is similar to Arduino.
The CLI can also use the pre-processor if you use a .ino file for your code instead of .cpp since the actual compiler can’t compile .ino files. If you use Workbench you can see the .cpp that is generated from the .ino file.
When you include application.h a lot of other header files get included as well, which allows the compiler to build your C++ code for the particle platform. If you’re curious, you can look at application.h for yourself. You can also use #include "Particle.h"
since it’s an alias for application.h
.
Generally, you should have #include "Particle.h"
at the top of every .cpp or .h file in your project that depends on internal libraries and modules provided by Particle. If you can, I would highly recommend trying out Workbench. It has much more functionality than just the Web IDE or the CLI.
For most of my projects, I often organize them something like this:
example/
src/
exampleApp.cpp
exampleLib.cpp
exampleLib.h
lib/
sensor1/
...
sensor2/
...
Also, if you want to look at the implementation of Servo
, you can find it here.
4 Likes
Awesome info. Thanks!
I installed work bench and setup/transferred a couple of my projects. Looks really nice.
I also poked about in application.h and things are much clearer.
Thanks again for the help.
1 Like