Installing a C/C++ library with homebrew on M1 Macs

In Linux, when a C/C++ library is installed through a package manager, the C/C++ compiler on the system is immediately able to find the installed library. I haven't used Intel Macs but this is definitely not the case in M1 Macs.

On M1 Macs, homebrew package manager installs software and libraries under /opt folder. It seems that clang that comes with XCode is not able to recognize this path so each time I compile my program against a particular library, I need to specify the library path using compiler's appropriate flag.

Is there a way to make clang compiler in M1 Macs recognize installed C/C++ library paths by default?


Add an environment variable as follows

export CPATH=/opt/homebrew/include
export LIBRARY_PATH=/opt/homebrew/lib

to the file corresponding to the shell, like ~/.bash_profile for bash.

Alternatively create CMake file with content like

link_directories("/opt/homebrew/lib")
include_directories("/opt/homebrew/include")

Or

set(CMAKE_EXE_LINKER_FLAGS "-L/opt/homebrew/lib " CACHE STRING "" FORCE)
set(CMAKE_C_FLAGS   "-I/opt/homebrew/include " CACHE STRING "" FORCE )
set(CMAKE_CXX_FLAGS "-I/opt/homebrew/include " CACHE STRING "" FORCE)

and create projects like cmake -C ../link_dir.cmake -B ../build -S .