OK, so how do I get the full pathname then?

Used to be that it was simply this:

char fileName[] = __FILE__;

...which could be displayed on the device web page:

Now that behavior has been 'featured' away: FILE no longer returns the full path. How do you get the path name of the source file?

not sure it's available the way you suggest see predefined macros

encoding the directory and date in builds is a common practice. probably done using a makefile to build a signature file

I get :

C:\Users\micro\AppData\Local\Temp\arduino_modified_sketch_427643\sketch_aug01b.ino

What do you get ?

Arduino Nano
IDE 1.8.13
Windows 10

Solaria:
Used to be that it was simply this:

char fileName[] = __FILE__;

...which could be displayed on the device web page:

Now that behavior has been 'featured' away: FILE no longer returns the full path. How do you get the path name of the source file?

You'll need to provide some proof of that... I've used FILE many times, and it always returns the full, absolute path of the file, exactly as the spec indicates it does.

Create a simple program, that does nothing more than print the string to Serial, and post the code, and output here.

Regards,
Ray L.

Hi @Solaria

Which Arduino board are you compiling for?

Which software are you using (e.g., Arduino IDE, Arduino CLI, PlatformIO)?

Arduino IDE 1.8.13

Adafruit Feather HUZZAH ESP8266

file: /home2/neisius/Arduino/testor/testor.ino

char fileName[] = __FILE__;

void setup() {
  Serial.begin(9600);
  Serial.println(" ");
  Serial.println(sizeof(fileName));
  Serial.println(fileName);
}

void loop() {
}

Serial output:

11
testor.ino

I've seen references to this problem back at version 1.8.8:

It's a feature (a "change in core plans") !
It is also a step towards reproducible generation of flash binaries,

I guess it's an ESP8266 problem?

Solaria:
I've seen references to this problem back at version 1.8.8

Version 1.8.8 of what?

Solaria:
I guess it's an ESP8266 problem?

Correct. If you use file with other boards platforms, such as the official Arduino AVR Boards, you still get the full path. So the change to FILE only containing the sketch name when compiling for an ESP8266 board was a change made by the ESP8266 boards platform authors, not by Arduino.

i don't get a full path using gcc on my laptop

So likely a bug in the Huzzah Feather board package, which is most likely created by Huzzah?

Regards,
Ray L.

RayLivingston:
So likely a bug in the Huzzah Feather board package

Not at all a bug. This was an intentional change:

It's a feature (a "change in core plans") !
It is also a step towards reproducible generation of flash binaries,
it also offers smaller flash occupation with debug messages,
privacy for pre-generated core libraries,

RayLivingston:
which is most likely created by Huzzah?

"Huzzah" is Adafruit's marketing fluff alias for ESP8266 (like "NeoPixels" instead of WS2812). I haven't seen a lot of involvement by Adafruit with the ESP8266 boards platform, but I'm sure they've had some. Probably a lot of the work was done before Adafruit even came out with an ESP8266 board. I think the ESP8266 boards platform started as a volunteer project, but probably the manufacturer of the ESP8266, Espressif, has provided some support.

pert:
Correct. If you use file with other boards platforms, such as the official Arduino AVR Boards, you still get the full path. So the change to FILE only containing the sketch name when compiling for an ESP8266 board was a change made by the ESP8266 boards platform authors, not by Arduino.

Well that's odd. Yes, it works fine on a e.g. Circuit Playground Express...but: isn't FILE resolved by the C preprocessor? Seems like it wouldn't matter what board it was compiled for.

pi@rpi3:~ $ echo fileName=__FILE__ >cppTest.txt
pi@rpi3:~ $ cpp /home/pi/cppTest.txt 
# 1 "/home/pi/cppTest.txt"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "/home/pi/cppTest.txt"
fileName="/home/pi/cppTest.txt"

Is there a hack for the compiler toolchain that will make this work?

Solaria:
isn't FILE resolved by the C preprocessor? Seems like it wouldn't matter what board it was compiled for.

It does matter because the ESP8266 boards platform uses a different compiler than the Adafruit SAMD Boards platform of the Circuit Playground Express and the compiler installed on your RPi. The ESP8266 boards platform authors actually patched the compiler their platform uses to change its behavior in defining FILE.

Solaria:
Is there a hack for the compiler toolchain that will make this work?

Replace the patched version of the xtensa-lx106-elf-gcc compiler installed by the ESP8266 platform with an unpatched version. It's installed under ~/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc

pert:
I think the ESP8266 boards platform started as a volunteer project, but probably the manufacturer of the ESP8266, Espressif, has provided some support.

the esp8266 Arduino is a community project without involvement of Espressif. even the creator of the esp8266 Arduino and some of early contributors now work for Espressif, they do not contribute now to esp8266 Arduino. they work on Espressif projects like the SDKs and the esp32 Arduino

the esp8266 Arduino is based on the Espressif's NonOS SDK but not on the latest version because many parts of SDK were replaced by open source versions and some parts of SDK are patched. so no, no help from Espressif

Found a solution.

For Arduino IDE:

in the hardware subfolder of the Arduino IDE installation folder, create platform.txt:

compiler.cpp.extra_flags=-D__PATH__="{build.source.path}"

(complete list of Global Predefines properties here: [Redirecting])

You can then write sketches like this:

void setup() {

  Serial.begin(115200);
  delay(500);
  Serial.printf("__PATH__: %s\n",__PATH__);
  Serial.printf("__FILE__: %s\n",__FILE__);

}

void loop() {
}

...that will generate output like this:


10:22:20.191 -> __PATH__: /tmp/pathTEST
10:22:20.191 -> __FILE__: pathTEST.ino

For PlatformIO:

edit the file platformio.ini and add:

build_flags = 
	-D__PATH__=\"$PROJECT_DIR\"

The PlatformIO IDE will flag "PATH" as "undefined". You can ignore that, since the compile will complete without errors, or you can add this at the top of the program to make the IDE happy:

#ifndef __PATH__
#define __PATH__ "PATHundefined"
#endif

Was the full path ever useful from Arduino builds? After all, it usually points to some obscure temp directory rather than the actual source file.

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