Ubuntu – C++ fmt library instalation is not working

ccommand linelibrariespermissions

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?

Best Answer

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+'