Using external fmt library
I use my own copy of the fmt library so that I have the latest version and I can more easily decouple fmt from spdlog. (I use the fmt library in my code in places where I do not use spdlog.) I ran into some conflicts (unresolved externals) when doing this, if the external version of fmt that I am using does not match the bundled version and I do not #include the external version of fmt before I #include <spdlog/spdlog.h>.
Has this scenario been tested? I'm doing some more testing to see if I can isolate the problem to my own code or spdlog.
Hi you need to #define SPDLOG_FMT_EXTERNAL before including spdlog.h
Thanks for the prompt response, Gabi. I uncommented the #define for SPDLOG_FMT_EXTERNAL in tweakme.h. Should that not (effectively) do the same thing?
Yes, its the same of course. As you noted the problem is probably because different versions of fmt.. Try to upgrade your fmt version.
I was already using a pretty new version of fmt, but I found out what the real problem is. I wanted to share in case others had the same issue.
In my case, I am using the external fmt library in header only mode, and FMT_HEADER_ONLY was not being defined. If I #define FMT_HEADER_ONLY before including spdlog.h, then all is fine.
I have a suggestion. It may be a good idea to add a section to tweakme.h (right after the SPDLOG_FMT_EXTERNAL section) for FMT_HEADER_ONLY, like the lines shown below. This way, people that want to use an external copy of fmt in header only mode do not have to #define FMT_HEADER_ONLY in every file prior to including <spdlog/spdlog.h>, which is somewhat tedious.
/////////////////////////////////////////////////////////////////////////////// // Uncomment to use the external copy of the fmt library in header only mode. // If SPDLOG_FMT_EXTERNAL is enabled above, enabling FMT_HEADER_ONLY below will // cause the fmt library to include <fmt/format.cc> for you so that you don't // need to add it to your project. If SPDLOG_FMT_EXTERNAL is disabled, then // FMT_HEADER_ONLY has no effect. // //#define FMT_HEADER_ONLY ///////////////////////////////////////////////////////////////////////////////
I am not sure.. spdlog only defines FMT_HEADER_ONLY if using the bundled lib. Otherwise it is totally out of its scope.. Users can put the FMT_HEADER_ONLY (or any other macros for that matter) wherever they see fit
I realize this is sort of a corner case and a vast majority of users probably use the bundled fmt library. The use case for me is whenever I download new versions of spdlog, I will have to reapply my changes to tweakme.h, and it's a lot easier to uncomment a line than to remember to paste in a #define FMT_HEADER_ONLY. If you are supporting an external version of fmt via a #define, I think you've opened the door a bit. You'll include fmt/format.h for me, but if I want to use it as header-only (not surprising as that is the way spdlog works), I have to #define FMT_HEADER_ONLY myself or it won't link, and the reason why isn't immediately obvious. Is that out of scope? Maybe, but I think it is possible to argue otherwise.
Sure, I could create another header file that defines both SPDLOG_FMT_EXTERNAL and FMT_HEADER_ONLY, then includes spdlog.h, but there is already built-in support in tweakme.h to define SPDLOG_FMT_EXTERNAL, which makes implementing my own solution less compelling.
I understand your viewpoint, and desire to resist scope creep. Ultimately, you will do what is best for spdlog. I'm just making you aware of the issue I encountered.
Those are valid points, but I am not sure why fmt must be used as header only when used externally. Are you sure it cannot be used otherwise? If I am recalling correctly, I tried it once (external, non header) and it was ok but not sure..If it really the case It should be fixed..
No, it doesn't have to be used as header-only, that's merely how I am choosing to use it.
I wrote a header (much like you did for the version of fmt that you bundle) that does a #define FMT_HEADER_ONLY, then #include fmt/format.h. I do that so that parts of my program that need fmt (but not spdlog) will use fmt in header-only mode. That's great, but when I go to use spdlog and I want to reuse my existing fmt library, it includes <fmt/format.h>, not my header. So, of course, it bypasses that mechanism.
In order to get FMT_HEADER_ONLY defined, the solutions I can readily see are to:
(1) add #define FMT_HEADER_ONLY to<fmt/format.h> (2) add #define FMT_HEADER_ONLY in every source file where I #include <spdlog/spdlog.h> (3) add #define FMT_HEADER_ONLY to tweakme.h (4) create another header like <spdlog/main.h> that does the #define FMT_HEADER_ONLY and then #include <spdlog/spdlog.h>.
In my case (1) is undesirable if I'm going to continually update fmt because it is easy to lose the change.
Option (2) is a lot of unnecessary duplication of effort.
I can do (3), but adding code to this file will get lost when I update spdlog and would require an external means of remembering what to add. It's true that uncommenting lines in tweakme.h would also get lost when updating spdlog, but uncommenting a couple of lines is far less burdensome than having to add lines yourself.
I can certainly do (4), and I concede that may even be the "right" solution for my situation (I'm still considering this). In this case though, I might as well #define FMT_HEADER_ONLY and #define SPDLOG_FMT_EXTERNAL both in my own header file and not modify tweakme.h at all, so I don't have to modify any spdlog source. The disadvantage is that sort of defeats the purpose of having a tweakme.h file to begin with.
I totally get your viewpoint -- if you allow users to hook up their own version of fmt, you don't really want to make assumptions about how it will be used. Plus, adding such an option opens the floodgates, so to speak, and you certainly don't want to be in the business of managing all of the fmt library's #defines in spdlog.
I just wanted to raise the idea with you, as if someone wants to have spdlog use an external version of fmt, it could cause issues if you set up fmt as header-only, like I did. I understand the issue now and can decide for myself what to do, but others may not realize what is happening in such a case.
Thanks for raising it. I believe the wiki would be a could place to document it.