fortran compilation on mac osx 10.8.5 - macos

I have a mac 10.8.5. Dont have xcode installed and dont want to install unless needed. using homebrew for installation of most packages.
Default gcc:
gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
installed latest gcc (4.9) using homebrew, using following command on terminal
'brew install gcc'
This installed gcc-4.9 on /usr/local/Cellar
Now gcc , invokes gcc-4.2 and use gcc-4.9 for invoking latest gcc.
I have a hello.f90 file that I need to compile using gcc using
gfortran -o hello hello.f90
this compiles but ./hello gives the following output:
*>>./hello
dyld: Library not loaded: ##HOMEBREW_PREFIX##/lib/gcc/x86_64-apple-darwin12.5.0/4.9.1/libgfortran.3.dylib
Referenced from: /Users/gayathri/work/code/pythonize/fortran/./hello
Reason: image not found
Trace/BPT trap: 5***
I HAVE SET HOMEBREW_PREFIX TO /usr/local/Cellar
I find the libgfortran.3.dylib in the /usr/local/Cellar... path. Why does not gfortran library get located?
My fortran code is simple hello world code:
program hello
implicit none
print *, "hello"
end program hello

Related

How to run c program using GCC not using clang in mac

When I check the version of clang
❯ clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.3.0
when I check the version of gcc
❯ gcc --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.3.0
Here I am not able to use the gcc compiler that I installed using homebrew
❯ ls -l /opt/homebrew/Cellar/gcc
total 0
drwxr-xr-x 15 kumar admin 480 Feb 5 20:30 12.2.0
Please tell me how can I use the gcc(installed using Homebrew) for running any c program.
When I compile any program by writing gcc hello.c ,Then it is similar to clang hello.c Whereas I want to compile the file hello.c with the gcc that is installed using homebrew at /opt/homebrew/Cellar/gcc
You can't "run" any program using gcc, you can only compile and link using gcc.
Your shell determines which program to run according to your PATH. If your PATH contains /usr/bin before /opt/homebrew/bin then you will be running Apple's gcc rather than the homebrew one. You can check your PATH with:
echo $PATH
You can check which gcc will be run when you type gcc without a path by running:
type gcc
Pretty much all homebrew packages link their binaries into /opt/homebrew/bin, so firstly you should set your PATH like this:
export PATH=/opt/homebrew/bin:PATH
That makes your shell look for homebrew packages before Apple ones. You should do that in your login profile so it is set every time you login.
Then you need to check what name your homebrew GCC package has installed gcc under, it might be gcc-10 or gcc-11. You can check with:
ls /opt/homebrew/bin/gcc*
If it is gcc-11, you would compile with:
gcc-11 -o prog program.c
Note that if your code is C++, you will need:
g++-11 -o prog program.cpp

gfortran 12.2.0 not working on M1 Macbook Air with MacOS Ventura 13.1-arm64

gfortran does not work on my M1 Macbook Air with MacOS Ventura 13.1-arm64
When I try to compile any program, e.g.
program main
print *, "Hello world."
end program main
I get the following error message:
(base) BL#MacBook-Air ~ % gfortran hello.f95 -o hello
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status
Only gfortran seems to give an error; I can compile an equivalent C program using gcc just fine.
I tried a few things:
Updated from Ventura 13.0 to 13.1,
Used homebrew to update gcc from the older version which was installed
Unlinked the homebrew version and installed gfortran using https://github.com/fxcoudert/gfortran-for-macOS
But none of these made any difference.
I have the latest version of Xcode installed, so the command line tools are present.
Some details about my setup now:
Xcode is installed, Version 14.2 (14C18)
(base) BL#MacBook-Air ~ % which gfortran
/usr/local/bin/gfortran
(base) BL#MacBook-Air ~ % which gcc
/usr/bin/gcc
(base) BL#MacBook-Air ~ % which ld
/usr/bin/ld
Somewhere somebody suggested including a link, but that gave another problem:
(base) BL#MacBook-Air ~ % gfortran hello.f95 -o hello -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, missing required architecture arm64 in file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd
So there seems to be both a linking problem and a problem with missing support for the M1 chip.
Any suggestions on how to solve this?
I started to suspect that the SDK which I linked to was outdated. I searched for "SDKs" on my disk and found a version which looked newer. So I changed the link to this:
-L/System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib
Now gfortran seems to compile as expected.

Compiling code with gcc on macOS Monterey 12.5.1 results in clang error

