Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use fmt library in the header-only mode?

Tags:

c++

fmt

Having a hard time using the header-only mode of fmt library. Here is what I tried in details: I downloaded fmt7.1.3 from https://fmt.dev/latest/index.html, only put the directory fmt-7.1.3/include/fmt in a directory ([trgdir]) and wrote a test.cpp as follow:

#include <iostream>
#include <fmt/format.h>
int main() {
    fmt::format("The answer is {}.", 42);
    return 0;
}

Then in the terminal I use

gcc -I[trgdir] test.cpp

where gcc I defined as

alias gcc='gcc-10 -xc++ -lstdc++ -shared-libgcc -std=c++17 -O2 '

I got the error goes as

Undefined symbols for architecture x86_64:
  "__ZN3fmt2v76detail7vformatB5cxx11ENS0_17basic_string_viewIcEENS0_11format_argsE", referenced from:
      _main in ccEeTo0w.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

I have checked this post but I still cannot solve my issue. How to use the fmt library without getting "Undefined symbols for architecture x86_64"

like image 495
Ethanabc Avatar asked Apr 04 '21 18:04

Ethanabc


People also ask

How do I use FMT library?

To use the {fmt} library, add fmt/core. h , fmt/format. h , fmt/format-inl. h , src/format.cc and optionally other headers from a release archive or the Git repository to your project.

How do headers only work in libraries?

Header-only libraries do not need to be separately compiled, packaged and installed in order to be used. All that is required is to point the compiler at the location of the headers, and then #include the header files into the application source.

Why are some libraries header-only?

Having a header-only library also means you don't have to worry about different platforms where the library might be used. When you separate the implementation, you usually do so to hide implementation details, and distribute the library as a combination of headers and libraries ( lib , dll 's or . so files).

Are header-only libraries good?

The benefit of header-only libraries is that they are easy to include in your project as you simply include the header and you are done (there is no need to compile the library as there are no source files to compile).


1 Answers

You need to define a macro before include, like this:

#define FMT_HEADER_ONLY
#include "fmt/format.h"
like image 122
Jovibor Avatar answered Oct 18 '22 05:10

Jovibor