How can I view the source file generated by the Arduino IDE from my sketch, which is actually compiled by the compiler?

Hi all,
I use Arduino IDE 2.x and my question is how can I view the source file generated by the Arduino IDE from my sketch, which is actually compiled by the compiler?

look in the detailed compilation output in the console
you'll see the path of the temp directory that has been created for the compilation

for example, on my Mac, compiling an empty sketch with two tabs

void setup() {
  // put your setup code here, to run once:

}

and tab loop.ino

void loop() {
  // put your main code here, to run repeatedly:

}

generates everything needed into this temp directory

/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/arduino/sketches/19CCB75298EDA44DB8DC5C4F951E723E

in this directory you have a sketch directory where you can find your original ino that has been "tortured"

#include <Arduino.h>
#line 1 "/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/.arduinoIDE-unsaved2024422-4541-1p4h1r4.40rsf/sketch_may22a/sketch_may22a.ino"
#line 1 "/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/.arduinoIDE-unsaved2024422-4541-1p4h1r4.40rsf/sketch_may22a/sketch_may22a.ino"
void setup();
#line 1 "/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/.arduinoIDE-unsaved2024422-4541-1p4h1r4.40rsf/sketch_may22a/loop.ino"
void loop();
#line 1 "/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/.arduinoIDE-unsaved2024422-4541-1p4h1r4.40rsf/sketch_may22a/sketch_may22a.ino"
void setup() {
  // put your setup code here, to run once:

}



#line 1 "/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/.arduinoIDE-unsaved2024422-4541-1p4h1r4.40rsf/sketch_may22a/loop.ino"
void loop() {
  // put your main code here, to run repeatedly:

}

so you can see both tabs were merged and you'll see reference to another temp directory

/private/var/folders/96/rsjd0mkd3xn0whm5622w0f500000gn/T/.arduinoIDE-unsaved2024422-4541-1p4h1r4.40rsf/sketch_may22a

because my sketch was not saved so that's where it was temporarily saved

The process uses separate compile units (multiple files being compiled and linked together) you won't see a full cpp file with main() etc being put together

Thank you for the rapid response.
I forgot to specify that I am using Wondows10.
When I check in File->Preferences the "Show verbose output during compile" checkbox I can see in the ARDUINO IDE output window the path and name of the .cpp file I'm interested in, but when I try to open the specified folder with WIndows explorer - the folder doesn't exist . (no such folder and path)

Part of the path might be a hidden folder. You will need to enable "show hidden files and folders" (or something in that line) in Windows Explorer (under Options).

I'm currently not using Windows so can't advise more accurately.

I can't see the folder and the file.
This is my output in ARDUINO IDE 2.3.2:

Blockquote

FQBN: MiniCore:avr:328:clock=8MHz_internal,BOD=disabled,eeprom=erase,variant=modelPB
Using board '328' from platform in folder: C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1
Using core 'MCUdude_corefiles' from platform in folder: C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1

Detecting libraries used...
C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -std=gnu++17 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega328pb -DF_CPU=8000000L -DARDUINO=10607 -DARDUINO_AVR_ATmega328 -DARDUINO_ARCH_AVR -Wextra -flto -g -IC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\cores\MCUdude_corefiles -IC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\variants\pb-variant C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A\sketch\sketch_may16a.ino.cpp -o nul
Generating function prototypes...
C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -std=gnu++17 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -w -x c++ -E -CC -mmcu=atmega328pb -DF_CPU=8000000L -DARDUINO=10607 -DARDUINO_AVR_ATmega328 -DARDUINO_ARCH_AVR -Wextra -flto -g -IC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\cores\MCUdude_corefiles -IC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\variants\pb-variant C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A\sketch\sketch_may16a.ino.cpp -o C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\3326515538\sketch_merged.cpp
C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\builtin\tools\ctags\5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\3326515538\sketch_merged.cpp
Compiling sketch...
"C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -Wall -Wextra -std=gnu++17 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega328pb -DF_CPU=8000000L -DARDUINO=10607 -DARDUINO_AVR_ATmega328 -DARDUINO_ARCH_AVR -Wextra -flto -g "-IC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\cores\MCUdude_corefiles" "-IC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\variants\pb-variant" "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A\sketch\sketch_may16a.ino.cpp" -o "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A\sketch\sketch_may16a.ino.cpp.o"
Compiling libraries...
Compiling core...
Using precompiled core: C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\cores\c11f6225b574148d546561cffac1c518\core.a
Linking everything together...
"C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-gcc" -Wall -Wextra -Os -Wl,--gc-sections -mmcu=atmega328pb -w -flto -g -o "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.elf" "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A\sketch\sketch_may16a.ino.cpp.o" "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/..\..\cores\c11f6225b574148d546561cffac1c518\core.a" "-LC:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A" -lm
"C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.elf" "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.eep"
"C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-objcopy" -O ihex -R .eeprom "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.elf" "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.hex"
cmd /C echo. && "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-objdump" --disassemble --source --line-numbers --demangle --section=.text "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.elf" > "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino_atmega328pb_8000000L.lst"

