2

I have a problem with #include <stdio.h> on mac Mojave 10.14.1

I have a default gcc compiler together with other gcc compilers.

DorothyeMacBook:Desktop dorothy$ which gcc
/usr/local/bin/gcc

The version is

DorothyeMacBook:Desktop dorothy$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-apple-darwin15.6.0/7.1.0/lto-wrapper
Target: x86_64-apple-darwin15.6.0
Configured with: ../gcc-7.1.0/configure --enable-languages=c++,fortran
Thread model: posix
gcc version 7.1.0 (GCC) 

Now I am in the path : /Users/dorothy/Desktop

I have stdio.h in the lib

DorothyeMacBook:Desktop dorothy$ find /usr -name "stdio.h"
find: /usr/sbin/authserver: Permission denied
/usr/local/include/c++/7.1.0/tr1/stdio.h
/usr/local/include/c++/4.9.2/tr1/stdio.h
/usr/local/lib/gcc/x86_64-apple-darwin15.6.0/7.1.0/include/ssp/stdio.h
/usr/local/lib/gcc/x86_64-apple-darwin14.0.0/4.9.2/include/ssp/stdio.h
/usr/local/Cellar/gcc/8.2.0/include/c++/8.2.0/tr1/stdio.h
/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/gcc/x86_64-apple-darwin18.2.0/8.2.0/include/ssp/stdio.h
/usr/local/Cellar/gcc/8.2.0/lib/gcc/8/gcc/x86_64-apple-darwin18.2.0/8.2.0/include-fixed/stdio.h

However when I compile the file under the current path /Users/dorothy/Desktop Terminal will give me an error:

DorothyeMacBook:Desktop dorothy$ gcc  inverse.c -o inv
inverse.c:1:10: fatal error: stdio.h: No such file or directory
 #include <stdio.h>
          ^~~~~~~~~
compilation terminated.
White Thigh
  • 41
  • 1
  • 1
  • 7
  • Seems kind of strange that `gcc` was built only for c++ and fortran but you are trying to use it as a `c` compiler. If you rename `inverse.c` to `inverse.cpp` does it work? – fdk1342 Dec 29 '18 at 23:48
  • `Configured with: ../gcc-7.1.0/configure --enable-languages=c++,fortran` Yeah this looks odd, mine has `c` in this list, and without it I can't tell if `stdio.h` would be supported. Judging from the error message I'd say it is not. – Havenard Dec 30 '18 at 00:16
  • 1
    Did you install xcode? – Shawn Dec 30 '18 at 00:20
  • @JonathanLeffler: Why do you think this us a duplicate? There are many ways software installations can be messed up. The issue there appeared to have been a broken Xcode/Apple tools installation. This question seems to involve some GCC version, which is different (Apple uses Clang now). – Eric Postpischil Dec 30 '18 at 02:24
  • @EricPostpischil: Because the symptoms are the same — upgraded to Mojave and … No `stdio.h` was how I encountered the problem. – Jonathan Leffler Dec 30 '18 at 02:56
  • @JonathanLeffler: The question does not indicate there was an upgrade to Mojave from a previous macOS version, just that 10.14.1 is the version the issue is occurring on. And a comment has identified a likely cause: The GCC installation was configured for C++ and FORTRAN and hence does not support C. – Eric Postpischil Dec 30 '18 at 03:15
  • 2
    See [Can't compile C program on a Mac after upgrade to Mojave](https://stackoverflow.com/questions/52509602/cant-compile-c-program-on-a-mac-after-upgrade-to-mojave) for a probable solution to your problem. Not everyone agrees that your trouble will be resolved by the steps there, though I strongly suspect that what you're seeing is what I saw, and what the OP of that question saw — the system headers have been removed from `/usr/include` by Apple during an upgrade to Mojave. – Jonathan Leffler Dec 30 '18 at 03:45
  • @Jonathan Leffler: Hi, according to your tip, I jumped to the "Can't compile C program on a Mac after upgrade to Mojave ". Then I tried to install the package (macOS_SDK_headers_for_macOS_10.14.pkg) – White Thigh Jan 05 '19 at 06:01
  • @Jonathan Leffler: However, terminal gives me this error:Error: macOS_SDK_headers_for_macOS_10.14.pkg: undefined method `xar!' for Formulary::FormulaNamespace03a5542d706800cf492f2f8aead0a0ff:Module – White Thigh Jan 05 '19 at 06:02
  • @Jonathan Leffler: Could you give me more advice and I was checking the release notes from Apple but not big help. – White Thigh Jan 05 '19 at 06:03
  • Does this answer your question? [GCC fatal error: stdio.h: No such file or directory](https://stackoverflow.com/questions/19580758/gcc-fatal-error-stdio-h-no-such-file-or-directory) – 273K Jul 10 '23 at 08:18

2 Answers2

1

I still have a problem linking gcc with stdio.h on my mac. However, I have found an alternative way to compile the program with gcc. I typed brew install gcc in my terminal, that means a new gcc8.0.2 will be installed.(which is my case, I don't know what gcc you will get) Then I use gcc-8 instead of gcc to compile my program. In this way, the new gcc8.0.2 is invoked.

You can check the command name for gcc variants by going to /usr/local/bin All the names for gcc commands are listed there. As for me, those are

g++                 
g++-8               
gcc                 
gcc-8 

If you have further question, you can contact me and I will give more details.

sungtm
  • 547
  • 3
  • 12
White Thigh
  • 41
  • 1
  • 1
  • 7
0

@Jonathan Leffler is right. Apple made another big cake for us. Since /usr/include has been moved (now it in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include after you install commandline tools), old versions of gcc build cannot find the most bottom level include files. And this is a problem. (What are there in their head) So only you are developer you use /usr/include?

This is NOT Unix-like.

Here is what happened. For gcc-4.9(gcc-4.9 and gcc-9 below are all brew gcc.), the include search paths are:

enter ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
.
/usr/local/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
/usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include
/usr/local/Cellar/gcc@4.9/4.9.4_1/include
/usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include-fixed
/System/Library/Frameworks
/Library/Frameworks
End of search list. here

There is system level file stdio.h, this is why stdio.h cannot be found.

The problem in:

inverse.c:1:10: fatal error: stdio.h: No such file or directory
#include <stdio.h>
      ^~~~~~~~~
compilation terminated.

is:

Refer to /usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include/ssp/stdio.h

#ifndef _SSP_STDIO_H
#define _SSP_STDIO_H 1

#include <ssp.h>
#include_next <stdio.h>

#include_next means include next one name stdio.h, this one is just a guider.

Give a deprecated shot. cp all files in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include to /usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9/gcc/x86_64-apple-darwin17.3.0/4.9.4/include-fixed, gcc-4.9 will work again.

But for gcc-9 the path problem is fine. See:

#include "..." search starts here:
#include <...> search starts here:
.
/usr/local/include
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include # key path
/usr/local/Cellar/gcc/9.1.0/lib/gcc/9/gcc/x86_64-apple-darwin18/9.1.0/include
/usr/local/Cellar/gcc/9.1.0/lib/gcc/9/gcc/x86_64-apple-darwin18/9.1.0/include-fixed
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks
End of search list.

Actually this bug can be simply fixed by reinstall xcode commandline tools, after that gcc-4.9 will work again.

Kelly Hwong
  • 39
  • 1
  • 5