gcc-11 from Homebrew Not Found by MacOS - installation and $PATH seem OK - macos

I'm trying to compile a package called Kraken on my M1 Mac running Big Sur.
MacOS fails to compile: clang gets upset. I installed gcc from Homebrew (twice) and $ brew doctor says I'm ready to go.
My path is now:
/usr/local/opt/llvm/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/opt/homebrew/bin.
The gcc-11 etc files exist at /opt/homebrew/bin
But /usr/local/bin does not exist!
MacOS does not update the command alias for gcc:
$ rm gcc
then
$ ln -s /opt/homebrew/bin/gcc-11 gcc
return no error
but then
$ which gcc
returns
/usr/bin/gcc
I'm lost and gather this compiler-pointing has been a mess; is there any new insight here? I've seen a bunch of entries but nothing I've tried has worked.
Hoping this is a stupid newbie (me) problem, I thank you for any help you can give.
Michael

Homebrew avoids linking binaries with the same name as system binaries like gcc(appleclang). ln -s /opt/homebrew/bin/gcc-11 /opt/homebrew/bin/gcc or tell your configurator to explicitly use gcc-11 via CC= etc. and the same for g++-11 if C++ is used.
If it's a ./configure script, you can try calling CC=/opt/homebrew/bin/gcc-11 CXX=/opt/homebrew/bin/g++-11 ./configure
if cmake then use cmake .. -DCMAKE_C_COMPILER=/opt/homebrew/bin/gcc-11 -DCMAKE_CXX_COMPILER=/opt/homebrew/gcc++-11
Also, try seeing where the homebrew gcc link points ls -l /opt/homebrew/bin/gcc

Related

Why my Make file doesn't compiling in Mac and display ld: library not found for -lSystem? [duplicate]

