Has anyone here debugged Arduino in VS Code ?

I verified the program, it was OK, but when run to debug, it shows:

…ino: file format not recognized; treating as linker script

and, the task terminated with exit code 1.

are you trying to run your Arduino code, the .ino file, on a PC in Visual Studio?

No and yes, since VS Code has “debugging” function, so I want to debug my code after it passed verify.

I just test this function on another pc with Ubuntu OS, the attachment was the error prompt.

No and yes

You mean yes.

If your code uses any Arduino specific hardware (e.g. pins, timers etc) or Arduino functions included by the IDE on your behalf in an INO sketch, you’re going to get nowhere fast with this. Also, if you use any Arduino libraries, they same requirements must be met by them and you must provide copies of them somewhere in your VS include path.

If you have pure C/C++ code then you should be able to debug by renaming ino as .c/.cpp.

Other than that I suspect this approach is doomed to fail hard.

Even if you do get the code to compile you will be debugging x86/x64 code, not (insert Arduino target platform CPU here) code.

when i "simulate" Arduino code on a laptop, i rename the .ino .cpp, i include a file that contains a main() and stubs from many of the Arduino built in functions (e.g. pinMode(), digitalWrite(), serial) and often have to create a stub struct/class for any libraries

Could you explain "stubs" more clearly ?

Could you explain “stubs” more clearly ?

“stubs” are instances of the core functions that allow the program to compile, but don’t actually do the actual function. Like:

uint16_t analogRead(int pin) {
 return 123;
}
void analogWrite(int pin, int val) {
 // do nothing.
}

MianQi:
Could you explain “stubs” more clearly ?

i’ll include the file, “sim.h” at the top of a .ino renamed .cpp

it also include my “HardwareSerial.h”

i see some mistakes in these (e.g TWO_PI). they get hacked up for specific cases, but usually work most of the time. obviously the grow over time

HardwareSerial.h (3.54 KB)

sim.h (5.67 KB)

Thanks you two. I am inspired by these 3 articles:

https://www.codeproject.com/Articles/5150391/Creating-and-Debugging-Arduino-Programs-in-Visual

and, I found maybe the solution:

platfomIO extension for VS Code.

kinda curious about how Visual Studio supports Arduino. does it support all possible libraries?

when i try to simulate someone else’s code, they often include libraries i’m not familiar with. those sub-function calls are not defined. So I stub them out with do nothing functions so that I can compile with gcc and run the code on a shell window

here are two examples

class AccelStepper {
  public:
    AccelStepper          (int id, byte p0, byte p1) {};
    void setMaxSpeed      (int max_speed) {};
    void setAcceleration  (int acceleration) {};
    void runToNewPosition (int motorPos) {};
};
class LiquidCrystal {
  public:
    LiquidCrystal (int a, int b, int c, int d, int e, int f) {};

    void begin (int a, int b) {};
    void clear (void) {};
    void setCursor (int a, int b) { printf ("\n"); };

    void print (const char * s) { printf (" %s", s); };
    void print (float f)        { printf (" %.2f", f); };
    void print (int i)          { printf (" %d", i); };
    void print (int i, int n)   { printf (" %d", i); };
};

gcjr:
kinda curious about how Visual Studio supports Arduino. does it support all possible libraries?

when i try to simulate someone else's code, they often include libraries i'm not familiar with. those sub-function calls are not defined. So I stub them out with do nothing functions so that I can compile with gcc and run the code on a shell window

PIO supports on-device debugging (Debugging — PlatformIO latest documentation), so there's no need to stub anything, since everything is cross-compiled using the usual compiler and with the native libraries for the device.

Pieter

I tried to debug Arduino in VS Code by simavr on ubuntu, i found 2 pages useful:

  1. 2.3. Get Started — SimAVR 1.0.0 documentation
  2. https://www.instructables.com/id/Debugging-AVR-code-in-Linux-with-simavr/

while, they all different with my environment, I’m still finding solution for this target.