Score:2

C++ fmt library not working

mx flag

Hi guys I'm a C++ beginner and I just found that the fmt lib is really interesting. But I ran into a problem when trying to use it. I used homebrew to install fmt lib brew install fmt .I check the fmt installation using apt-file search /usr/include/fmt/format. and it then returned libfmt-dev: /usr/include/fmt/format.h .I tried to build a sample project to test it as follows:

#include <fmt/format.h>

int main() {
  fmt::print("The answer is {}.\n", 42);
}

After I tried to complie it with gcc11 it returned

/home/linuxbrew/.linuxbrew/bin/ld: /tmp/ccmrL6RH.o: in function `void fmt::v8::print<int>(fmt::v8::basic_format_string<char, fmt::v8::type_identity<int>::type>, int&&)':
/home/linuxbrew/.linuxbrew/include/fmt/core.h:3209: 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

Honestly I don't understand what this problem is... The syntax looks good and there is nothing wrong with the compiler and IDE. Could you help me out? Thanks in advance.

Update: @steeldriver told me that it was because the fmt lib was not linked. So I added some commands in the tasks.json file:

...
 "tasks": [
        ...
        "args": [
            ...
            "-lfmt", "-L~/usr/include/fmt"
        ]
]
...

/usr/include/fmt is the path to fmt lib. @steeldriver recommended this reference https://stackoverflow.com/questions/49486880/how-to-link-fftw3-libraries-for-c-in-vs-code-in-ubuntu.

cocomac avatar
cn flag
What OS and version are you using?
mx flag
@cocomac it's ubuntu linux 20.04.3, operating on virtual machine on macbook pro
us flag
Try `sudo apt install libfmt-dev` instead of homebrew.
mx flag
@ArchismanPanigrahi Thank you my friend. I just tried this code and then I tried include the full path to the fmt lib `#include </usr/include/fmt/format.h>`, but it returned ``/home/linuxbrew/.linuxbrew/bin/ld: /tmp/ccBkstet.o: in function `void fmt::v6::print<char [19], int, 0>(char const (&) [19], int&&)': /usr/include/fmt/core.h:1514: undefined reference to `fmt::v6::vprint(fmt::v6::basic_string_view<char>, fmt::v6::format_args)'``. It seems that it has something to do with homebrew?
mx flag
after `sudo apt install ilbfmt-dev` i uninstalled fmt lib using `brew uninstall fmt`. Notice that v8 changed to v6. @ArchismanPanigrahi
us flag
Does it still show the same error (related to `linuxbrew`) even after removing the version from homebrew? Can you try rebooting? Maybe it is still trying to using the homebrew version from cache.
hr flag
@ArchismanPanigrahi it look like the issue now is that it's still using the homebrew version of `ld` (perhaps because the OP placed `/home/linuxbrew/.linuxbrew/bin/` early in their `PATH`)
mx flag
Yes. I just rebooted the system and still the same error. Can I change the path? @steeldriver
hr flag
@WilliamB it would likely be something added to one of your shell startup scripts when you installed linuxbrew - look in ~/.bashrc, ~/.profile etc.
mx flag
I found the /.profile by this code `xdg-open .profile`. And I found these text in this file ` # set PATH so it includes user's private bin if it exists if [ -d "$HOME/.local/bin" ] ; then PATH="$HOME/.local/bin:$PATH" fi eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"` I guess these lines is what you meant. Is that right? Should I change it? BTW, I have uninstalled homebrew @steeldriver
hr flag
Yes if you no longer intend to use brew, you can remove (or comment out - using `#`) the line `eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"`
mx flag
Yes I just did that. But the result is confusing. I changed the gcc compiler path configuration in visual studio code. The result is as follows ``/usr/bin/ld: /tmp/ccdxHuLT.o: in function `void fmt::v6::print<char [19], int, 0>(char const (&) [19], int&&)': /usr/include/fmt/core.h:1514: undefined reference to `fmt::v6::vprint(fmt::v6::basic_string_view<char>, fmt::v6::format_args)' collect2: error: ld returned 1 exit status`` @steeldriver
hr flag
It's hard to comment on that without seeing the actual gcc command(s) generated by VSCode
mx flag
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/134281/discussion-between-williamb-and-steeldriver).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.