1

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?

1
  • Re the C/C++ compiler on the system is immediately able to find the installed library. that is not strictly true. Nder Linux most libraries though are hard coded to one place and the code using them has the same hard coding. In general all Unix has the same issues as you see in macos
    – mmmmmm
    Feb 27 at 19:58
3

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 .

3
  • @nmd_07 Also try export CPATH=opt/homebrew/include
    – nohillside
    Feb 27 at 20:15
  • @nohillside well, for /opt/homebrew/include that worked. I am not sure about LDFLAGS though.
    – nmd_07
    Feb 27 at 20:19
  • So LDFLAGS and CFLAGS seem to be the wrong variables. Tryp CPATH and LIBRARY_PATH instead.
    – nohillside
    Feb 27 at 20:19

You must log in to answer this question.

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