I am compiling C++, Fortran, and OpenMPI code using Homebrew installed packages. I also use the make (GNU Make 4.3) and cmake (3.24.1) packages installed with Homebrew.
I compile the test programs as follows:
### C++ ###
int main (int argc, char** argv)
{
int x=2;
return 0;
}
$ g++ test.cc
> clang: error: invalid version number in '-mmacosx-version-min=12.5'
### Fortran ###
program hello
print *, "Hello, world!"
end program hello
$ gfortran test.f90
> clang: error: invalid version number in '-mmacosx-version-min=12.5'
I get a clang error: clang: error: invalid version number in '-mmacosx-version-min=12.5' when using gcc-12 and gfortran-12. The version number requirement in the above error changes depending on the gcc version installed. For example, gcc-10 and gcc-11 result in clang: error: invalid version number in '-mmacosx-version-min=12.0'.
What's interesting is that the error invoked is coming from Apple clang even though I am not using it for compiling any of the programs.
Some relevant specs:
macOS Monterey 12.5.1
xcode-select version 2395
xcode-select path: /Library/Developer/CommandLineTools
$ /usr/bin/clang --version
Apple clang version 13.1.6 (clang-1316.0.21.2.5)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
Update: I now installed gfortran using the intel standalone installer here, I still get the same error.
Someone who seem to have had the same problem:
Found solution: reinstalling the Command Line Tools and exporting SYSTEM_VERSION_COMPAT=1 worked for me. Another possible solution could be to force the compilation to occur through GCC only, but this is not something I tried.
note: compilation was performed without including MPI software.
note2: pkg-config had to be installed as an extra dependency, but this was probably required by CMake.
https://groups.google.com/g/hande-user/c/dtGvyFTJFWE

dyld: Symbol not found: __ZdaPvm - Running KING on Mac OS X

I'm having an issue running KING on Mac OS X . It has to do with a dyld link error, I think. Does anybody have any suggestions on how to fix this error?
Thanks in advance.
> ./king -b ./ex/ex.bed
Returns:
dyld: Symbol not found: __ZdaPvm
Referenced from: /Users/gaelgarcia/Downloads/./king (which was built for Mac OS X 10.13)
Expected in: /usr/local/lib/libstdc++.6.dylib
in /Users/gaelgarcia/./king
Abort trap: 6
I had similar issues trying to use the provided precompiled Mac version. I was able to get a running version built from source running the following in the unzipped source directory:
clang++ -L /opt/local/lib/libomp/ -lm -lz -O2 -fopenmp -o king *.cpp
having first installed libomp in the above directory. I use Macports, so I did this with
port install libomp
For Homebrew users, the recommendation seems to be to simply install llvm, which now includes openmp support directly.
EDIT: Having installed llvm via Homebrew (brew install llvm), the command that got KING properly built and running on my Mac OS 10.12 was:
/usr/local/Cellar/llvm/6.0.0/bin/clang++ -I /usr/local/Cellar/llvm/6.0.0/include -L /usr/local/Cellar/llvm/6.0.0/lib -O2 -fopenmp -lm -lz -o king *.cpp
Running the newly built executable with the example .bed file provided:
> ./king -b ../ex/ex.bed
KING 2.1.3 - (c) 2010-2018 Wei-Min Chen
The following parameters are in effect:
Binary File : ../ex/ex.bed (-bname)
For anyone like me looking for an answer years later, I managed to solve this problem by installing gcc with Homebrew.
brew install gcc
which includes libgfortran. Probably unnecessary, since I had dyld installed in Anaconda, but I found it was the easiest way to get King working on my mac.
Edit: I also had to set my DYLD_LIBRARY_PATH in my bash profile, as well, by going
nano ~/.bash_profile
and adding
export DYLD_LIBRARY_PATH=/opt/local/lib/libgcc/
or wherever the dynamic library libgcc_s.1.dylib is when you search for it on your computer.

GCC-4.2 error on Mac OSX Mountain Lion, unable to install mysql-python

I'm having trouble building MySQLdb on Mac OSX Mountain Lion. After upgrading to OSX Mountain Lion from OSX Lion, I have downloaded and installed Xcode 4.4 also. Then, I went to Preference > Downloads of the Xcode and installed Command Line Tools.
I've downloaded MySQL-python ver. 1.2.3 from http://sourceforge.net/projects/mysql-python/
When I run
python setup.py build
I get below message:
running build
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.6-intel-2.7/MySQLdb
running build_ext
building '_mysql' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -g -O2 -DNDEBUG -g -O3 -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.6-intel-2.7/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64
unable to execute gcc-4.2: No such file or directory
error: command 'gcc-4.2' failed with exit status 1
However, the gcc exists. When I run
gcc
I get
i686-apple-darwin11-llvm-gcc-4.2: no input files
Below is a similar question that I have found but its solution, which is exactly what I have done already, doesn't work for me.
How to install MySQLdb on Mountain Lion
I've had a similar problem while working with Ruby On Rails 3.2.7.
I too had upgraded the system to Mountain Lion, installed Xcode 4.4.1 and downloaded the Command Line Tools.
On the command line I got an error message saying it was impossible to find the file: /usr/bin/gcc-4.2 (I can't paste the precise output right now, I'm sorry).
I did have a /usr/bin/gcc and its version was i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1
I solved the problem by symlinking the file in the same directory and giving it the name the Ruby script was looking for:
sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2
After that, everything worked fine.
Use the following command to make make (or similiar) use the correct gcc:
export CC=/usr/bin/gcc
I ran into this. For me it was because I installed python from a DMG installer at http://python.org . Those are built against the wrong gcc. I fixed it by compiling python from source using Homebrew: Link
brew install python
That links against the correct gcc
(In my specific case I was using an older Python which is why I had used a DMG installer. I discovered that homebrew also has formulas for older versions)
I detoured this problem by using ActivePython.
There's specific installation instruction for MySQLdb here.
Since this is the first time I've used ActivePython I'm not sure if this' a robust solution for this problem. Therefore, I will leave this question open until I make sure this works.

Resources