I am getting a strange error when trying to compile a simple fortran-90 file using gfortran. I am working on a macOS mojave system. I have gfortran 8.2.0 installed and I checked to be sure of this by doing the following:
Input: gfortran --version
Output: GNU Fortran (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Which indicates that gfortran installed correctly and I also confirmed this by typing simply: gfortran, into the terminal, which returns:
gfortran: fatal error: no input files
compilation terminated.
However, when I try to compile my file like so:
gfortran hello.f90 -o hello.x
I get an error which reads:
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status
I've tried compiling in different ways and I also tried setting the LD_LIBRARY_PATH in .bash_profile as described by https://gcc.gnu.org/wiki/GFortranBinaries32Linux but I still get the same error. The gfortran directory has the pathname: /usr/local/gfortran, however another gfortran which is not a directory can be found in /usr/local/bin
Typing:
which gfortran
Outputs:
/usr/local/bin/gfortran
And a long list search of this directory:
Input: ls -l /usr/local/bin/gfortran
Output: lrwxr-xr-x 1 root wheel 32 15 May 14:07
/usr/local/bin/gfortran -> /usr/local/gfortran/bin/gfortran
How can I fix this where everything seems to be correctly installed?
Any help would be greatly appreciated!
On macOS Big Sur v11.1:
Relevant SO post: https://apple.stackexchange.com/questions/408999/gfortran-compiler-error-on-mac-os-big-sur
The fix is to add the stdlib to your $LIBRARY_PATH. For some reason or another it isn't in your standard $PATH anymore on 11.1.
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
(or add it to login file and restart terminal).
I added this to my ~/.profile:
export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
export LIBRARY_PATH="$LIBRARY_PATH:$SDKROOT/usr/lib"
And it works.
I'll also chime in (19 Dec 2020) that I just updated from MacOS Mojave to Catalina and suddenly got this error.
It resolved itself with
brew reinstall gcc
brew reinstall gfortran
Update: I tried what was suggested and installed the gfortran build from the gcc package using homebrew which gave me a newer version of gfortran and my problem was fixed!
I had the same problem, but installing gfortran build from the gcc package using homebrew did not help. Instead, I installed the whole gcc using homebrew:
brew install gcc
and that solved the problem.
For those using MacPorts.
# Installed `gcc8` and `gcc_select`
sudo port install gcc_select gcc8
# then to create "gfortran" softlink to "gfortran-mp-8"
sudo port select --set gcc gcc8
# and added to my .profile
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
I did ls -l /Library/Developer/CommandLineTools/SDKs/ and found that Xcode had installed one SDK for Catalina and one for Big Sur. I changed the soft link for MacOSX.sdk to point to the Catalina version.
Just placed this line at the end of .zshrc:
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
and ran my Fortran code again to compile – success! The issue with -lSystem has been solved in my macOS system.
I had this problem, and I'd tried lots of thing to solve the problem, but only using these "both" commands solved it:
brew reinstall gcc
brew reinstall gfortran
I recently had this
ld: library not found for -lSystem
problem after upgrading Mac OS from v10 to v11 (Big Sur). I am using Xcode 12.3 and I used brew install gcc to install gfortran 10.2.0. I still saw the error after trying brew reinstall gcc. I was able to solve the problem using:
brew install gfortran
but I do not know why this works.

compiling F90 code on mac osx catalina: library issues [duplicate]

I am getting a strange error when trying to compile a simple fortran-90 file using gfortran. I am working on a macOS mojave system. I have gfortran 8.2.0 installed and I checked to be sure of this by doing the following:
Input: gfortran --version
Output: GNU Fortran (GCC) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Which indicates that gfortran installed correctly and I also confirmed this by typing simply: gfortran, into the terminal, which returns:
gfortran: fatal error: no input files
compilation terminated.
However, when I try to compile my file like so:
gfortran hello.f90 -o hello.x
I get an error which reads:
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status
I've tried compiling in different ways and I also tried setting the LD_LIBRARY_PATH in .bash_profile as described by https://gcc.gnu.org/wiki/GFortranBinaries32Linux but I still get the same error. The gfortran directory has the pathname: /usr/local/gfortran, however another gfortran which is not a directory can be found in /usr/local/bin
Typing:
which gfortran
Outputs:
/usr/local/bin/gfortran
And a long list search of this directory:
Input: ls -l /usr/local/bin/gfortran
Output: lrwxr-xr-x 1 root wheel 32 15 May 14:07
/usr/local/bin/gfortran -> /usr/local/gfortran/bin/gfortran
How can I fix this where everything seems to be correctly installed?
Any help would be greatly appreciated!
On macOS Big Sur v11.1:
Relevant SO post: https://apple.stackexchange.com/questions/408999/gfortran-compiler-error-on-mac-os-big-sur
The fix is to add the stdlib to your $LIBRARY_PATH. For some reason or another it isn't in your standard $PATH anymore on 11.1.
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
(or add it to login file and restart terminal).
I added this to my ~/.profile:
export SDKROOT=$(xcrun --sdk macosx --show-sdk-path)
export LIBRARY_PATH="$LIBRARY_PATH:$SDKROOT/usr/lib"
And it works.
I'll also chime in (19 Dec 2020) that I just updated from MacOS Mojave to Catalina and suddenly got this error.
It resolved itself with
brew reinstall gcc
brew reinstall gfortran
Update: I tried what was suggested and installed the gfortran build from the gcc package using homebrew which gave me a newer version of gfortran and my problem was fixed!
I had the same problem, but installing gfortran build from the gcc package using homebrew did not help. Instead, I installed the whole gcc using homebrew:
brew install gcc
and that solved the problem.
For those using MacPorts.
# Installed `gcc8` and `gcc_select`
sudo port install gcc_select gcc8
# then to create "gfortran" softlink to "gfortran-mp-8"
sudo port select --set gcc gcc8
# and added to my .profile
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
I did ls -l /Library/Developer/CommandLineTools/SDKs/ and found that Xcode had installed one SDK for Catalina and one for Big Sur. I changed the soft link for MacOSX.sdk to point to the Catalina version.
Just placed this line at the end of .zshrc:
export LIBRARY_PATH="$LIBRARY_PATH:/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
and ran my Fortran code again to compile – success! The issue with -lSystem has been solved in my macOS system.
I had this problem, and I'd tried lots of thing to solve the problem, but only using these "both" commands solved it:
brew reinstall gcc
brew reinstall gfortran
I recently had this
ld: library not found for -lSystem
problem after upgrading Mac OS from v10 to v11 (Big Sur). I am using Xcode 12.3 and I used brew install gcc to install gfortran 10.2.0. I still saw the error after trying brew reinstall gcc. I was able to solve the problem using:
brew install gfortran
but I do not know why this works.

Why can't I compile with gfortran after upgrading to OSX Yosemite? gfortran error: libgfortran.spec: No such file or directory

Since upgrading to Yosemite I cannot compile with gfortran. Initially I was getting "segmentation fault 11", then I:
Updated to gcc v.5.0 from http://hpc.sourceforge.net/
Re-installed Xcode command line utilities v.6.1.1
After doing the above, I am able to compile very simple fortran programs but the more complex code that I run gives me the following error:
gfortran: error: libgfortran.spec: No such file or directory
But in fact, the libgfortran.spec file is located in /usr/local/lib
ls /usr/local/lib | grep gfortran
libgfortran.3.dylib
libgfortran.a
libgfortran.dylib
libgfortran.la
libgfortran.spec
All of the gcc binaries are located in /usr/local/bin
which gfortran
/usr/local/bin/gfortran
And my path seems to be OK
echo $PATH
/usr/local/lib:/usr/local/bin:/usr/local:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/lib:/usr/texbin:/Users/MattCooper/bin:/Applications/MRT/bin
I also tried re-installing gcc with homebrew:
brew install gcc
Error: gcc-4.9.2 already installed
To install this version, first 'brew unlink gcc'
However, I get v. 5.0 when I ask 'gcc --version':
gcc --version
gcc (GCC) 5.0.0 20141005 (experimental)
When I try to unlink or upgrade gcc with homebrew I get a permission denied error:
brew unlink gcc
Unlinking /usr/local/Cellar/gcc/4.9.2... Error: Permission denied - /usr/local/bin/c++-4.9
similarly
brew upgrade gcc
Permission denied - /usr/local/bin/c++-4.9
Finally
which gcc
/usr/local/bin/gcc
Please help. Many thanks ahead of time. Please let me know what I've forgotten to include.
I ended up solving the problem via homebrew. I can't pinpoint the exact source of the problem but it was related to the fact that I installed gcc v4.9 from http://hpc.sourceforge.net/, then somewhere along the way I re-installed and/or updated gcc with homebrew. Then, after upgrading to Yosemite, I installed gcc v5 from http://hpc.sourceforge.net/. Somewhere within that process, the binaries within /usr/local/bin and usr/local/lib were not linked properly.
The main clue was the Permission Denied error when trying to run 'brew upgrade gcc'. I found a solution here: Brew doctor says: "Warning: /usr/local/include isn't writable."
To fix, I used:
brew doctor
which returned a bunch of directories that were not writeable, for example usr/local/lib, thus brew was unable to ugrade gcc to the v5.0 and link everything.
I went through and issued:
sudo chown -R $USER /usr/local
and did this for all of the other non-writeable directories returned by 'brew doctor'. Then, I was able to use brew to ugrade gcc:
brew upgrade gcc
During the ugrade, homebrew returned an error that stated it could not finish the upgrade because there was already an existing gfortran file located in:
/usr/local/bin/gfortran
To fix, homebrew suggested I run:
brew -link gcc
at which point everything worked. Interesting (to me, because I don't fully understand all of this) when I ask brew to clean things up, I get:
brew cleanup
Warning: Skipping (old) /usr/local/Cellar/gcc/4.9.2 due to it being linked
There's some similar information located here: How to link to a new gcc version with brew?
Similar to that user, when I look in my cellar, I find two versions of gcc, but I'm not sure if I need both
ls /usr/local/Cellar/gcc
4.9.2 4.9.2_1
I had a similar issue with similar solution. It could not find this file:
gfortran: error: libgfortran.spec: No such file or directory
So my problem was that I had installed gcc through hpc.sourceforge.net and through homebrew. I had to uninstall the homebrew version
brew uninstall gcc
Then I had to reinstall the gcc from hpc.sourceforge.net
sudo tar -xvf gcc-4.9-bin.tar.gz -C /
This worked for me when I wanted to revert to gcc 4.9 from hpc.sourceforge.net, using Yosemite.

On mac, g++ (clang) fails to search /usr/local/include and /usr/local/lib by default

I'm on a mac and I used homebrew to install gmp.
Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx
In file included from main.cpp:2:
./gcjlib.hpp:4:10: fatal error: 'gmpxx.h' file not found
#include <gmpxx.h>
^
1 error generated.
So then I explicitly told g++ to use /usr/local/include
Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include
ld: library not found for -lgmp
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So then I explicitly told g++ to use /usr/local/lib
Kyumins-iMac:gcjlib math4tots$ g++ main.cpp -lgmp -lgmpxx -I/usr/local/include -L/usr/local/lib
Kyumins-iMac:gcjlib math4tots$ ./a.out
sum is -4444
absolute value is 4444
So the only issue seems to be that g++ fails to acknowledge /usr/local.
But it is tedious to type all this out all the time, especially when I'm just writing small single file programs.
Is there a way for me to get g++ to acknowledge the stuff in /usr/local by default? Is there a standard way homebrew users do this?
I'm on OS X 10.9.3 with Xcode 5.1.1 in case it is relevant.
I also use Homebrew and had a similar problem on Mac OSX Maverick 10.9.5 and Xcode 6.0.1, but it was solved by running:
xcode-select --install
Note that it doesn't work without the double hyphens given by the previous answer. This installs the command-line tools that also create /usr/lib/ and /usr/include/. I don't know why Homebrew doesn't automatically check this upon installation, since it does check for Xcode...
If you want to check exactly what folders your compiler is looking through for header files you can write:
cpp -v
A workaround would be to:
export C_INCLUDE_PATH=/usr/local/include
export CPLUS_INCLUDE_PATH=/usr/local/include
At least this tricked the pre-processor to behave here :)
Try running xcode-select --install
At least on Mavericks, I've found that if I install the Xcode application without installing the command-line tools, then the tools are sort of available, but normal unix-ey builds don't work correctly. One symptom is that /usr/local/include is not on the include search path. The command-line tools seem to resolve this issue.
I have Yosemite 10.10.5 and running xcode-select --install didn't fix the problem for me. The command returned with xcode-select: error: command line tools are already installed, use "Software Update" to install updates.
When I ran xcode-select -p, it showed /Applications/Xcode.app/Contents/Developer. I ended up deleting Xcode from the Applications directory, which resulted in xcode-select -p returning /Library/Developer/CommandLineTools. This fixed compiler error for me.
That was helpful for me:
Use the latest version. 1.0.2o_1 just a current build.
brew install openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/include/openssl /usr/local/include/openssl
ln -s /usr/local/Cellar/openssl/1.0.2o_1/lib /usr/local/lib/openssl
There are a few questions around this topic with answers that suggest putting a symlink in /usr/local/include. However I'm running macOS Monterey 12.3 (on an M1 MacBook) and that directory doesn't exist.
I had installed the Xcode command line tools by downloading the package from Apple, so xcode-select --install just tells me it's already installed and doesn't create any directories.
I ran cpp -v to see which directories are searched for #include <...>:
/Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
/Library/Developer/CommandLineTools/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
I picked /Library/Developer/CommandLineTools/usr/include for the symlink. In that directory, I ran the following command (note the new location for Homebrew installations, under /opt/homebrew - some old answers are also out of date on this point):
sudo ln -s /opt/homebrew/opt/openssl#3/include/openssl .
clang was then able to find the OpenSSL files.
apk add --no-cache build-base
it works fine !!!!
go build -tags musl -o main main.go

changing search-dirs for $ sudo gcc

On OS X I'm trying to install the zlib prerequisite for haskell's Cabal. I get this error:
$ sudo ./Setup build
Preprocessing library zlib-0.5.0.0…
ld: library not found for -lgmp
collect2: ld returned 1 exit status
linking dist/build/Codec/Compression/Zlib/Stream_hsc_make.o failed
command was: /usr/bin/gcc -lz -L/sw/lib/ghc-6.8.3/lib/bytestring-0.9.0.1.1 -L/sw/lib/ghc-6.8.3/lib/array-0.1.0.0 -L/sw/lib/ghc-6.8.3/lib/base-3.0.2.0 -L/sw/lib/ghc-6.8.3 -lm -lgmp -ldl dist/build/Codec/Compression/Zlib/Stream_hsc_make.o -o dist/build/Codec/Compression/Zlib/Stream_hsc_make
The library -lgmp is found in /sw/lib, so I can run that command ("/usr/bin/gcc ...") successfully if I manually add -L/sw/lib. The problem is that sudo doesn't know about /sw/lib. Behold:
$ gcc -print-search-dirs | grep sw
libraries: =/lib/i686-apple-darwin9/4.0.1/:/lib/:/usr/lib/i686-apple-darwin9/4.0.1/:/usr/lib/:./i686-apple-darwin9/4.0.1/:./:/sw/lib/i686-apple-darwin9/4.0.1/:/sw/lib/:/usr/lib/gcc/i686-apple-darwin9/4.0.1/:/usr/lib/gcc/i686-apple-darwin9/4.0.1/:/usr/lib/gcc/i686-apple-darwin9/4.0.1/../../../../i686-apple-darwin9/lib/i686-apple-darwin9/4.0.1/:/usr/lib/gcc/i686-apple-darwin9/4.0.1/../../../../i686-apple-darwin9/lib/:/usr/lib/gcc/i686-apple-darwin9/4.0.1/../../../i686-apple-darwin9/4.0.1/:/usr/lib/gcc/i686-apple-darwin9/4.0.1/../../../
$ sudo gcc -print-search-dirs | grep sw
$
How do I tell the sudo version of gcc to look in /sw/lib for libraries? Do I add an environment variable on root's .bash_profile? If so, which one?
UPDATE:
There’s probably a more proper way to do this, but here’s what worked. I created a bash script with this in it:
#!/bin/sh
export LIBRARY_PATH=/sw/lib:$LIBRARY_PATH
./Setup build
And then I ran
$ sudo ./script.sh
That compiled zlib without complaining - hooray! Unfortunately cabal-install is still giving me the error:
$ ./Setup configure
Configuring cabal-install-0.6.2…
Setup: At least the following dependencies are missing:
zlib >=0.4 && <0.6
So I went back to the cabal-install dir (which is what I'm trying to do in the first place), and ran...
$ ./bootstrap.sh
...and that installed everything as expected.
Why you use sudo ever? You should not compile as super user. Compile as normal user and install as super user.
Try setting LDFLAGS=-L/sw/lib.
GHC now comes with an installer for OS X (Leopard, not sure about Tiger). The only issue is that if you use macports or fink, these will probably not see that you have GHC installed and try to install their own version of it.

Resources