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 to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view<char>, fmt::v7::format_args) #2157

Closed
MrClyde07 opened this issue Feb 28, 2021 · 8 comments

Comments

@MrClyde07
Copy link

MrClyde07 commented Feb 28, 2021

I have been running into a compiling issue with GCC 10.2.0. Where I tried to compile my code in MVSC with the header "fmt\format.h" and it would tell me that vformat is undefined. I'm very new to programming and still learning. I need this header for the book I'm reading and I don't really want to skip anything.

`#include
#include "fmt/format.h"

int main()
{

std::cout << fmt::format("Hello your number is {}", 42) << std::endl;
return 0;`

}`

undefined reference to `fmt::v7::detail::vformat[abi:cxx11](fmt::v7::basic_string_view, fmt::v7::format_args)'
collect2.exe: error: ld returned 1 exit status

@MrClyde07
Copy link
Author

Nevermind I figured it out...

@Blazin64
Copy link

Blazin64 commented Mar 24, 2021

Nevermind I figured it out...

Would you mind sharing what the solution was? I have been experiencing the same error.

Oh, I missed the last entry in the features section of the README. 😅

For anybody else who encounters this error: You need to insert #define FMT_HEADER_ONLY above #include "fmt/format.h" so your code will compile.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented May 4, 2022

Wow, what a pain in the butt! If you do not include the define like this:

#define FMT_HEADER_ONLY
#include <fmt/format.h>

...then what's the solution?

UPDATE 4 May 2022: here's the answer: #2157 (comment).

In short: run cmake, make, and make install, then build with -lfmt or "/usr/local/lib/libfmt.a" as part of your g++ build command to link against the fmt library. See the link just above for full instructions.

@vitaut
Copy link
Contributor

vitaut commented May 4, 2022

To enable the header-only mode you should define FMT_HEADER_ONLY in your build system or use fmt::fmt-header-only CMake target instead of doing it before includes.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented May 4, 2022

@vitaut , thanks. How does one use the not header-only mode? I mean: what's the other way to build and use the lib if we choose to use the mode which is not header only?

@vitaut
Copy link
Contributor

vitaut commented May 4, 2022

Please see https://fmt.dev/latest/usage.html.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented May 4, 2022

@vitaut , thank you very much for responding so quickly! What I really needed, unfortunately, wasn't to be found in that documentation link, but I just figured out the following. Perhaps you can update the documentation at that link with this additional information? I had already followed those instructions to make and install the library prior to my first comment here: #2157 (comment). I had already run:

git clone https://github.com/fmtlib/fmt.git
cd fmt
mkdir -p build 
cd build
time cmake ..  # takes ~2 sec
time make      # takes ~3~5 minutes
time sudo make install  # takes ~7 sec

Then I tried to build and run with:

time g++ -Wall -Wextra -Werror -O3 -std=c++17 fmt_lib_demo.cpp -o bin/a && bin/a

and I got this error:

/tmp/ccYuiFgR.o: In function `main':
fmt_lib_demo.cpp:(.text.startup+0x121): undefined reference to `fmt::v8::vformat[abi:cxx11](fmt::v8::basic_string_view<char>, fmt::v8::basic_format_args<fmt::v8::basic_format_context<fmt::v8::appender, char> >)'
fmt_lib_demo.cpp:(.text.startup+0x174): 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> >)'
fmt_lib_demo.cpp:(.text.startup+0x19b): 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

When I ran sudo make install previously, however, I remembered this important output line:

-- Installing: /usr/local/lib/libfmt.a

So, I linked to that /usr/local/lib/libfmt.a library by adding either -lfmt OR "/usr/local/lib/libfmt.a" to the build command, as follows. Either of those approaches now work! Please add that info. to your documentation here (https://fmt.dev/latest/usage.html) for those, like me, who are using raw gcc/g++ to build and test, rather than a build system.

Here are the 2 solutions to link to the libfmt.a static library produced and installed by cmake and make. Both work. Neither are documented in your documentation:

# Option 1: add `-lfmt`
time g++ -Wall -Wextra -Werror -O3 -std=c++17 fmt_lib_demo.cpp -o bin/a -lfmt && bin/a

# OR: Option 2: add `"/usr/local/lib/libfmt.a"`
time g++ -Wall -Wextra -Werror -O3 -std=c++17 fmt_lib_demo.cpp "/usr/local/lib/libfmt.a" -o bin/a && bin/a

Thanks!

I'm now also enforcing compile-time error checks by adding the FMT_ENFORCE_COMPILE_STRING define to my build command, and using FMT_STRING() around all format strings, as explained here: https://fmt.dev/latest/api.html#compile-time-format-string-checks.

Here is my final build and run command:

time g++ -Wall -Wextra -Werror -O3 -std=c++17 -DFMT_ENFORCE_COMPILE_STRING fmt_lib_demo.cpp -o bin/a -lfmt && bin/a

This will be going into my eRCaGuy_hello_world demonstration code in this file here: eRCaGuy_hello_world/cpp/fmt_lib_demo.cpp.

@ElectricRCAircraftGuy
Copy link

I added my instructions here: C++ fmt library installation & setup

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

4 participants