New User - Basic problems

New to pio.
tried a basic arduino sketch.
Got a basic error.
Code:

#include <Arduino.h>

void setup() {
initSerial();
}

void loop() {}

void initSerial(){
Serial.begin(9600);
}

and the Error:

‘initSerial’ was not declared in this scope

Tried puting it up, down, change the name…
still get the msg.

I have to say that it’s very discouraging for a new platform user.
just copy pasted a basic code from the arduino IDE,
after about an hours’ worth of struggle to setup the boards and environments,
and now this pops up… :-/

P.S.
If this is the wrong thread,
please redirect me to the correct one.

Please read this FAQ section. You need to write proper C++ code. For that, at the time a function is called or a variable is referenced, it has to be known in advance. In C++, you use function prototype declarations for that before the call. So just correct your code

#include <Arduino.h>

/* prototype declaration. definition (=implementation) is below, but is called before that, so we need this ...*/
void initSerial(); 

void setup() {
initSerial();
}

void loop() {}

void initSerial(){
Serial.begin(9600);
}
1 Like

Than why didn’t it work when I just copied it above?..
so, we have code completion, but we still need declarations,
Even though in the basic IDE its transparent.
K. Cool.
So a header file, old school.
only a header “Tab”, for declarations…

I honestly think I’ll just stick with the old IDE.
But it was a nice try though.

Thanks for the quick reply.

Because Arduino sketches are .ino files, but you put that code in a *.cpp file, which is handled with the strict C/C++ rules.

PlatformIO can still compile your original code without any modifactions – just rename the file that your code is contained in from .cpp to .ino and run “Build”, and the build will succeed.

However, the Microsoft C/C++ Intellisense plugin only works on actually valid C/C++ files, so you won’t have autocomplete in ino files. That’s why it’s heavily recommended to start writing valid C/C++ code in .cpp files instead of .ino files.

1 Like

The Arduino IDE does a huge amount of work, behind the scenes, under the covers, and elsewhere, to make your life easier – provided you stick with that IDE.

As soon as you migrate to any other (proper) IDE, confusion reigns.

Thanks to the Arduino IDE, some people think that a library is just a header file, for example.

If you are interested in what the Arduino does for you, I wrote it up at Tutorial for creating multi cpp file arduino project - #36 by normandunbar - might be useful.

Cheers,
Norm.

2 Likes

Hi @razvanmal,

if you have a multi-file Arduino IDE project that you may wish to convert to C++ and PlatformIO, then have a look at this post (on the same thread as before) which is a basic example of a full conversion.

Have fun.

Cheers,
Norm.

1 Like