VS Arduino
Visual Micro >> Project Guidance >> Setting compile order of .ino / .cpp files
https://www.visualmicro.com/forums/YaBB.pl?num=1558364438

Message started by Abbott HMG on May 20th, 2019 at 3:00pm

Title: Setting compile order of .ino / .cpp files
Post by Abbott HMG on May 20th, 2019 at 3:00pm
I have a top-heavy .ino file with lots of classes that precede my code. It all works except if I put the classes in separate cpp files. For maintenance convenience and clarity  I'll try to put the various classes into libraries (but they're not libraries).
It would be easier to create cpp/h files and tell Vmicro what order to compile them in, since they must be known (parsed by) the compiler prior to parsing the .ino. Just compiling the multiple files gives me reference errors.
I understand that this works in the Arduino IDE by using tabs and naming modules in the reverse order of compile (1module.ino, 2class.cpp, 3class.cpp. 4class.cpp) a class that invokes another must have a lower number.
See attached .ino.
:-[
Abbott
https://www.visualmicro.com/forums/YaBB.pl?action=downloadfile;file=HMGVacuum.ino ( 8 KB | 4 Downloads )

Title: Re: Setting compile order of .ino / .cpp files
Post by Visual Micro on May 20th, 2019 at 4:38pm
Hi

I think some confusion. cpp files are supported but they must be in the project folder or in a folder called \src below the project or any folder below the src folder.

cpp files are highly recommended and should build fine. The names of cpp files is not important, the linker works out the order.

For multiple .ino files the names are important and define the order that they are all combined into a cpp file in the temp build folder. It is the resulting cpp of the combined ino files that is compiled. However the project_name.ino will always be first.

Putting classes in the ino files is not recommended at all.

If you cpp files reference libraries then either make sure the "vmicro>compiler>deep search" is enabled (which it is by default) or make sure all of your library #includes are also in the project_name.ino

If you create any user types such as structs in the .ino files that are used in the signature of methods in the same file then make sure you also add the prototypes between where the type is defined and the first method that uses the type in its signature.

If you create an object from a class in a .cpp and need to use it in the .ino then then mark the object as extern for it to be found by the .ino code.

If you create seperate cpp/h make sure you add the #include <arduino.h> which is automatically added to the temp .ino code prior to compile so you won't see it in the .ino code.

If you are using avr, then click "vMicro>show/hide hidden files" and look at how the arduino core works. HardwareSerial.cpp/h is a good example which shows Serial, Serial1 etc being defined and created.

Does this help?


Title: Re: Setting compile order of .ino / .cpp files
Post by Abbott HMG on Jun 13th, 2019 at 10:10pm
Thanks for the response.
I originally had 12 cpp files but included them in ino to clean compile.

I now have the following cpp with the call below:



Code (c++):
#include<Arduino.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Clcd.h> // i2c LCD i/o class header
//#include "LCDManager.h"

static hd44780_I2Clcd* lcd;
class LcdManager {
public:
     static void PrintLn(String message, int rowNo = 0) {
           if (rowNo == 0) {
                 lcd->setCursor(0, 0);
           }
           else {
                 lcd->setCursor(0, 1);
           }
           lcd->print(message);
     }
     static void Init() {
           lcd = new hd44780_I2Clcd(_lcd_addr);
           lcd->begin(2, 16);
           // Turn on the blacklight and print a message.
           lcd->backlight();
           lcd->clear();
           lcd->print(START_MSG);
     }
};


Called by:


Code (c++):
     void Setup() {
           Serial.begin(9600);// start serial port
           Serial.println("Vacuum Test");
           LcdManager::Init();            
     }


I get LcdManager not defined error
do I need an extern ... and where
Thanks
Abbott

Title: Re: Setting compile order of .ino / .cpp files
Post by Visual Micro on Jun 13th, 2019 at 10:16pm
If you use the Add Code > C++ class and header to add a class called "MyTestMeClass" you will see that an object is created called MyTestMe based on the MyTextMeClass and that it is available to the ino code.

I'm not the expert when it comes to code syntax, it's just whatever arduino supports, therefore you can ask on other forums that are designed for code questions.

note: When adding arduino libraries never use a path, just the .h name

#include <hd44780_I2Clcd.h>

NOT

#include <hd44780ioClass/hd44780_I2Clcd.h>


VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.