c++g++include-pathlibrary-path

For G++ on MacOS how to add default include dir /usr/local/include and default library search path /usr/local/lib?


I have installed libfmt with homebrew. Files could be found in /usr/local/lib/libfmt.dylib and and /usr/local/include/fmt. Formally they are links to /usr/local/Cellar/....

G++ does not search /usr/local/include/ and /usr/local/lib/ by default. And I would not like to add them on project level.

❯❯❯ g++-9 -print-search-dirs
install: /usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/
programs: =/usr/local/Cellar/gcc/9.3.0_1/libexec/gcc/x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/libexec/gcc/x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/libexec/gcc/x86_64-apple-darwin18/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/../../../../../../x86_64-apple-darwin18/bin/x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/../../../../../../x86_64-apple-darwin18/bin/
libraries: =/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/../../../../../../x86_64-apple-darwin18/lib/x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/../../../../../../x86_64-apple-darwin18/lib/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/../../../x86_64-apple-darwin18/9.3.0/:/usr/local/Cellar/gcc/9.3.0_1/lib/gcc/9/gcc/x86_64-apple-darwin18/9.3.0/../../../

Solution

  • You can do this by setting the relevant environment variables in your shell, e.g:

    export CPATH=...
    export LIBRARY_PATH=...
    

    Full documentation here