Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined reference with FMT_HEADER_ONLY and core.h #1296

Closed
mdcdr opened this issue Sep 2, 2019 · 2 comments
Closed

Undefined reference with FMT_HEADER_ONLY and core.h #1296

mdcdr opened this issue Sep 2, 2019 · 2 comments

Comments

@mdcdr
Copy link

mdcdr commented Sep 2, 2019

When building with FMT_HEADER_ONLY and including fmt/core.h.

gcc version 9.2.0

$ cat fmt_test.cpp

#include <iostream>
#include <fmt/core.h>

int main()
{
        std::string message = fmt::format("The answer is {}", 42);
        std::cout << message << "\n";
        return 0;
}

$ g++ -o fmt_test -DFMT_HEADER_ONLY -I./include fmt_test.cpp

/usr/bin/ld: /tmp/ccQQn4MB.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > fmt::v6::format<char [17], int, char>(char const (&) [17], int&&)':
fmt_test.cpp:(.text._ZN3fmt2v66formatIA17_cJiEcEENSt7__cxx1112basic_stringIT1_St11char_traitsIS5_ESaIS5_EEERKT_DpOT0_[_ZN3fmt2v66formatIA17_cJiEcEENSt7__cxx1112basic_stringIT1_St11char_traitsIS5_ESaIS5_EEERKT_DpOT0_]+0x70): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > fmt::v6::internal::vformat<char>(fmt::v6::basic_string_view<char>, fmt::v6::basic_format_args<fmt::v6::basic_format_context<std::back_insert_iterator<fmt::v6::internal::buffer<char> >, char> >)'
collect2: error: ld returned 1 exit status

Shouldn't this compile?
If format.h is included instead core.h, it works.
I'm trying to include only core.h, to see if I can reduce executable size.
According to benchmarks executable should be smaller than tinyformat, but it's opposite.

@vitaut
Copy link
Contributor

vitaut commented Sep 3, 2019

Shouldn't this compile?

No. Use fmt/format.h instead of fmt/core.h in the header-only mode. I clarified this in the docs: fe642d7.

Compiling in the header mode may also pull in unnecessary symbols, so I recommend building with the static library instead, possibly even with LTO. Note that the benchmark compares per-call code bloat, not fixed library sizes. I don't know how the library sizes compare.

@vitaut vitaut closed this as completed Sep 3, 2019
@vitaut
Copy link
Contributor

vitaut commented Sep 3, 2019

The {fmt} library size is ~200-300kiB (depends on the system) although there were reports of people being able to bring it down significantly on embedded platforms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants