greiman / SdFat

Arduino FAT16/FAT32 exFAT Library
MIT License
1.05k stars 496 forks source link

'File' does not name a type #291

Open emp-98 opened 3 years ago

emp-98 commented 3 years ago

include

SdFat sd;
File file; char fileName[] = "datalog.csv";

define LOGFILE "datalog.csv"

file = sd.open(fileName, FILE_WRITE);

It gives me the following error: 'File' does not name a type. Thanks in advance.

greiman commented 3 years ago

So what?

int n;
n = strlen("abc");
void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:
}

when not in a function gives:

sketch_apr29a:2:1: error: 'n' does not name a type

 n = strlen("abc");

 ^

This compiles:

#include <SdFat.h>
SdFat sd;
File file;
char fileName[] = "datalog.csv";
#define LOGFILE "datalog.csv"

void setup() {
  file = sd.open(fileName, FILE_WRITE);
}
void loop() {
  // put your main code here, to run repeatedly:
}
arnoson commented 2 years ago

Im using the exact code you posted (below 'This compiles'). With a fresh platformio project and a teensy41 and unfortunately it doesn't compile. It used to in another project also using the teensy41. This is the output I get from platformio run:

Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/teensy/teensy41.html
PLATFORM: Teensy (4.13.1) > Teensy 4.1
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 7.75MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES:
 - framework-arduinoteensy 1.154.0 (1.54)
 - tool-teensy 1.154.210805 (1.54)
 - toolchain-gccarmnoneeabi 1.50401.190816 (5.4.1) 
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 93 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <SdFat> 2.0.7
|   |-- <SPI> 1.0
Building in release mode
Compiling .pio\build\teensy41\src\main.cpp.o
Compiling .pio\build\teensy41\lib797\SPI\SPI.cpp.o
Compiling .pio\build\teensy41\libcc3\SdFat\ExFatLib\ExFatDbg.cpp.o
Compiling .pio\build\teensy41\libcc3\SdFat\ExFatLib\ExFatFile.cpp.o
Compiling .pio\build\teensy41\libcc3\SdFat\ExFatLib\ExFatFilePrint.cpp.o
Compiling .pio\build\teensy41\libcc3\SdFat\ExFatLib\ExFatFileWrite.cpp.o
Compiling .pio\build\teensy41\libcc3\SdFat\ExFatLib\ExFatFormatter.cpp.o
Compiling .pio\build\teensy41\libcc3\SdFat\ExFatLib\ExFatPartition.cpp.o
src\main.cpp:3:1: error: 'File' does not name a type
 File file;
 ^
src\main.cpp: In function 'void setup()':
src\main.cpp:8:3: error: 'file' was not declared in this scope
   file = sd.open(fileName, FILE_WRITE);
   ^
*** [.pio\build\teensy41\src\main.cpp.o] Error 1
arnoson commented 2 years ago

There has been a Teensyduino update recently (https://github.com/PaulStoffregen/cores/releases/tag/1.54) which also mentions Sdfat a few times, maybe this has something to do with it?

greiman commented 2 years ago

Yes there are conflicts in Teensy. It's best to use the Teensy wrapper for SdFat.

You could try the github version of SdFat but you must use a more basic type in place of 'File'. Try replacing 'SdFat' with 'SdFs' and 'File' with 'FsFile'.

The type 'File' is a typedef but there are conflicts in Teensy so I don't define 'File' in Teensy.

arnoson commented 2 years ago

Thank you @greiman for the quick help! The SdFat type was still working, but using FsFile did the trick. I'll have a look into the teensy's sd wrapper, although I wan't my code to work with various arduinos so maybe I just create a custom typedef and set it to File by default und to FsFile for the teensy.

greiman commented 2 years ago

It's becoming difficult to write compatible programs that access SD cards on all Arduino like boards.

I wrote the first version of SdFat at the end of 2008 for the ATmega328. Arduino still uses a 2009 version with a few modifications in its SD.h library.

Teensy has a virtual filesystem that uses SdFat-beta. ESP boards use a wrapper for FatFS. The new "Arduino Pro system" has a new API based mbed SD libraries.

Each RTOS has its own way of handling SD cards and SD drivers. Too bad Arduino didn't develop a more advanced system and set the standard for a real IoT system a long time ago.

PaulStoffregen commented 2 years ago

Sorry, it's my fault "File" broke for SdFat-only usage on Teensy. I want to fix this for Teensyduino 1.56. Even though we now have a SD library which is merely a thin wrapper for SdFat, where File does work, I also want to find a way to make all the SdFat examples with File "just work" on Teensy. So far I do not know any way which doesn't involve adding Teensy-specific code to SdFat... and I'm going on the assumption that won't be so desirable, right?

And yes, it is a shame Arduino didn't design a better system. They really had the opportunity to think about "File" when they added the Bridge library for Arduino Yun, which accesses files on the Linux side of that board. Sadly they went with a kludge to define it 2 different ways depending on which library was used (you can't use both in the same program), rather than putting in the work to create an abstraction layer like ESP8266 did for FS.h (as far as I know ESP8266 was first, then copied by ESP32 and later by me for Teesny). I hope eventually all Arduino compatible boards will migrate to a FS.h abstraction layer so all programs using File and work interchangeably with all libraries providing File instances.

greiman commented 2 years ago

At this point I accept that SdFat will have conflicts on many systems. I will just tell users too bad you can't use SdFat or maybe some of its features.

The File class was introduced in a 2009 wrapper by SparkFun. I didn't like the idea, The name File is too generic, too much like Unix/Linux FILE. I had a SdFile class derived from Print and should have just derived SdFile from Stream. I added the File class to SdFat because so many programs used the Arduino wrapper.

I am now in a major restructuring of the core of SdFat. I am removing all Arduino dependencies to produce a "library" that is easy to use on any RTOS and the Raspberry Pi Pico system. I am testing with mbed, ChibiOS and the Pico RP2040 systems. I am also making it easier to use the core with other block devices like USB drives.

The Arduino stuff will just be a very simple template class so I suspect something could be done with Teensy to allow File.

PaulStoffregen commented 2 years ago

I'm excited to hear you're working on a major redesign. Really curious to hear more.

But I'm also a little dismayed you're testing on other systems but not Teensy, which today may be the only widely used system actually using SdFat (version 2) as the default library for SD card and other disk access.

PaulStoffregen commented 2 years ago

As a quick fix, in the copy of SdFat we're shipping to Teensy users, I've edited all the examples to "#define SD_FAT_TYPE 3". While this doesn't solve the fundamental conflict, hopefully it will help improve user experience.

greiman commented 2 years ago

That sounds like the good simple solution.

I never guessed SdFat would be around after so many years. It started as a toy I threw together without much thought for FAT16 on a 168 Arduino. It has evolved in a random way. The Arduino reminded me of the first small computer I used, a PDP8.

At that time I was a research physicist designing/simulating a Clos network with 1Tb/sec throughput for the Atlas experiment at CERN using two tiers of 64 (8X8) 10Gb/sec Ethernet switches.

I was no longer allowed to program since I was a scientist so SdFat was a diversion with toy hardware.