5

I am using MACOS Mojave version 10.14.3 and need to use GNU compiler and not clang.

I installed gcc compiler using brew install gcc. Then I installed fmt library using brew install fmt.

I put #include <fmt/format.h> at the top of my C++ script

Then I type:

/usr/local/bin/g++-8 -O0 -g3 -Wall -c -fmessage-length=0 -std=c++17 -MMD -MP -MF"src/trail2.d" -MT"src/trail2.o" -o "src/trail2.o" "../src/trail2.cpp"

However I get this error:

fatal error: fmt/format.h: No such file or directory

Same happens when I try using the boost library.

When I use clang, fmt and boost are found without problem.

Just in case I am using Eclipse IDE for C/C++ Developers. Version: 2018-12 (4.10.0).

To be specific, how can I get fmt library to work with the gcc I installed with brew?

What I have in my Mac at /usr/local/Cellar is:

drwxr-xr-x   3 aaa  staff    96 Feb 22 22:07 gcc
drwxr-xr-x   3 aaa  staff    96 Feb 23 01:58 fmt

What I have in my Mac at /usr/local/Cellar is:

    lrwxr-xr-x    1 aaa  admin        29 Feb 22 22:07 c++-8 -> ../Cellar/gcc/8.2.0/bin/c++-8
    lrwxr-xr-x    1 aaa  admin        29 Feb 22 22:07 cpp-8 -> ../Cellar/gcc/8.2.0/bin/cpp-8
    lrwxr-xr-x    1 aaa  admin        29 Feb 22 22:07 g++-8 -> ../Cellar/gcc/8.2.0/bin/g++-8
    lrwxr-xr-x    1 aaa  admin        29 Feb 22 22:07 gcc-8 -> ../Cellar/gcc/8.2.0/bin/gcc-8

Thanks for the help in advanced

vitaut
  • 49,672
  • 25
  • 199
  • 336
ananvodo
  • 371
  • 5
  • 13
  • 2
    This is not an issue with GCC, it's an issue where the *library* is installed. If it's not installed in a "standard" location, you must tell the `g++` frontend program where to find the header and library files. Use the `-I` (upper-case i) option to specify where the header files are installed, and the `-L` and `-l` (lower-case L) options for any linker libraries. – Some programmer dude Feb 23 '19 at 07:59
  • I'd guess that's [/usr/local/include and /usr/local/lib](https://stackoverflow.com/q/25540848/243245) – Rup Feb 23 '19 at 08:00
  • Considering the contents of `/usr/local/Cellar` my *guess* is that you need to add `-I/usr/local/Cellar`. Since I don't know how `brew` organizes its packages when installed, perhaps something like `-I/usr/local/Cellar/fmt/include`? You can add the directory in your Eclipse project settings somewhere (Preprocessor settings tab or similar?), but then usually without the actual `-I` option. – Some programmer dude Feb 23 '19 at 08:04
  • Hi. I am new user in C++. Could you please help me with how my g++ command line should look like? In `/usr/local/lib` I have this: `lrwxr-xr-x 1 aaa admin 32 Feb 23 01:58 libfmt.a -> ../Cellar/fmt/5.3.0/lib/libfmt.a` – ananvodo Feb 23 '19 at 08:04

1 Answers1

5

The way homebrew works is that it puts everything in

/usr/local/Cellar/PACKAGE-NAME/PACKAGE-VERSION

and then it creates links to those things for binaries in /usr/local/bin, e.g.

/usr/local/bin/grep -> /usr/local/Cellar/grep/4.17/bin/grep

so that you just need to put /usr/local/bin in your PATH and all homebrew programs will be runnable by name, e.g. grep in the above example.


It does the same thing for compiling, it puts the actual headers and libraries in:

/usr/local/Cellar/PACKAGE-NAME/PACKAGE-VERSION/include
/usr/local/Cellar/PACKAGE-NAME/PACKAGE-VERSION/lib

and also creates links to those in

/usr/local/include
/usr/local/lib

So, your gcc command will be:

g++-8 -I /usr/local/include -L /usr/local/lib -lfmt <PROGRAM.CPP> -o <PROGRAM>

You can see what files belong to your fmt package and where they are with:

brew ls fmt --verbose

If you install pkg-config, using:

brew install pkg-config

it will use the file fmt.pc and can tell you the correct switches for compiling if you type:

pkg-config --libs --cflags fmt

Sample Output

-I/usr/local/Cellar/fmt/5.3.0/include -L/usr/local/Cellar/fmt/5.3.0/lib -lfmt

That means you can simplify your gcc command to:

g++-8 $(pkg-config --libs --cflags fmt) <PROGRAM.CPP> -o <PROGRAM>
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432