10

I'm a new C++ user and I have been facing some problems to install external libraries in my programs. Currently, I'm trying to install the fmt library. I downloaded the repository content and unpack it. Then I add de sub-directory fmt to usr/include/fmt, which doesn't work. I also tried add fmt to usr/local/include, also without success. Here is a sample code and the respective error that returns:

#include <iostream>
#include <string>
#include <format.h> // I also tried include <fmt/format.h>

using namespace std;

int main(){
   string s = fmt::format("{0}{1}{0}", "abra", "cad");
   cout<< s<<endl;
   return 0;
}

error:

stack.cpp:3:10: fatal error: format.h: No such file or directory
 #include <format.h>
          ^~~~~~~~~~
compilation terminated

I note that the fmt folder is "closed" (marked with a "X") after the command I use to transfer it to usr/~ using:

cp fmt-master/include/fmt usr/local/include/fmt

I tried to change the folder permissions using:

sudo chmod -rwx usr/local/include/fmt

But EVEN this didn't work. The command runs without output and fmt folder continues with X mark. I would like to know 1) Save the library folder in /usr/include is the standard procedure to install external libraries in C++? (It looks too manual to me) 2) What am I doing wrong here?

8
  • Have you tried #include "format.h" or #include "fmt/format.h"?
    – Itakura
    Commented Jan 26, 2020 at 2:54
  • I just tried #include "format.h" and #include "fmt/format.h". It didn't work
    – Lucas
    Commented Jan 26, 2020 at 2:56
  • Is there a reason you don't want to use the libfmt-dev package from apt? You need to enable the universe repository: sudo add-apt-repository universe
    – mchid
    Commented Jan 26, 2020 at 3:08
  • 1
    @Hartnäckig Yes, apt is the standard way of installing pretty much everything on Ubuntu. I'm running 18.04. If it cannot find the package, try running all three commands again. When you use apt to install stuff, everything usually "just works".
    – mchid
    Commented Jan 26, 2020 at 3:24
  • 1
    @Hartnäckig One more thing that will help, you can also install apt-file to help you find files that you need. sudo apt install apt-file and then update it: sudo apt-file update and then to search for the file: apt-file search /usr/include/fmt/format.h This should show you what package contains the file.
    – mchid
    Commented Jan 26, 2020 at 3:30

1 Answer 1

12

Most packages and libs don't need to be manually downloaded and installed on Ubuntu. You can install libfmt-dev using the somewhat official "universe" repository using apt.

Run the following commands to install:

sudo add-apt-repository universe
sudo apt update
sudo apt install libfmt-dev

Most of the "build" type packages in the Ubuntu repositories have the "dev" suffix at the end. 9 times out of 10, when you need to install some prerequisite or dependency to build stuff using g++ or C++, these will be the "lib" packages that you need.

To search for available packages, you can use the apt-cache search command like in this example:

apt-cache search libfmt

If you get too many results, you can pipe the command to grep to narrow the results like this:

apt-cache search libfmt | grep dev

or something like this:

apt-cache search fmt | grep 'C+'

or you can use the -i flag with grep so that it is not case sensitive like this:

apt-cache search fmt | grep -i 'c+'
3
  • 1
    In header only mode, I get /usr/include/fmt/format.h:4007:11: fatal error: 'format.cc' file not found. So the install is incomplete it seems.
    – Michel
    Commented Mar 24, 2020 at 17:20
  • @Michel apt-file search format.cc returns two packages. We usually need the packages with the "dev" suffix so my guess it that this will fix the error: sudo apt install libspdlog-dev The other package is c++-annotations but it looks like that one just has an example file.
    – mchid
    Commented Apr 4, 2020 at 17:47
  • @Michel See this comment on how to install apt-file
    – mchid
    Commented Apr 4, 2020 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .