Macos – Permission Denied error when trying to compile hello world C program in Mac terminal

cmacospermission-deniedterminal

I'm trying to compile a hello world c program in the mac terminal and I am generating these errors:

mysource.c:1:19: error: /usr/local/include/stdio.h: Permission denied
mysource.c: In function ‘main’:
mysource.c:3: warning: incompatible implicit declaration of built-in function ‘printf’

This is what I typed into the terminal (mysource is the name of the .c file):

MacBook-Pro:~ drummer0014$ gcc mysource.c -o mysource

I had xcode 3 installed and generated the same error so I just installed xcode 4.2 today and am having the same error. I am on a macbook running snow leopard.
I also tried typing gcc and then dragging the file into the terminal so that it would have the full path but I get the same error. Any help would be greatly appreciated.

When I typed gcc –verbose mysource.c -o mysource
Here is what I received:

    Using built-in specs.
    Target: i686-apple-darwin10
    Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.1~3/src/configure --disable-         checking --enable-werror --prefix=/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --enable-         llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.1~3/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
    gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
     /usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin10/4.2.1/cc1 -quiet -v -imultilib    x86_64 -iprefix /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin10/4.2.1/ -D__DYNAMIC__ mysource.c -fPIC -quiet -dumpbase mysource.c -mmacosx-version-min=10.6.8 -m64 -mtune=core2 -auxbase mysource -version -o /var/folders/7r/7rMZhHx3F0WhnoyEK1zUgE+++TI/-Tmp-//ccaqeJkd.s
ignoring nonexistent directory "/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/include"
ignoring nonexistent directory "/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin10/4.2.1/../../../../i686-apple-darwin10/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin10/4.2.1/include
     /usr/local/include
     /Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin10/4.2.1/include
     /usr/include
     /System/Library/Frameworks (framework directory)
     /Library/Frameworks (framework directory)
    End of search list.
    GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00) (i686-apple-    darwin10)
    compiled by GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00).
    GGC heuristics: --param ggc-min-expand=150 --param ggc-min-heapsize=131072
    Compiler executable checksum: e787fa4ffdc9e78ad5e913828c220d85
    mysource.c:1:19: error: /usr/local/include/stdio.h: Permission denied
    mysource.c: In function ‘main’:
    mysource.c:3: warning: incompatible implicit declaration of built-in function ‘printf’

Best Answer

STATUS_ACCESS_DENIED is sort of correct, but I wouldn't change the permissions on /usr/local just yet. On a typical Mac OS X installation, /usr/local is not used for anything. If anything goes there, it's third-party software.

So, the real question is why GCC is looking for (and finding) headers there. I suspect it's because you're not running the GCC from Xcode, but that you have a third-party version of GCC in /usr/local.

What does which gcc report? What does echo $PATH show?

When you installed Xcode, did you install the UNIX Development component? If not, then you have to access the tools in /Developer/usr/bin, either explicitly or by adding that directory early in your PATH. If you did install that component, then make sure that /usr/bin and /usr/sbin are earlier in your PATH than /usr/local/bin or /usr/local/sbin.


Update:

Hmm, the interesting part of your gcc --verbose output is:

#include "..." search starts here:
#include <...> search starts here:
 /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin10/4.2.1/include
 /usr/local/include
 /Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin10/4.2.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.

I just checked on my system and llvm-gcc-4.2 (the compiler that's actually being used) produces the same search list here. So, I guess it's normal that the compiler would look in /usr/local/include for headers – and look there before /usr/include – it's just abnormal that you would have any headers there.

My recommendation is that you move aside /usr/local:

sudo mv /usr/local /usr/local.bak

or at least /usr/local/include and /usr/local/lib:

sudo mv /usr/local/include /usr/local/include.bak
sudo mv /usr/local/lib /usr/local/lib.bak

Whatever third-party software originally installed that stuff may break, but at least then you'd know what it was. More likely, you will never notice any problems from doing that.

Related Topic