Notifications
Clear all

Platform.io one annoying feature

9 Posts
4 Users
0 Likes
4,118 Views
(@pugwash)
Sorcerers' Apprentice
Joined: 5 years ago
Posts: 923
Topic starter  

These are only my personal preferences but 1. I like to have my setup() and loop() functions at the top of my sketches, and 2. for ease of navigation order my functions alphabetically.

Platform.io will not compile unless the functions are between the header files and the setup() function.

There is a workaround and that is to declare all the functions between the header files and setup() but write the function's code below the loop() section. I don't know why the platform.io developers decided to implement in this manner but the C++ compiler doesn't care where the functions are situated.


   
Quote
Topic Tags
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 
Posted by: @pugwash

I don't know why the platform.io developers decided to implement in this manner

I've always written my C/C++ stuff this way in the distant past (no c++ between 2004 and 2019) : declare first, define later; with usually my declarations in .h files and my definitions in the cpp files.

Actually the permissiveness of the Arduino IDE (and other compilers ?) surprised me.

I'm a declarative type of guy ?Β 

Edit :

After following some links on the Web for something else i stumbled upon this, which explains some forgiveness from the Arduino IDE :

https://github.com/arduino/Arduino/wiki/Build-Process#pre-processing

Β 

Eric


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@pugwash

I tried platform IO quite a while back, but found it too awkward, though I do like my VSCode editor and use it a lot!Β  Maybe I will give platform IO another go at some stage.

I did try the new Arduino IDE in Alpha.Β  It looks promising and I look forward to it's final release - The dark theme is nice, but not quite the same as my Monokai Vibrant on VSCode πŸ™‚

Posted by: @pugwash

Platform.io will not compile unless the functions are between the header files and the setup() function.

This is actually the norm in every IDE I've ever worked with - Forward declarations are always required.Β  Like ZeFerby, I too was surprised how the Arduino IDE allowed it, but they may well just be forward declaring behind the scenes for you, or some other kind of compiler trickery! πŸ™‚


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @frogandtoad

I too was surprised how the Arduino IDE allowed it, but they may well just be forward declaring behind the scenes for you, or some other kind of compiler trickery! ?

Yes, that's exactly what it does.Β  It's simply a multi-pass compiler.Β  It goes thought the code first to obtain all the declarations, and then complies it after that.Β Β Β  I like to define my functions at the end of my main code.Β  But as has been pointed out some compilers force the user to put them at the top.Β 

I've been working with OpenCV on the Jetson Nano and discovered that it works this way too.Β  I had defined a method in a program and referenced before it had been defined.Β  It wouldn't compile.Β Β  It didn't even say exactly what the problem was.Β  It just pointed to the line were the function was called and said, "function undefined"Β Β  I knew that was baloney because the function was actually defined on the very next line!Β 

But nope the compiler doesn't look even so much as one line ahead.Β  I had to move the function above where I'm calling it from.

This has nothing to do with coding in general or languages in general.Β Β  This is entirely a compiler characteristic.Β  Some compilers allow functions to be placed at the end of a program.Β  Other won't.

To be certain you can always place the function definitions at the top, then all compilers will be sure to compile them without a problem.Β  I've been in the habit for years of placing my function methods at the bottom of the code text editor.

Another thing you can do is to place all your functions in a separate tabbed class called "Functions",Β  Then they'll always be there when you import or include your Functions class.

Β 

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

This has nothing to do with coding in general or languages in general.Β Β  This is entirely a compiler characteristic.Β  Some compilers allow functions to be placed at the end of a program.Β  Other won't.

I wouldn't say that's entirely true, simply because programs are generally read in a top down fashion, and so parsed sequentially.Β  Obviously you can parse the code twice, but then you loose compiler efficiency, especially in large programs, just for the sake of recording and determining the symbols, so there is a trade off, but most likely not such a big deal in embedded programming, as programs are usually small as can be.

Posted by: @robo-pi

Another thing you can do is to place all your functions in a separate tabbed class called "Functions",Β  Then they'll always be there when you import or include your Functions class.

Not sure if you've just stated it incorrectly, but the proper way is to us a header and implementation file (a .h (header) file, with an associated .cpp (implementation) file).Β  I have posed examples of such code in the past, and this is the best way to do it when your programs start to become larger in size.


   
ReplyQuote
Robo Pi
(@robo-pi)
Robotics Engineer
Joined: 5 years ago
Posts: 1669
 
Posted by: @frogandtoad

Not sure if you've just stated it incorrectly, but the proper way is to us a header and implementation file (a .h (header) file, with an associated .cpp (implementation) file).Β  I have posed examples of such code in the past, and this is the best way to do it when your programs start to become larger in size.

Yes that's what I'm talking about.Β  My post wasn't intended to be a tutorial. ?Β  You need to take care of all the details.Β  My point is that if you don't like having your functions at the beginning of your code you can move them to a separate tab.Β Β  Or, as you point out, to a separate "two tabs".

DroneBot Workshop Robotics Engineer
James


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@robo-pi

Posted by: @robo-pi

My point is that if you don't like having your functions at the beginning of your code you can move them to a separate tab.Β Β  Or, as you point out, to a separate "two tabs".

Just to clarify for newbies new to programming... tabs are something an IDE (for example, an Arduino IDE) displays for ease of access to your individual programming files, and not part of the language.


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

Eric


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

As you can guess, the same project in a more conventional C++ IDE like VSCode+PlatformIO (with all ino files renamed to cpp), required the usual prototype declaration forΒ 

void myUndeclaredFunction();

and a header file for myfile002, with a prototype declaration of

void myOtherUndeclaredFunction();

image

Eric


   
ReplyQuote