"C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A/sketch_may16a.ino.elf"
Sketch uses 1992 bytes (6%) of program storage space. Maximum is 32384 bytes.
Global variables use 250 bytes (12%) of dynamic memory, leaving 1798 bytes for local variables. Maximum is 2048 bytes.

Blockquote

if I understand correctly, the file I am interested is:
C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\3326515538\sketch_merged.cpp

in folder C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\ there is no subfolder "3326515538" as in the path above.
(Show hiden files in enbabled in Win Explorer)

what about

C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Temp\arduino\sketches\CC11E7F9EFE8DED9B3451D314AF5431A\sketch

This folder exist and contains 3 files:
"sketch_may16a.ino.cpp"
"sketch_may16a.ino.cpp.d"
"sketch_may16a.ino.cpp.o"

The first file is something very close to my sketch source file, but it is not exactly file I expect to see. As new user I can't attach the file, so I will copy it contents in my post"

#include <Arduino.h>
#line 1 "C:\\Users\\Emil_T.ROKONET_TREE\\Documents\\Arduino\\sketch_may16a\\sketch_may16a.ino"
int a;

#line 3 "C:\\Users\\Emil_T.ROKONET_TREE\\Documents\\Arduino\\sketch_may16a\\sketch_may16a.ino"
void setup();
#line 20 "C:\\Users\\Emil_T.ROKONET_TREE\\Documents\\Arduino\\sketch_may16a\\sketch_may16a.ino"
void loop();
#line 3 "C:\\Users\\Emil_T.ROKONET_TREE\\Documents\\Arduino\\sketch_may16a\\sketch_may16a.ino"
void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  }

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example, '!' is the same as 33, so you could also use this:
// int thisByte = '!';

void loop() {
  // prints value unaltered, i.e. the raw binary version of the byte.
  // The Serial Monitor interprets all bytes as ASCII, so 33, the first number,
  // will show up as '!'
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);
  // But you can declare the modifier for decimal if you want to.
  // this also works if you uncomment it:

  // Serial.print(thisByte, DEC);


  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);

  Serial.print(", bin: ");
  // prints value as string in binary (base 2) also prints ending line break:
  Serial.println(thisByte, BIN);

  // if printed last visible character '~' or 126, stop:
  if (thisByte == 126) {  // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while (true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++;
}

I want to see cpp file with main() function inside and all serup() and loop() functions insiede the main function.

Is it possible?

Hi @emilvtc

It is defined in the file at this path:

C:\Users\Emil_T.ROKONET_TREE\AppData\Local\Arduino15\packages\MiniCore\hardware\avr\3.0.1\cores\MCUdude_corefiles\main.cpp

No, there will not be a single file that contains everything as source code. The process will compile every single .cpp file (known as a compilation unit) to an object file (extension .o) and after that it will link them all together.

