You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Potential Regression on #4189: Incorrectly attempting to link flockfile #4646
Hi,
So I'm running into the same issue identified in #4189. I'm trying to compile fmt for use on a Raspberry Pi Pico 2 (with the official pico-sdk). I'm using the latest release (12.1.0).
I'm assuming the issue has to do with the function being defined in the headers but not any of the linked libraries (as this is for a micro-controller without an OS or, thus, any filesystems).
If there isn't a way to properly disable this using existing compile flags/checks, can one be added?
As a workaround I'm currently just commenting out these lines:
As a potential aside (not sure if it's relevant), I'm having a bit of difficulty fully debugging the issue because I can see the enable_if-style guard for has_flockfile checks for the existence of the f(un)lockfile functions, but functions of this name are literally defined within fmt, so I'm honestly not sure if it's the standard library headers or fmt itself that is creating the belief the f(un)lockfile functions exist.
I also can't make heads or tails of the functions within fmt of that name because they depend on functions named _(un)lock_file, and I wasn't able to figure out where they are supposed to be defined.
Most likely your system defines flockfile but doesn't provide an implementation - I think we've seen a broken system like this in the past. You can disable the file locking functionality by compiling with the FMT_USE_FALLBACK_FILE macro defined to 1.
@vitaut,
Sorry for the delay, but this is the error I'm getting.
/usr/lib/gcc/arm-none-eabi/14.2.1/../../../arm-none-eabi/bin/ld: ../_deps/fmt-build/libfmtd.a(format.cc.o): in function `fmt::v12::detail::file_print_buffer<__sFILE, void>::file_print_buffer(__sFILE*)':
/home/mvastola/src/Pico2-MultiTool/.cache/cpm-sources/fmt/c411/fmt/include/fmt/format-inl.h:1683:(.text._ZN3fmt3v126detail17file_print_bufferI7__sFILEvEC2EPS3_[_ZN3fmt3v126detail17file_print_bufferI7__sFILEvEC5EPS3_]+0x24): undefined reference to `flockfile'
/usr/lib/gcc/arm-none-eabi/14.2.1/../../../arm-none-eabi/bin/ld: ../_deps/fmt-build/libfmtd.a(format.cc.o): in function `fmt::v12::detail::file_print_buffer<__sFILE, void>::~file_print_buffer()':
/home/mvastola/src/Pico2-MultiTool/.cache/cpm-sources/fmt/c411/fmt/include/fmt/format-inl.h:1692:(.text._ZN3fmt3v126detail17file_print_bufferI7__sFILEvED2Ev[_ZN3fmt3v126detail17file_print_bufferI7__sFILEvED5Ev]+0x3c): undefined reference to `funlockfile'
If it matters, I'm using fmt via CPM. This is how I'm trying to add it.
CPMAddPackage(
NAMEfmtGIT_TAG12.1.0GITHUB_REPOSITORY"fmtlib/fmt"OPTIONSEXCLUDE_FROM_ALL"FMT_SYSTEM_HEADERS ON""FMT_UNICODE OFF""FMT_OS OFF"# disable using os libraries"FMT_MODULE OFF""FMT_TEST OFF""CMAKE_BUILD_TYPE Debug""BUILD_SHARED_LIBS OFF""FMT_INSTALL OFF""FMT_LIB_EXPORT ON""FMT_USE_CMAKE_MODULES ON""FMT_USE_FALLBACK_FILE 1""CMAKE_CXX_FLAGS -DFMT_USE_FALLBACK_FILE=1""CMAKE_VERBOSE_MAKEFILE ON"PATCHES# "${TOPLEVEL}/cmake/patches/fmt-disable-flock.diff""${TOPLEVEL}/cmake/patches/fmt-constexpr-static-format.diff"# fixed in next release"${TOPLEVEL}/cmake/patches/fmt-test-error.diff"
)
target_compile_definitions(fmtINTERFACEFMT_USE_FALLBACK_FILE=1)
target_compile_definitions(fmtPUBLICFMT_USE_FALLBACK_FILE=1)
As for the patch files, this is fmt-disable-flock.diff (which prevents the error):
diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h
index 945cb91..ba19164 100644
--- a/include/fmt/format-inl.h+++ b/include/fmt/format-inl.h@@ -1478,9 +1478,9 @@ template <typename F> auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) {
template <typename F = FILE, typename Enable = void>
struct has_flockfile : std::false_type {};
-template <typename F>-struct has_flockfile<F, void_t<decltype(flockfile(&std::declval<F&>()))>>- : std::true_type {};+//template <typename F>+//struct has_flockfile<F, void_t<decltype(flockfile(&std::declval<F&>()))>>+// : std::true_type {};
// A FILE wrapper. F is FILE defined as a template parameter to make system API
// detection work.
This is fmt-test-error.diff, which is diff I created to ensure FMT_USE_FALLBACK_FILE is being set.
diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h
index 945cb912..49aaf5b3 100644
--- a/include/fmt/format-inl.h+++ b/include/fmt/format-inl.h@@ -1475,6 +1475,9 @@ template <typename F> auto getc_unlocked(F* f) -> decltype(_fgetc_nolock(f)) {
}
#endif
+#if !FMT_USE_FALLBACK_FILE+#error FMT_USE_FALLBACK_FILE is not set+#endif
template <typename F = FILE, typename Enable = void>
struct has_flockfile : std::false_type {};
As an aside, is there any way FMT_USE_FALLBACK_FILE (or something similar) could be made a CPM-compatible config option? Something I noticed was that I needed to manually set
target_compile_definitions(fmt PUBLIC FMT_USE_FALLBACK_FILE=1)
to ensure fmt/format-inl.h was only ever included with FMT_USE_FALLBACK_FILE.
To be clear though, I'm getting the linker error even with that target_compile_definitions(fmt PUBLIC ...) line.
(FWIW, I can't figure out why the INTERFACE definition doesn't work by itself, but that's a separate issue.)
Ah, you are right - FMT_USE_FALLBACK_FILE is insufficient to remove the use of flockfile. I think we should add a separate macro to control it, FMT_USE_FLOCKFILE, to force the has_flockfile trait to false even if there is a declaration of flockfile. A PR to do it is welcome.
As an aside, is there any way FMT_USE_FALLBACK_FILE (or something similar) could be made a CPM-compatible config option?
I am not familiar with CPM. Do you mean adding a CMake option in addition to the macro?
Ah, you are right - FMT_USE_FALLBACK_FILE is insufficient to remove the use of flockfile. I think we should add a separate macro to control it, FMT_USE_FLOCKFILE, to force the has_flockfile trait to false even if there is a declaration of flockfile. A PR to do it is welcome.
Cool. I'll try my hand at a PR then.
I am not familiar with CPM. Do you mean adding a CMake option in addition to the macro?
My bad, I thought CPM was officially supported (I think they give an example of using fmt rather than the reverse.) But yes, that's part of it. The other part is making it so when this library is built with FMT_USE_FALLBACK_FILE and imported into another CMake project,
target_compile_definitions(fmt PUBLIC FMT_USE_FALLBACK_FILE=1)
Thanks for the confirmation, I'm glad this is resolved now although I would recommend reporting an issue to pico-sdk.
Now that that''s in, would you accept a patch to add a corresponding CMake option?
I don't think we should duplicate configuration macros as CMake options. Setting the former with target_compile_definitions seems relatively straightforward. Maybe CPM can provide a mechanism to make it even easier.
I don't think we should duplicate configuration macros as CMake options. Setting the former with target_compile_definitions seems relatively straightforward. Maybe CPM can provide a mechanism to make it even easier.
@vitaut, Interesting. Is there any particular reason why? I would argue:
the fact FMT_USE_FALLBACK_FILE (and now FMT_USE_FLOCKFILE) aren't exposed as options (and actually don't seem to be documented anywhere at all), makes using this library unnecessarily more difficult for developers building for platforms where the default values don't compile/link, and having to set these macros manually can give the impression they are hacky/experimental/unreliable/unsupported/intentionally buried.
Ideally, FMT_USE_FLOCKFILE shouldn't need to be set manually at all: CMake should be testing if the f(un)lockfile symbols are available and automatically setting this macro accordingly (unless its manually overridden).
When building this library separately/directly on the command line, the ability to set these macros is somewhat buried and necessitates doing something like -DCMAKE_CXX_FLAGS=-DFMT_USE_FALLBACK_FILE=1.
If the option FMT_INSTALL is set, fmt.pc and fmt-config.cmake files are generated, and those files should contain any macros that were set when the library was built. Otherwise, the headers fmt was built with and the headers fmt is being invoked with will be effectively effectively different, leading to all sorts of potential hard-to-trace problems.
This library literally explicitly boasts "Portability" and "Ease of Use" among its selling points.
FMT_USE_FLOCKFILE is a temporary workaround for an external bug (which I’ve suggested reporting upstream), not a general configuration knob. It exists to unblock affected users, not as a long-term portability switch.
I don’t think it makes sense to duplicate internal configuration macros as CMake options. That increases maintenance surface and ties configuration to one build system, while the same functionality is already available in a form which works everywhere.
I’m open to making it easier to pass a set of configuration macros and documenting them better, but I don’t think exposing each individual macro as a CMake option is the right approach.
Hrm. I guess it depends what you mean by "internal configuration macros". I also didn't know this was thought up to be an external bug.
Can you link me to the issue with this information or else point me to the source of the upstream bug? I'm not sure it's actually a bug in my case, but I would definitely like to learn more.
I obviously don't think internal macros that aren't expected to be configured/changed for normal usage need to be exposed anywhere, but I'm not sure this issue is as ephemeral as we'd like. (I'm happy to investigate.)
As for "making it easier to pass a set of configuration macros", did you have any alternative methods in mind?
Can you link me to the issue with this information or else point me to the source of the upstream bug?
It is clearly a bug because on your system flockfile is declared but not implemented and as a result any code that tries to use this standard POSIX API will get a linkage error. I don't know if there is already an issue open and was suggesting you to open it against pico-sdk which is the likely culprit here:
I would recommend reporting an issue to pico-sdk
As for "making it easier to pass a set of configuration macros", did you have any alternative methods in mind?
Something like a single CMake option to pass any additional configuration macros although it is quite likely that this is already solvable on CMake level by other means.
It is clearly a bug because on your system flockfile is declared but not implemented and as a result any code that tries to use this standard POSIX API will get a linkage error. I don't know if there is already an issue open and was suggesting you to open it against pico-sdk which is the likely culprit here:
@vitaut ah, I see were the disconnect is. The pico-sdk isn't where the declaration lies. The declaration is in newlib, which isn't an sdk but rather a popular implementation of libc for embedded systems.
newlib is designed such that the platform's SDK (in this case, pico-sdk) provides the glue between the hardware and newlib. Since newlib is package in its own right that is installed separately, the newlib libc headers are installed system-wide and aren't unique to any particular target architecture.
As far as I can tell, it's by design that the function is declared but not defined. I would be utterly shocked if the newlib maintainers were willing to entertain putting the declaration behind a macro or something because the logical extension of that decision would be to do the same for each and every libc function that was possibly not supported by the underlying architecture. Even then, that would require replicating all of those macros when building fmt, and that would make building more complicated than a simple portability switch.
Ultimately, this isn't an upstream bug (at least in my case; it's possible the other times this was brought up it was), and therefore there's nothing temporary about this workaround and there's nothing for the pico-sdk maintainers to do here.
I would argue there's a reason build systems like autoconf exist, and why CMake has try_compile and similar functions: headers alone are often not sufficient to determine whether certain functions are usable on a given platform. I would argue it's for related reasons that compilers and linkers, by default, don't raise warnings or errors when a function is declared (even outside of system headers, in the case of g++, as I just discovered) but not defined.
Given this, I would ask you to reconsider your previous assumption that FMT_USE_FLOCKFILE isn't a long-term portability switch and the conclusions that flowed from that: there should be an actual cmake option, as well as, ideally, a config-time test to determine if the option should be set if a value isn't explicitly set. This is exactly what build systems are for, and if someone isn't using CMake they can always set the macro manually without issue.
It doesn't matter if the bug is in newlib or pico-sdk.
Even then, that would require replicating all of those macros when building fmt, and that would make building more complicated than a simple portability switch.
It wouldn't because we are detecting presence of these function and the only reason why it doesn't work is that newlib is providing a broken declaration in this case.
Ultimately, this isn't an upstream bug
it is.
Please report it upstream and once it is done we can discuss the next steps.
It doesn't matter if the bug is in newlib or pico-sdk.
What I'm saying is that newlib is designed to work with may platforms, and some of them do support those methods, so newlib needs to have them declared for platforms where they make sense.
Even then, that would require replicating all of those macros when building fmt, and that would make building more complicated than a simple portability switch.
It wouldn't because we are detecting presence of these function and the only reason why it doesn't work is that newlib is providing a broken declaration in this case.
I think you misunderstood me.. I know fmt checks if newlib declares the function. What I'm saying is newlib can't know to not declare the function because it's designed not to know. It relies on the hardware sdk and/or operating system to implement low-level functions it depends on. Because it (by design) isn't packaged or built individually for specific target platforms, it has no way to know the target supports any given feature.
What I am saying is that the only way it could possibly conditionally declare f(un)lockfile would be to put it behind a macro, and -- even if it did so -- that macro would then need to be defined appropriately for the target platform, and would therefore need to set when fmt is being built.
Maybe this is a stupid question, but what leads you to believe that providing a declaration that doesn't exist makes newlib/pico-sdk broken? I agree this would be the case if we were talking about a typical software library (like fmt), but I don't see how something like newlib could be expected to meet that standard.
Please report it upstream and once it is done we can discuss the next steps.
I'm willing to do this, but -- given the foregoing -- can you be more specific as to what you expect them to do? The only feasible way I can see for newlib to not declare this would be to put it behind a macro, but it would still be portability switch.
@vitaut, for whatever it's worth, to sanity-check my argument, I just downloaded the official Arm GNU Toolchain for target arm-none-eabi, (which is the same target triple as my Pico 2W) and wrote a super-simple test program:
(For reference, if it matters, I downloaded x86_64 Linux hosted version, as that is my OS.)
When I compiled it without linking (with arm-none-eabi-g++ --std=c++23 -c test.cpp), it succeeded without error, meaning, of course, that all the functions used were declared.
However, when I tried to compile it into an executable (with arm-none-eabi-g++ --std=c++23 -o test test.cpp), this is the output I got:
Expand here
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libstdc++.a(fs_ops.o): in function `std::filesystem::resize_file(std::filesystem::__cxx11::path const&, unsigned long long)':
fs_ops.cc:(.text._ZNSt10filesystem11resize_fileERKNS_7__cxx114pathEy+0x7c): undefined reference to `truncate'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libstdc++.a(fs_ops.o): in function `std::filesystem::resize_file(std::filesystem::__cxx11::path const&, unsigned long long, std::error_code&)':
fs_ops.cc:(.text._ZNSt10filesystem11resize_fileERKNS_7__cxx114pathEyRSt10error_code+0x3c): undefined reference to `truncate'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-exit.o): in function `exit':
exit.c:(.text.exit+0x28): undefined reference to `_exit'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-abort.o): in function `abort':
abort.c:(.text.abort+0x10): undefined reference to `_exit'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-closer.o): in function `_close_r':
closer.c:(.text._close_r+0x18): undefined reference to `_close'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-fstatr.o): in function `_fstat_r':
fstatr.c:(.text._fstat_r+0x1c): undefined reference to `_fstat'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-lseekr.o): in function `_lseek_r':
lseekr.c:(.text._lseek_r+0x24): undefined reference to `_lseek'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-openr.o): in function `_open_r':
openr.c:(.text._open_r+0x24): undefined reference to `_open'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-readr.o): in function `_read_r':
readr.c:(.text._read_r+0x24): undefined reference to `_read'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0x1c): undefined reference to `_kill'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x4): undefined reference to `_getpid'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-sbrkr.o): in function `_sbrk_r':
sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-unlinkr.o): in function `_unlink_r':
unlinkr.c:(.text._unlink_r+0x18): undefined reference to `_unlink'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-writer.o): in function `_write_r':
writer.c:(.text._write_r+0x24): undefined reference to `_write'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-isattyr.o): in function `_isatty_r':
isattyr.c:(.text._isatty_r+0x18): undefined reference to `_isatty'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-linkr.o): in function `_link_r':
linkr.c:(.text._link_r+0x1c): undefined reference to `_link'
/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld:/usr/local/arm-gnu-toolchain-15.2.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/libc.a(libc_a-statr.o): in function `_stat_r':
statr.c:(.text._stat_r+0x1c): undefined reference to `_stat'
collect2: error: ld returned 1 exit status
As you can see, practically every symbol used by this simple program was not defined. This isn't awfully surprising -- after all, this toolchain is for bare-metal platforms, so of course functions relating to file systems and process management aren't defined, but this illustrates the exact point I was making:
On an embedded platform, standard/low-level libraries like libc and libstdc++ rely on external, platform-supplied functions to implement much of their APIs. And they are shipped without knowing if implementations for specific platform functions ultimately can (or do) exist at link-time.
In this example, obviously I made no attempts to link in platform-provided code. If I had, functions like _write and _close (at least as they pertain to STDOUT) would have been defined. But that has no bearing on the filesystem methods, because the most the platform can do once a function has been declared is to implement it (presumably to always return an error) and that would just lead fmt to invoke the function, believing it to be supported.
Again, I'm willing to reach out to newlib if that will help things (and I promise to stop bothering you and do that if you confirm that's what you want), but I wanted to see if there's any chance of us getting on the same page first, because my understanding of what you want them to do doesn't seem feasible.
PS: If you want to duplicate compiling my test program, below is the Dockerfile I used that may simplify things:
I think declaring functions without actually implementing them is really broken and should be fixed but I'm open to a PR to set FMT_USE_FLOCKFILE to 0 on newlib, provided that it's easy to detect.
@vitaut fyi, I put up #4737 and continued this discussion there. Hoping for some feedback as to if you're okay with the direction I went in before I finalize.
Activity
vitaut commentedon Jan 7, 2026
Please post the full error that you get.
vitaut commentedon Jan 7, 2026
Most likely your system defines
flockfilebut doesn't provide an implementation - I think we've seen a broken system like this in the past. You can disable the file locking functionality by compiling with theFMT_USE_FALLBACK_FILEmacro defined to 1.vitaut commentedon Jan 10, 2026
Closing since it doesn't look like a bug in {fmt} and there is a way to workaround it on problematic systems.
mvastola commentedon Feb 3, 2026
@vitaut,
Sorry for the delay, but this is the error I'm getting.
If it matters, I'm using
fmtvia CPM. This is how I'm trying to add it.As for the patch files, this is
fmt-disable-flock.diff(which prevents the error):This is
fmt-test-error.diff, which is diff I created to ensureFMT_USE_FALLBACK_FILEis being set.As an aside, is there any way
FMT_USE_FALLBACK_FILE(or something similar) could be made a CPM-compatible config option? Something I noticed was that I needed to manually setto ensure
fmt/format-inl.hwas only ever included withFMT_USE_FALLBACK_FILE.To be clear though, I'm getting the linker error even with that
target_compile_definitions(fmt PUBLIC ...)line.(FWIW, I can't figure out why the
INTERFACEdefinition doesn't work by itself, but that's a separate issue.)vitaut commentedon Feb 3, 2026
Ah, you are right -
FMT_USE_FALLBACK_FILEis insufficient to remove the use offlockfile. I think we should add a separate macro to control it,FMT_USE_FLOCKFILE, to force thehas_flockfiletrait tofalseeven if there is a declaration offlockfile. A PR to do it is welcome.I am not familiar with CPM. Do you mean adding a CMake option in addition to the macro?
mvastola commentedon Feb 3, 2026
Cool. I'll try my hand at a PR then.
My bad, I thought CPM was officially supported (I think they give an example of using
fmtrather than the reverse.) But yes, that's part of it. The other part is making it so when this library is built with FMT_USE_FALLBACK_FILE and imported into another CMake project,or its equivalent comes pre-set.
mvastola commentedon Feb 3, 2026
To clarify, would
FMT_USE_FLOCKFILEbe set to0(and therefore default to1) to forcehas_flockfileto false? I'm a little confused by the naming.Could you also please explain what
FMT_USE_FALLBACK_FILEis intended to do? (For the CMake option description, as well as so I can understand haha).Thanks,
Mike
vitaut commentedon Feb 3, 2026
Yes
Let's not introduce CMake options yet.
FMT_USE_FALLBACK_FILEdisables detection of knownFILElayouts and related optimizations.mvastola commentedon Feb 3, 2026
Ah ok. Then the PR is pretty trivial. Will be done in 5.
Add FMT_USE_FLOCKFILE macro to force has_flockfile trait to false
mvastola commentedon Feb 3, 2026
Pushed.
Are there CI tests that need to be run? Or specific tests I should run locally?(Nevermind. They just took a minute to show.)Also, should I be writing unit tests for this? (I don't see any for
FMT_USE_FALLBACK_FILE)5 remaining items
mvastola commentedon Feb 3, 2026
Cool. I can confirm master is working for me on my end.
Now that that''s in, would you accept a patch to add a corresponding CMake option?
vitaut commentedon Feb 4, 2026
Thanks for the confirmation, I'm glad this is resolved now although I would recommend reporting an issue to pico-sdk.
I don't think we should duplicate configuration macros as CMake options. Setting the former with
target_compile_definitionsseems relatively straightforward. Maybe CPM can provide a mechanism to make it even easier.mvastola commentedon Feb 7, 2026
@vitaut, Interesting. Is there any particular reason why? I would argue:
FMT_USE_FALLBACK_FILE(and nowFMT_USE_FLOCKFILE) aren't exposed as options (and actually don't seem to be documented anywhere at all), makes using this library unnecessarily more difficult for developers building for platforms where the default values don't compile/link, and having to set these macros manually can give the impression they are hacky/experimental/unreliable/unsupported/intentionally buried.FMT_USE_FLOCKFILEshouldn't need to be set manually at all: CMake should be testing if thef(un)lockfilesymbols are available and automatically setting this macro accordingly (unless its manually overridden).-DCMAKE_CXX_FLAGS=-DFMT_USE_FALLBACK_FILE=1.FMT_INSTALLis set,fmt.pcandfmt-config.cmakefiles are generated, and those files should contain any macros that were set when the library was built. Otherwise, the headersfmtwas built with and the headersfmtis being invoked with will be effectively effectively different, leading to all sorts of potential hard-to-trace problems.vitaut commentedon Feb 14, 2026
FMT_USE_FLOCKFILEis a temporary workaround for an external bug (which I’ve suggested reporting upstream), not a general configuration knob. It exists to unblock affected users, not as a long-term portability switch.I don’t think it makes sense to duplicate internal configuration macros as CMake options. That increases maintenance surface and ties configuration to one build system, while the same functionality is already available in a form which works everywhere.
I’m open to making it easier to pass a set of configuration macros and documenting them better, but I don’t think exposing each individual macro as a CMake option is the right approach.
mvastola commentedon Feb 22, 2026
Hrm. I guess it depends what you mean by "internal configuration macros". I also didn't know this was thought up to be an external bug.
Can you link me to the issue with this information or else point me to the source of the upstream bug? I'm not sure it's actually a bug in my case, but I would definitely like to learn more.
I obviously don't think internal macros that aren't expected to be configured/changed for normal usage need to be exposed anywhere, but I'm not sure this issue is as ephemeral as we'd like. (I'm happy to investigate.)
As for "making it easier to pass a set of configuration macros", did you have any alternative methods in mind?
vitaut commentedon Feb 28, 2026
It is clearly a bug because on your system
flockfileis declared but not implemented and as a result any code that tries to use this standard POSIX API will get a linkage error. I don't know if there is already an issue open and was suggesting you to open it against pico-sdk which is the likely culprit here:Something like a single CMake option to pass any additional configuration macros although it is quite likely that this is already solvable on CMake level by other means.
mvastola commentedon Feb 28, 2026
@vitaut ah, I see were the disconnect is. The
pico-sdkisn't where the declaration lies. The declaration is in newlib, which isn't an sdk but rather a popular implementation oflibcfor embedded systems.newlib is designed such that the platform's SDK (in this case,
pico-sdk) provides the glue between the hardware and newlib. Since newlib is package in its own right that is installed separately, the newlib libc headers are installed system-wide and aren't unique to any particular target architecture.As far as I can tell, it's by design that the function is declared but not defined. I would be utterly shocked if the newlib maintainers were willing to entertain putting the declaration behind a macro or something because the logical extension of that decision would be to do the same for each and every libc function that was possibly not supported by the underlying architecture. Even then, that would require replicating all of those macros when building fmt, and that would make building more complicated than a simple portability switch.
Ultimately, this isn't an upstream bug (at least in my case; it's possible the other times this was brought up it was), and therefore there's nothing temporary about this workaround and there's nothing for the pico-sdk maintainers to do here.
I would argue there's a reason build systems like autoconf exist, and why CMake has
try_compileand similar functions: headers alone are often not sufficient to determine whether certain functions are usable on a given platform. I would argue it's for related reasons that compilers and linkers, by default, don't raise warnings or errors when a function is declared (even outside of system headers, in the case of g++, as I just discovered) but not defined.Given this, I would ask you to reconsider your previous assumption that FMT_USE_FLOCKFILE isn't a long-term portability switch and the conclusions that flowed from that: there should be an actual cmake option, as well as, ideally, a config-time test to determine if the option should be set if a value isn't explicitly set. This is exactly what build systems are for, and if someone isn't using CMake they can always set the macro manually without issue.
vitaut commentedon Mar 11, 2026
It doesn't matter if the bug is in newlib or pico-sdk.
It wouldn't because we are detecting presence of these function and the only reason why it doesn't work is that newlib is providing a broken declaration in this case.
it is.
Please report it upstream and once it is done we can discuss the next steps.
mvastola commentedon Mar 12, 2026
What I'm saying is that newlib is designed to work with may platforms, and some of them do support those methods, so newlib needs to have them declared for platforms where they make sense.
I think you misunderstood me.. I know fmt checks if newlib declares the function. What I'm saying is newlib can't know to not declare the function because it's designed not to know. It relies on the hardware sdk and/or operating system to implement low-level functions it depends on. Because it (by design) isn't packaged or built individually for specific target platforms, it has no way to know the target supports any given feature.
What I am saying is that the only way it could possibly conditionally declare
f(un)lockfilewould be to put it behind a macro, and -- even if it did so -- that macro would then need to be defined appropriately for the target platform, and would therefore need to set when fmt is being built.Maybe this is a stupid question, but what leads you to believe that providing a declaration that doesn't exist makes newlib/pico-sdk broken? I agree this would be the case if we were talking about a typical software library (like fmt), but I don't see how something like newlib could be expected to meet that standard.
I'm willing to do this, but -- given the foregoing -- can you be more specific as to what you expect them to do? The only feasible way I can see for newlib to not declare this would be to put it behind a macro, but it would still be portability switch.
mvastola commentedon Mar 12, 2026
@vitaut, for whatever it's worth, to sanity-check my argument, I just downloaded the official Arm GNU Toolchain for target
arm-none-eabi, (which is the same target triple as my Pico 2W) and wrote a super-simple test program:(For reference, if it matters, I downloaded
x86_64 Linux hostedversion, as that is my OS.)When I compiled it without linking (with
arm-none-eabi-g++ --std=c++23 -c test.cpp), it succeeded without error, meaning, of course, that all the functions used were declared.However, when I tried to compile it into an executable (with
arm-none-eabi-g++ --std=c++23 -o test test.cpp), this is the output I got:Expand here
As you can see, practically every symbol used by this simple program was not defined. This isn't awfully surprising -- after all, this toolchain is for bare-metal platforms, so of course functions relating to file systems and process management aren't defined, but this illustrates the exact point I was making:
On an embedded platform, standard/low-level libraries like
libcandlibstdc++rely on external, platform-supplied functions to implement much of their APIs. And they are shipped without knowing if implementations for specific platform functions ultimately can (or do) exist at link-time.In this example, obviously I made no attempts to link in platform-provided code. If I had, functions like
_writeand_close(at least as they pertain to STDOUT) would have been defined. But that has no bearing on the filesystem methods, because the most the platform can do once a function has been declared is to implement it (presumably to always return an error) and that would just lead fmt to invoke the function, believing it to be supported.Again, I'm willing to reach out to newlib if that will help things (and I promise to stop bothering you and do that if you confirm that's what you want), but I wanted to see if there's any chance of us getting on the same page first, because my understanding of what you want them to do doesn't seem feasible.
PS: If you want to duplicate compiling my test program, below is the Dockerfile I used that may simplify things:
Click to expand
vitaut commentedon Mar 28, 2026
I think declaring functions without actually implementing them is really broken and should be fixed but I'm open to a PR to set
FMT_USE_FLOCKFILEto 0 on newlib, provided that it's easy to detect.[DRAFT, WIP] Detect presence of flockfile
mvastola commentedon Apr 16, 2026
@vitaut fyi, I put up #4737 and continued this discussion there. Hoping for some feedback as to if you're okay with the direction I went in before I finalize.
fmtlib: update to 12.2.0.
fmtlib: update to 12.2.0.