Cannot link any C program with gcc on Mac Mojave - c

I've just updated to OSX 10.14.6 (Mojave) and now, after upgrading Xcode and migrating MacPorts, even the simplest C program will not link with any gcc version. For example, linking the program
int main() {}
gives:
dcs16> gcc test.c
ld: library not found for -lSystem
collect2: error: ld returned 1 exit status
I am using gcc 6.5 in /opt/local/bin:
dcs16> gcc --version
gcc (MacPorts gcc6 6.5.0_4) 6.5.0
The Xcode version is 11.0 and I have installed the package (which was recommended for solving some compile problems that other people had):
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
Any help is greatly appreciated.
Note: There is no problem linking with clang.
Note gcc search dirs are:
test> gcc -print-search-dirs
install: /opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/
programs: =/opt/local/libexec/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/libexec/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/libexec/gcc/x86_64-apple-darwin18/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/bin/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/bin/
libraries: =/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/lib/x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../../../x86_64-apple-darwin18/lib/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../x86_64-apple-darwin18/6.5.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin18/6.5.0/../../../

Adding "-L/usr/lib" solves the problem.
Also downgrading from Xcode 11.0 to Xcode 10.3 solves the problem! Note: I also checked Xcode 11.1 which just came out but the problem still exists with 11.1.
Note added: Another programmer informs me that upgrading to OSX 10.15 solves the problem. So this is an incompatibility between OSX 10.14 and Xcode 11.
[Added 28/12/2020] And another programmer informs me that he ran into this problem with Xcode 12.3. His solution was to downgrade to 12.2.

Related

gcc-9 on MacOS Gives ld warning when asan is enabled?

I am running gcc version 9.4 on MacOS Monterey. I am compiling a simple program with asan enabled. Whenever I do compile, I compile successfully, but I get the following warnings:
ld: warning: dylib (/usr/local/Cellar/gcc#9/9.4.0/lib/gcc/9/libasan.dylib) was built for newer macOS version (11.3) than being linked (11.2)
ld: warning: dylib (/usr/local/Cellar/gcc#9/9.4.0/lib/gcc/9/libubsan.dylib) was built for newer macOS version (11.3) than being linked (11.2)
I do not want to upgrade my version of gcc because my remote machine uses gcc 9.4 and I do not have sudo access on that machine to upgrade it.
I have tried using the -L flag along with the following directory:
-L/usr/local/Cellar/gcc#9/9.4.0/lib/gcc/9/, but that also did not get rid of the warning. Is there a way I can fix this warning while also not upgrading my version of gcc?
Edit: gcc was installed via Homebrew

macOS llvm can't find stdio.h anymore

I come to you with a weird problem I've been facing.
So last week, I installed llvm and libomp for a C project, and it compiled just fine with this at the start of my makefile:
CC=clang
CFLAGS = -O2 -fopenmp
CFLAGS += -I/usr/local/opt/libomp/include
LDFLAGS = -fopenmp
LDFLAGS += -L /usr/local/opt/libomp/lib
Today I meant to finish that project, I opened vs code (I don't know if that's relevant), added some code left and right, and when I wanted to compile the code again to test if everything was fine, it gave me
me#macbookPro% make
clang -c -O2 -fopenmp -I/usr/local/opt/libomp/include bubble.c -o bubble.o
bubble.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
make: *** [bubble.o] Error 1
which has left me dumbfounded since it worked just fine a few days ago.
I tried to do an export CPATH with
export CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
but I got a bunch of warning and it didn't work either.
Any help is greatly appreciated !
Info: I'm running the latest version of BigSur and VSCode.
Apparently a macOS update had uninstalled the XCode developer tools.
I ended up uninstalling llvm and libomp, and when reinstalling it told me brew didn't have CLT (use brew config to check that).
Then I ran the classic xcode-select --install then brew install llvm and brew install libomp, edited my path to include the llvm's bin and it works again.
My solution:
masOS12.3.1 monterey
"software update" --> "update commandLineTool" if push --> you can compile correctly without 'stdio.h' file not found.

How can I install gcc 4.2.1 on OSX Sierra?

I'm using the Bochs emulator and for my class we're using gcc 4.2.1. I believe I've gotten Bochs running, but now I need to compile our programs which are compatible with gcc 4.2/.1.
I understand OSX uses an alias for gcc 4.2.1, but how can I use gcc specifically and not clang?
Edit: GCC 4.6.3 not 4.2.1 sorry
You can install previous version of gcc pretty easily using homebrew.
If you have homebrew installed you can get gcc 4.9 by running
brew install gcc#4.9
After it is installed gcc will still map to the clang that came with your mac. The newly installed gcc will be installed at /usr/local/bin and be called something like gcc-4.
You can find the exact executable name using
ls /usr/local/bin | grep gcc
Hopefully 4.9 is close enough to 4.6 for your purposes.

OpenMP support on OSX 10.11, gcc errors with "file omp.h not found"

I have been using gcc version 5.3.0. It says that it comes with openmp support. But every time when I compile a program using either gcc [by terminal] or via xCode 7, I get same error, "file omp.h not found". I have searched too much on this issue and tried almost everything I found.
First I tried to locate omp.h on my mac. I found some files; then in header file, I used that specific location of omp.h but no help [it gave me linker error].
I installed gcc version 6.0 (pre-release) but no help. I tried changing C_INCLUDE_PATH [which is now, and previously set to none] but that didn't helped me as well.
I reinstalled clang-omp but no help.
I am using llvm compiler version 7.0. Although i have installed clang-omp, there is no omp.h in my /usr/include/*
I changed the compiler and now I am able to run it. [It was issue of clang, which I couldn't solve].
I am a student and Intel is giving Intel Parallel Studio 1 year licence for free to students.
So I downloaded, and installed it.
In xCode, under build settings, I set my compiler to 'Intel C/C++ compiler' and in parallalization, I turned it to 'yes'. That was it. Then it compiled successfully. But, note that you won't be using header file 'omp.h' anymore.
By the way, I am still looking for answers, just to know what I was doing wrong.
You can install 'clang-omp' or 'gcc' (corresponds to GCC 5.3 right now) packages via Homebrew, both of which support OpenMP.
The built in GCC is based upon GCC 4.2.1 abs uses LLVM back end via Dragonegg, which is why it doesn't support OpenMP.
As noted already, Intel compilers support OpenMP on Mac.
I don't use Xcode editor so I don't know how to use any of these from there, but all will work from terminal just as they do on Linux.
the compiler on the mac is clang (based on llvm 3.5) which does not support openmp.
you can try install llvm/clang/openmp from source or using prebuild binaries, but I must admit it does not work as advertised for me…
edit unless you use the -fopenmp=libomp flag.

How can I mimic the GNU/linux C development environment on OSX Lion?

I just tried to write a simple C program on OSX Lion
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
Compiling with gcc
$ gcc hello.c
test.c:1:19: error: stdio.h: No such file or directory
test.c: In function ‘main’:
test.c:3: warning: incompatible implicit declaration of built-in function ‘printf’
Ok...fine
$ gcc -I /Developer/SDKs/MacOSX10.6.sdk/usr/include
ld: library not found for -lcrt1.10.6.o
collect2: ld returned 1 exit status
What? Let's see...
$ gcc -I /Developer/SDKs/MacOSX10.6.sdk/usr/include -L/Developer/SDKs/MacOSX10.6.sdk/usr/lib test.c
$ ./a.out
hello world
Finally!
That seems like a lot of effort just to get a hello world working, how do I make gcc find libraries and header files in the MacOSX10.6.sdk directory by default like it does on linux? On linux I find ldd and ldconfig quite useful, they don't seem to exist on OSX...is there an equivalent? What other useful tools are there for developing C on OSX?
Yes, I know xcode makes this easier, but suppose I wanted to use vim and the command line to work on an opensource c project.
I can think of three possibilities:
You had 10.6 installed and upgraded to 10.7. You need to install the new version of Xcode (4.1, get it from the app store) to get back the developer tools.
you installed Xcode, did a custom install, and unchecked "UNIX development" or something. Rerun the installer and install the missing parts.
You installed a broken third-party version of gcc. Try which gcc and see what you get.
On OS X 10.7, if you have downloaded Xcode 4.1 from the Mac App Store and then run the installer that it downloads (in /Applications), your first attempt should have worked just fine assuming you have not set some environment variables that are looked at and influence Apple's gcc tool chain. The object file that is produced in this case would be using the default 10.7 ABI (include files and libs). If you want to produce something that would be compatible with OS X 10.6, then you need to tell the tool chain to use the 10.6 SDK ABI, which is what you did in the third attempt. A standalone project typically handles this by setting up a Makefile to automate building. But, unless you are trying to build something on a version of OS X (10.7 here) that will also run on earlier versions of OS X (say 10.6), there is generally no need to use an SDK.

Resources