To view the source file generated by the Arduino IDE 2.x from your sketch, you can follow these steps:

  1. Enable Verbose Output During Compilation:

    • Open the Arduino IDE.
    • Go to File > Preferences.
    • Check the box for "Show verbose output during compilation".
    • Click "OK" to save the changes.
  2. Compile Your Sketch:

    • Compile your sketch by clicking the "Verify" button (the checkmark icon).
    • The IDE will display detailed information in the output console at the bottom, including the paths to various temporary files and directories.
  3. Locate the Temporary Build Directory:

    • During the verbose compilation output, look for a line that indicates the path to the temporary build directory. It usually looks something like this:
      /tmp/arduino_build_<random_string>
      
    • This directory contains all the intermediate files generated during the build process, including the preprocessed source files.
  4. Access the Temporary Directory:

    • Navigate to the temporary build directory using your file manager or terminal.
    • In this directory, you will find several files, including the preprocessed source files, object files, and the final binary.
  5. Identify the Preprocessed Source File:

    • Look for files with extensions like .cpp (C++ source files) or .c (C source files).
    • These files represent the source code that the Arduino IDE has generated from your sketch.

Here's an example of what you might see in the verbose output (specific paths will vary based on your operating system and configuration):

Using board 'uno' from platform in folder: /home/user/.arduino15/packages/arduino/hardware/avr/1.8.3
...
/home/user/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/bin/avr-g++ -c -g -Os -Wall -Wextra -std=gnu++11 ...
...
/tmp/arduino_build_123456/sketch/sketch_name.ino.cpp -o /tmp/arduino_build_123456/sketch/sketch_name.ino.cpp.o

In this example, /tmp/arduino_build_123456/sketch/sketch_name.ino.cpp is the preprocessed source file generated from your sketch.

By following these steps, you can locate and view the source files generated by the Arduino IDE from your sketch, providing insight into how your code is being compiled and processed.

as I said

Nice ChatGPT output; unfortunately you forgot to tell ChatGPT that the user is using Windows and probably that the user is using IDE 2.X

BTW, sketch_merged.cpp is a huge, very temporary file, which is essentially the recursive preprocessor expansion of your .ino.cpp, along with the automatic #include <Arduino.h>; with a bunch of source file and line annotations. It is created, used, and deleted during the "Generating function prototypes" stage; not directly used for compiling.

@nath_morris has already been permanently suspended after a review of their account.

Although it is not directly useful to @emilvtc at this point in the discussion, the AI-generated post they made contains information that might be useful to others (even after taking into account the defects mentioned by @sterretje) who have a similar question and find this thread during their research, so the post was retained after checking it for any hidden spam links.

So please forget about the spammer and focus on assisting @emilvtc.

2 Likes

Thanks to everyone who answered my questions.
I have experience in programming embedded systems, but Arduino is something completely new to me.
With it, many things are "hidden" and are different from the MCU IDE I've worked with.

My question may be stupidly worded, but I'm trying to understand how the build process is implemented in the arduino IDE.

I understand that for the convenience of novice programmers, some of the processes are hidden to make writing programs easier and simpler, but I want to understand what is behind it all.

assuming you are using a UNO for example you'll find the core code in GitHub

in there you'll find the main.cpp file that is added automatically when you compile

if you look at the code you'll see that the main() function basically calls ancillary functions to set up the board (configure pins, timers etc for the Arduino environment) and call your setup() function and then engage in an infinite loop calling loop()

    init();
	setup();
	for (;;) loop();

(over-simplified)

the IDE joins all the .ino files together also in the temp directory before compiling and tries to generate function prototypes as well for all the functions you use (in case you use them before declaring them in your .ino)

then everything gets compiled, including core libraries and those you depend on.

Makes total sense to me!

Sorry, not sure how much this will help, note I am using Windows 11. When I wish to get to the directory that Arduino output the build information into, I often simply use the address:

Which gets me to:

I occasionally do this for a few different reasons. Sometimes it is to delete all of this cached information to force sketches to do a full recompile.

Double click on sketches and in my current case there is only one directory, when there are multiple ones, I either look at the verbose compiler output to find which one, or simply sort by date and check the most recent ones...

Sometimes I go here to look at some of the generated files, such as map or listing files.

Or sometimes I want a command prompt there and it is quick and easy to do so, if you have windows Terminal installed.. Right click in the explorer window, and choose the menu item: Open In Terminal

Where I might use it, if my Teensy 4.x program crashed with a Crash Report, with a command line to run addr2line on the the generated .elf file with the crash address, to hopefully find out where the crash happened.

Not sure if this answers any of your questions, but may give you a few shortcuts on how to get there and reasons why you might want to.

Good luck

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.