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

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

Related

gfortran does not work properly when Conda is installed on MacOs Ventura

I've been trying to get gfortran working on MacOs Ventura(with a M1 chip) for quite some time.
Briefly, the big problem is that gfortran works (when installed via homebrew) until the moment I install Anaconda/conda. After installing Conda, gfortran stops working and I don't know how to make it work correctly. And I need both (conda and gfortran) installed and working.
If I install gcc via homebrew, gfortran works perfectly(version 12.2.0). However, when installing Conda the following message appears when I try to compile something in fortran(in this case it was just a "Hello World"):
gfortran hello.f90 -o trash
ld: unsupported tapi file type '!tapi-tbd' in YAML file '/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/usr/lib/libSystem.tbd' for architecture arm64
collect2: error: ld returned 1 exit status
To get around this, I uninstalled gcc from homebrew and installed it using Conda. I installed the latest available version(11.2.0) using conda install gfortran=11.2.0.
gfortran —version
gfortran: warning: could not understand version 13.02.00
GNU Fortran (GCC) 11.2.0
Copyright (C) 2021 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.
And when I run "which gfortran”, it returns:
/Users/Odilon/opt/anaconda3/bin/gfortran
However, when trying to compile something the following message is displayed(I am using gfortran hello.f90 -o trash, where "trash" is just the name of the executable I chose):
gfortran: warning: could not understand version 13.02.00
ld: -rpath can only be used when targeting Mac OS X 10.5 or later
collect2: error: ld returned 1 exit status
xcode is in the last version(14.2) and Command Line tools is installed.
I need gfortran and conda to be installed and working normally on the MacOS.
How do I resolve these errors?
How can I make gfortran work normally even with Conda installed on the system?
Thanks in advance for the answers and help!

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.

Broken gcc on macOS Mojave

It seems like my gcc is broken.
When I try to install package from PyPI I have this error:
./uwsgi.h:165:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
In file included from core/protocol.c:1:
./uwsgi.h:165:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
XCode command line tools are installed:
➜ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
And gcc from brew installed:
➜ brew install gcc
Warning: gcc 8.2.0 is already installed and up-to-date
To reinstall 8.2.0, run `brew reinstall gcc`
When I try to recognize which gcc is used I have /usr/bin/gcc. I think it's XCode version.
But there is no gcc binary in brew:
➜ ls /usr/local/bin/gcc-*
/usr/local/bin/gcc-8 /usr/local/bin/gcc-ar-8 /usr/local/bin/gcc-nm-8 /usr/local/bin/gcc-ranlib-8
What can I do in this situation? How can I fix gcc?
The correct answer was in this thread: https://stackoverflow.com/a/52530212/1377912
The new Xcode doesn't provide headers by default. You should install it manually:
open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
As I wrote this answer, Mojave is deprecated and now is the age of Big Sur! but this problem still exists!
I don't know that following command will work on Mojave or not, but is working on Big Sur! if you still have this problem, just simply examine that.
Just set CFLAGS(for C compilers) or CXXFLAGS (for C++ compilers ) something like this:
export CFLAGS="-isysroot $(xcrun --sdk macosx --show-sdk-path) -I/usr/include -L/usr/lib -I$(xcrun --sdk macosx --show-sdk-path)/usr/include"
or
export CXXFLAGS="-isysroot $(xcrun --sdk macosx --show-sdk-path) -I/usr/include -L/usr/lib -I$(xcrun --sdk macosx --show-sdk-path)/usr/include"
It probably removes your fatal error: 'stdio.h' file not found error. If this error been removed, it might that you face to new problems, don't count them as this problem and loose your success by manipulating this! In this situation just be care about your new problems and try to fix them, one by one!
I hope this answer be useful for you
Maybe it's a stupid answer but have you tried to create and compile a very simple c file? However in my mojave system i've tried the gcc -v command and i have this output:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-
dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

fortran compilation on mac osx 10.8.5

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

GCC warning "incompatible implicit declaration of built-in function ‘printf’" under Mac OS X Snow Leopard

After a very long time away from C programming, I've decided to try and get familiar with it again. I am trying to compile a hello.c program but am having problems. Here's the code:
#include <stdio.h>
main()
{
printf("Hello\n");
}
And here's the output from gcc:
$ gcc -o hello hello.c
hello.c:1:19: error: stdio.h: No such file or directory
hello.c: In function ‘main’:
hello.c:4: warning: incompatible implicit declaration of built-in function ‘printf’
$
I am working on a Mac running Snow Leopard (10.6.8) and Xcode 3.2.6.
Here's the 'gcc -v' output:
$ gcc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
$
gcc is not able to find stdio.h which is located in /Developer/SDKs/MacOSX10.6.sdk/usr/include. Wondering about how to set an environment variable so I don't have to specify the include path on the command line. I get another error when I specify it on the command line:
$ gcc -I/Developer/SDKs/MacOSX10.6.sdk/usr/include -o hello hello.c
ld: library not found for -lcrt1.10.6.o
collect2: ld returned 1 exit status
$
My LD_LIBRARY_PATH environment variable is:
$ echo $LD_LIBRARY_PATH
/Developer/SDKs/MacOSX10.6.sdk/usr/lib
$
Any help is appreciated.
Thanks,
Keith
You need to re-run the Xcode installer and check the option for UNIX Development:
Without this option you can still use Xcode but you will not get /usr/include etc for gcc command-line builds.
You already have an answer for your particular problem, but I have seen this problem on OS X for a different reason, so this may be helpful to other people.
If you have tried installing a custom compiled version of GCC and it is in /usr/local/bin or you have added a PATH entry to /opt/sw or something similar, you can get this error.
Possible reasons for having the custom GCC but no headers are:
You tried to remove the custom compiled version but forgot to delete the gcc binary.
You used the wrong configure options when building the custom GCC.
You installed the header files to the wrong directory. (Very similar to the last option.)

Resources