Fmt: Including fmt into DLL project

Created on 20 Feb 2018  Â·  12Comments  Â·  Source: fmtlib/fmt

Hi. I'm trying to implement fmt into my project. It's a DLL plugin for Cockos Reaper DAW.
Inside the header I've added:

include "../Library/fmt/format.h" // path to fmt c++ library

using namespace fmt;

But, when I'm trying to use anything from fmt, like:

std::string message = format("The answer is {}", 42);

or

std::string message = fmt::format("The answer is {}", 42);

I get lots of errors. Eight, to be precise. This is the first one:

Error   LNK2019 unresolved external symbol "void __cdecl fmt::internal::report_unknown_type(char,char const *)" (?report_unknown_type@internal@fmt@@YAXDPEBD@Z) referenced in function "private: void __cdecl fmt::BasicWriter<char>::write_double<double,struct fmt::FormatSpec>(double,struct fmt::FormatSpec const &)" (??$write_double@NUFormatSpec@fmt@@@?$BasicWriter@D@fmt@@AEAAXNAEBUFormatSpec@1@@Z)   reaper  D:\VisualStudio2015\reaper.obj  1   

I must mention that I'm editing someone else sources of the plugin, not starting from scratch, so sln file was already present there.
I've tried adding #include "stdafx.h" into the .cpp file, but stdafx.h isn't present in the sources folder, so, obviously, it throws an error.

How do I properly add fmt to my current project?

All 12 comments

You need to add format.cc in your project or compile the library in the header-only mode (define FMT_HEADER_ONLY.

Changing format.h to format.cc helped. Thanks a lot. I've watched your lecture at cppcon, to find out, why fmt didn't work for me. Even though I didn't find an answer in the video, it was still a great lecture. 😄
Centered alignment would also help, cause I'm gonna use fmt to display text on the LCD. Nifty feature.

The only thing I don't get, yet, is how to set up both width and precision, so that the output text would always be 8 chars long and aligned in center, no matter if it's "1" or "1234567890".

str += format("{:.8}", temp);
gives precision, but only works for strings, which are larger than 8 chars.
str += format("{:^8}", temp);
gives width, but outputs full string, even if it's larger than 8 chars.
str += format("{:^.8}", temp);
same as precision.

You can combine width, precision and center alignment as follows:

  fmt::format("{:^8.8}", "1234567890"); // returns "12345678"
  fmt::format("{:^8.8}", "1");          // returns "   1    "

Thanks! A bit off topic, but I'm also interested, if it's possible to omit some symbols from the input string, such as non-letters/special symbols/vowels with fmt, or should I use match instead?
For example, to convert "Hello, World! (foobar)" into 8 characters string "HellWrld", by going through the following steps:

while string > 8 chars:

  1. remove text after the first bracket
  2. remove special chars
  3. remove spaces, starting from the end
  4. remove low-case vowels, starting from the end
  5. remove low-case consonants, starting from the end
  6. remove upper-case vowels, starting from the end
  7. remove upper-case consonants, starting from the end.

No. The process you spell-out is very specific to your needs, and thus must
be done by you prior to using the fmt module.

On Wed, Feb 21, 2018 at 5:38 AM, AlexFundorin notifications@github.com
wrote:

Thanks! A bit off topic, but I'm also interested, if it's possible to omit
some symbols from the input string, such as non-letters/special
symbols/vowels with fmt, or should I use match instead?
For example, to convert "Hello, World! (foobar)" into 8 characters string
"HellWrld", by going through the following steps:

while string > 8 chars:

  1. remove text after the first bracket
  2. remove special chars
  3. remove spaces, starting from the end
  4. remove low-case vowels, starting from the end
  5. remove low-case consonants, starting from the end
  6. remove upper-case vowels, starting from the end
  7. remove upper-case consonants, starting from the end.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/fmtlib/fmt/issues/662#issuecomment-367266787, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AImIsdIs6cRaRjl_JlC844Vl_-8WWIROks5tW-QTgaJpZM4SMZJE
.

--
Leo Mauro
Asesor
Dirección de Servicios Telemáticos
Universidad Simón Bolívar

A bit off topic, but I'm also interested, if it's possible to omit some symbols from the input string, such as non-letters/special symbols/vowels with fmt, or should I use match instead?

No, as @leomauro wrote, this is very specific to your use case and the fmt library doesn't provide any mechanism for that.

NP. I've used regex_match before formatting the final line with fmt. Thanks.

Another question, if possible:
I have some dynamic value in the string format. Let's say, 91. It's a volume in dB.

string a = "91";
btm_msg = format("{} dB", a);

How do I align the result string in center of the "screen", so it would look like this:
" 91 dB "
When I'm trying to align string a alone, I get something like this:
"91(four whitespaces here)dB"

UPD. I've used format on top of another format as a temporary workaround, but, it feels ugly, tbt.
btm_msg += format("{:^9}", format("{0:.2f}dB", VAL2DB(GetMediaTrackInfo_Value(tr, "D_VOL"))));

result: https://i.imgur.com/rvMK4jt.png

I think nested format statements are the way to go there. You should also have a space between the number and the unit.

There's not enough space to add space between the number and the unit. The lowest possible value is "-150.00". Combined with "dB", it fills 9 chars out of desired 8. Which is still acceptable, though.
https://i.imgur.com/7fp1fMC.png

@AlexFundorin, nested call to format is the only simple solution that I can see.

Yep. I'm using nested calls in my code and I'm quite happy with it. Thanks again.
https://i.imgur.com/9WHT0P8.png

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sab24 picture sab24  Â·  21Comments

jiri picture jiri  Â·  22Comments

cjdb picture cjdb  Â·  13Comments

arthurdead picture arthurdead  Â·  12Comments

christhomas picture christhomas  Â·  12Comments