1

I downloaded the library from https://github.com/fmtlib/fmt and then executed the following commands from official documentation https://fmt.dev/latest/usage.html:

mkdir build
cd build
cmake ..
sudo make install

The commands were executed without errors. The final output of the sudo make install command:

Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/libfmt.a
-- Installing: /usr/local/include/fmt/args.h
-- Installing: /usr/local/include/fmt/chrono.h
-- Installing: /usr/local/include/fmt/color.h
-- Installing: /usr/local/include/fmt/compile.h
-- Installing: /usr/local/include/fmt/core.h
-- Installing: /usr/local/include/fmt/format.h
-- Installing: /usr/local/include/fmt/format-inl.h
-- Installing: /usr/local/include/fmt/locale.h
-- Installing: /usr/local/include/fmt/os.h
-- Installing: /usr/local/include/fmt/ostream.h
-- Installing: /usr/local/include/fmt/printf.h
-- Installing: /usr/local/include/fmt/ranges.h
-- Installing: /usr/local/include/fmt/xchar.h
-- Installing: /usr/local/lib/cmake/fmt/fmt-config.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-config-version.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-targets.cmake
-- Installing: /usr/local/lib/cmake/fmt/fmt-targets-release.cmake
-- Installing: /usr/local/lib/pkgconfig/fmt.pc

I next created the file main.cpp with the following content:

#include <fmt/core.h>

int main()
{
    fmt::print("Hello");

    return 0;
}

And compiled it with the command g++ main.cpp -o main. However, the compilation results in the following error:

/usr/bin/ld: /tmp/ccJ3FzHH.o: in the function «main»:
main.cpp:(.text+0x8a): undefined reference to «fmt::v8::vprint(fmt::v8::basic_string_view<char>, fmt::v8::basic_format_args<fmt::v8::basic_format_context<fmt::v8::appender, char> >)»
collect2: error: ld returned 1 exit status

I am using Ubuntu 20.04

unknown6656
  • 2,765
  • 2
  • 36
  • 52
mozdotio
  • 21
  • 5

1 Answers1

1

Adding the argument -lfmt to the compilation command solves the issue. -l<name> arguments generally stand for including the library <name> into the compilation (see GCC documentation)

The command thus looks as follows:

$ g++ main.cpp -lfmt -o main
unknown6656
  • 2,765
  • 2
  • 36
  • 52
mozdotio
  • 21
  • 5