Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working when using SdFat instead of SD library #38

Open
rmicmir opened this issue Apr 9, 2023 · 4 comments
Open

Not working when using SdFat instead of SD library #38

rmicmir opened this issue Apr 9, 2023 · 4 comments

Comments

@rmicmir
Copy link

rmicmir commented Apr 9, 2023

Hi!

Thanks for your work on this library!

I've been trying to use IniFile compining against SdFat instead the defaukt SD library. I've used the examples included in IniFile witout success.

  • Example "IniBrowseExample", without any modifications (compiling using SD.h library) is successful:

`#include <SD.h>

#include <SPI.h>
#include <IPAddress.h>
#include <IniFile.h>

// The select pin used for the SD card
[...]`

  • Example "IniBrowseExample" compiling using SdFat is not successful:
    `#define PREFER_SDFAT_LIBRARY 1

#include <SdFat.h>

#include <SPI.h>
#include <IPAddress.h>
#include <IniFile.h>

// The select pin used for the SD card
[...]`

Error:

tmp/arduino/sketches/BB2A6256ECD89697A9217989C990A171/sketch/IniBrowseExample.ino.cpp.o: In function IniFile::open()':
/home/user/Arduino/libraries/IniFile/src/IniFile.h:161: undefined reference to SD' /tmp/arduino/sketches/BB2A6256ECD89697A9217989C990A171/sketch/IniBrowseExample.ino.cpp.o: In function setup':
/tmp/.arduinoIDE-unsaved202339-2452-1rlkvoh.nh7h/IniBrowseExample/IniBrowseExample.ino:72: undefined reference to IniFile::IniFile(char const*, int, bool)' /tmp/.arduinoIDE-unsaved202339-2452-1rlkvoh.nh7h/IniBrowseExample/IniBrowseExample.ino:126: undefined reference to SD'
/tmp/.arduinoIDE-unsaved202339-2452-1rlkvoh.nh7h/IniBrowseExample/IniBrowseExample.ino:126: undefined reference to `SD'
collect2: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1`

  • Example "IniBrowseExample" compiling using SdFat with some extra options is neither successful:

`#define PREFER_SDFAT_LIBRARY 1

#include <SdFat.h>
SdFat SD;

#include <SPI.h>
#include <IPAddress.h>
#include <IniFile.h>

// The select pin used for the SD card
[...]`

Error:

/tmp/arduino/sketches/BB2A6256ECD89697A9217989C990A171/sketch/IniBrowseExample.ino.cpp.o: In function setup':
/tmp/.arduinoIDE-unsaved202339-2452-1rlkvoh.nh7h/IniBrowseExample/IniBrowseExample.ino:73: undefined reference to `IniFile::IniFile(char const*, int, bool)'
collect2: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1`

Is there any way to solve this? If I can help any way please let me know.

Thanks!

@stevemarple
Copy link
Owner

I've not used the SdFat library - the changes to support it have been contributed from users. I cannot confirm that the examples will work with it (I hope they do but I have never tried them that way). You'll need to make sure that the SdFat SD object is defined somewhere, since IniFile.h expects it to exist (from IniFile.cpp line 8: extern SdFat SD;). This is the cause of the undefined reference to SD error you report.

If you define #define PREFER_SDFAT_LIBRARY 1 when including IniFile.h in your example or main .ino file you had better make sure that macro is in effect when compiling IniFile.cpp. For now that is probably best achieved by editing IniFile.h so the macro is defined everywhere it is needed. If you don't have this right then the IniFile constructor parameter types will be wrong, as evidenced by the undefined reference to IniFile::IniFile(char const*, int, bool)` error message you report.

A real IDE will allow you to define a macro globally that is applied for all compilation units. For some stupid ideological reasons the Arduino developers think this is not necessary. I've lost count of the number of times these type of problems have arisen because of this. I even submitted a PR (arduino/arduino-builder#29) to fix it but it has not been accepted.

@rmicmir
Copy link
Author

rmicmir commented Apr 11, 2023

Hi

Thanks for your answer. I'm not an experienced developer and I don't fully understand your proposal. I cannot understand how editting the IniFile.h and including the PREFER_SDFAT_LIBRARY macro there this would be solved.

As far as I can understand the code, the SD and SdFat classes should be managed the same by IniFile. The PREFER_SDFAT_LIBRARY only affects which one is loaded, but the IniFile constructor is not affected by the macro, right?

Just to make you aware, my interest in using SdFat instead of SD is because I've read that SdFat makes a better energy management of the SD hardware, and I'm trying to minimize power consumption on a project.

Thanks again

@stevemarple
Copy link
Owner

I cannot understand how editting the IniFile.h and including the PREFER_SDFAT_LIBRARY macro there this would be solved.

You define PREFER_SDFAT_LIBRARY before including IniFile.h, which affects the mode_t typedef, which in turn affects the IniFile constructor parameter types but only when compiling the .ino file. IniFile.cpp is a separate compilation unit, and it isn't affected by your PREFER_SDFAT_LIBRARY #define. The linkage error arises because the two compilation units (your arduino .ino file, and IniFile.cpp) are out of sync. Put them in sync by having PREFER_SDFAT_LIBRARY defined for both; this is simplest by adding the macro to line 5 of IniFile.h.

@rmicmir
Copy link
Author

rmicmir commented Apr 18, 2023

Hi!

You are a genius :)

I've finally got it working after following your instructions but I had some problems I would like to share with you: your solution works only if "IniFile.h" and "IniFile.cpp" are on the same directory as my .ino files. I've also tried to create a subfolder "Lib" in my project, just to keep things in order, and I've placed the .h and .cpp files there properly referenced from my .ino file and that keeps yelling the same error.

Is that "subfolder inclusion" not working for any other reason? Can you explain me why? I think I would learn something else from you :)

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants