Library.properties, ldflags parameter

Hello,

According to the docs (library.properties file format, Arduino website), it is possible to pass extra flags to the linker using "ldflags=" parameter.

And there is an example, showing "ldflags=-lm" (quite real-life example i think).

So I changed my library.properties file directly (~/SketchDirectory/libraries/LibraryName/library.properties)

And nothing happened.

Am I missing something? How this ldflags is supposed to be used?

Adding linker flags directly to "ld_flags" file (which is a part of the ESP-IDF, precompiled libs) did the job.

The only time I saw that done it was placed in the personal library folder and the file name was platform.local.txt. The contents were

compiler.libraries.ldflags=

Does that mean I need to create platform.local.txt and place it in libraries/LibraryName/src/ ?

Yes, but in libraries/Library where Library is your library.

Thanks for the answer but still no flags is passed to the linker.

Linker string doesnt change:

"E:\\Arduino15\\packages\\esp32\\tools\\esp-x32\\2405/bin/xtensa-esp32s3-elf-g++" "-Wl,--Map=C:\\Users\\Вячеслав\\AppData\\Local\\arduino\\sketches\\8C8C8D35D61C1CDA4B2BFF416A5DC251/standalone_shell.ino.map" "-LE:\\Arduino15\\packages\\esp32\\tools\\esp32-arduino-libs\\idf-release_v5.3-489d7a2b-v1\\esp32s3/lib" "-LE:\\Arduino15\\packages\\esp32\\tools\\esp32-arduino-libs\\idf-release_v5.3-489d7a2b-v1\\esp32s3/ld" "-LE:\\Arduino15\\packages\\esp32\\tools\\esp32-arduino-libs\\idf-release_v5.3-489d7a2b-v1\\esp32s3/qio_opi" -Wl,--wrap=esp_panic_handler "@E:\\Arduino15\\packages\\esp32\\tools\\esp32-arduino-libs\\idf-release_v5.3-489d7a2b-v1\\esp32s3/flags/ld_flags" "@E:\\Arduino15\\packages\\esp32\\tools\\esp32-arduino-libs\\idf-release_v5.3-489d7a2b-v1\\esp32s3/flags/ld_scripts" -Wl,--start-group "C:\\Users\\Вячеслав\\AppData\\Local\\arduino\\sketches\\8C8C8D35D61C1CDA4B2BFF416A5DC251\\sketch\\standalone_shell.ino.cpp.o" "C:\\Users\\Вячеслав\\AppData\\Local\\arduino\\sketches\\8C8C8D35D61C1CDA4B2BFF416A5DC251\\libraries\\espshell\\espshell.c.o" "C:\\Users\\Вячеслав\\AppData\\Local\\arduino\\cores\\cf251abcaec369c62915b5c6c3720754\\core.a" "@E:\\Arduino15\\packages\\esp32\\tools\\esp32-arduino-libs\\idf-release_v5.3-489d7a2b-v1\\esp32s3/flags/ld_libs" -Wl,--end-group -Wl,-EL -o "C:\\Users\\Вячеслав\\AppData\\Local\\arduino\\sketches\\8C8C8D35D61C1CDA4B2BFF416A5DC251/standalone_shell.ino.elf"

platform.local.txt:

compiler.libraries.ldflags=-Wl,--wrap=vTaskDelete -Wl,--wrap=xTaskCreatePinnedToCore -Wl,--wrap=xTaskCreateStaticPinnedToCore

Thats sad.

May be it is good idea for arduino-cli is to accept special files (if found in the sketch source tree) like c_flags, cpp_flags, ld_flags; and use their content for gcc,g++,ld

There were numerous requests for passing flags to a compiler. With *_flags files all these requests will be closed :slight_smile:

Maybe the example I was using (not mine) is wrong, so just check the Arduino.cc site under documentation, I have seen it there somewhere.

Hi @vvb333007.

Is your library a "precompiled" library?

Unfortunately the Arduino Library Specification doesn't effectively communicate that the ldflags property is only used when compiling precompiled libraries.

There is more information about precompiled libraries here:

https://arduino.github.io/arduino-cli/latest/library-specification/#precompiled-binaries

You can use the compiler.c.elf.extra_flags property instead of compiler.libraries.ldflags. This property is referenced in the recipe.c.combine.pattern, which is used as the template for the linking command:

https://arduino.github.io/arduino-cli/latest/platform-specification/#recipes-for-linking

So any flags you put in your definition of the compiler.c.elf.extra_flags property will be included in the linking command.

Thanks for the reply. No my library is not precompiled. Neither flags works. Even if I put random garbage into platform.local.txt.

Seems like I can not just place my platform.local.txt to the library folder:
(tried libraries/ , libraries/LibraryName/ , libraries/LibraryName/src, tried to rename the platform.local.txt to platform.txt- all was useless)

platform.local.txt

Introduced in Arduino IDE 1.5.7. This file can be used to override properties defined in platform.txt or define new properties without modifying platform.txt (e.g. when platform.txt is tracked by a version control system). It must be placed in the same folder as the platform.txt it supplements.

From the text above it is absolutely unclear where this file should be put. Previous paragraph says that platform.txt used to override local settings, while next paragraph says platform.local.txt used to override the global platform.txt

Allright, seems like it is not doable in a simple way. Seems like you can't specify LD_FLAGS (as well as C_FLAGS) when compiling library.

Unlikr other Arduino IDE's imperfections, inability to pass compiler/linker flags is a serious disadvantage. The simpliest way (as I see it) is just use files "c_flags", "cc_flags", "ld_flags", "as_flags" if found in the source tree.

If found - simple add their contents to command lines for the compiler, linker and assembler.

There may be special optimization options required for compiling. There may be special warnings enabled (for example, checking arguments of a printf-like function), and so on and so on. Now we can not handle these cases. What if library requires linking with another library? We can't even add "-lm" ;(.

That is correct. This is a platform configuration file. Not a library configuration file. You must put it in the platform in the same folder as the platform.txt file, not in the library.

Another thing to note is that you must restart Arduino IDE (or run the "Arduino: Restart Daemon" command from the "Command Palette") after you make any changes to the platform configuration files (including adding or editing *.local.txt files), as Arduino IDE won't recognize any changes you make while it is running.

Maybe it is an issue for you, but the thousands of other library developers get by without it without any problem.

It is true that, over the years, we have received a significant number of requests for the ability for the user to make global macro definitions in order to configure libraries and platforms for a given sketch project. However, very few library developers have reported needing such a thing for the persistent configuration of the library. This makes me wonder if this is actually an "XY problem", where injecting linker flags via the library configuration is the "Y", but some more achievable solution is available for whatever your true goal is ("X").

The forum helpers might be able to suggest such a solution if you provide a detailed description of exactly why you want to inject linker flags. Your other topic on the subject would be the appropriate place to do that: