title: Permission denied executing any gcc-compiled c program on OS X (10.8.2) Mountain Lion
body: <p>When executing any C program that I have compiled with <code>gcc</code> from the terminal, I get a permission denied error.</p>
  
  <p>To start, I have verified and repaired permissions on my drive (before doing this, the same problem was happening).</p>
  
  <p>To illustrate and isolate the problem, I'll show you what happens with this ultra-simple Hello World program (with other programs, the same thing occurs):</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  main()
  {
      printf("Hello World");
  }
  </code></pre>
  
  <p>Now, I save this to my desktop as <code>helloworld.c</code>. At this point, from the desktop, an <code>ls -l</code> returns:</p>
  
  <pre><code>total 8
  -rw-r--r--  1 michael  staff  56 Mar 13 14:08 helloworld.c
  </code></pre>
  
  <p>I then compile with <code>gcc -c helloworld.c -o helloworld</code> (I've also tried compiling without the <code>-o</code> flag with the same results). No warnings or errors. An <code>ls -l</code> now returns:</p>
  
  <pre><code>total 16
  -rw-r--r--  1 michael  staff   56 Mar 13 14:08 helloworld.c
  -rw-r--r--  1 michael  staff  724 Mar 13 14:16 helloworld.o
  </code></pre>
  
  <p>Attempting to execute the output of gcc, with <code>./helloworld.o</code> returns:</p>
  
  <pre><code>-bash: ./helloworld.o: Permission denied
  </code></pre>
  
  <p>Just for the sake of debugging, if I execute with sudo (<code>sudo ./helloworld.o</code>), it returns:</p>
  
  <pre><code>sudo: ./helloworld.o: command not found
  </code></pre>
  
  <p>Now, if I attempt to set the executable flag using <code>chmod +x helloworld.o</code>, as was recommended on a lot of search results I've found, <code>ls -l</code> returns:</p>
  
  <pre><code>total 16
  -rw-r--r--  1 michael  staff   56 Mar 13 14:08 helloworld.c
  -rwxr-xr-x  1 michael  staff  724 Mar 13 14:16 helloworld.o
  </code></pre>
  
  <p>However, attempting to execute with <code>./helloworld.o</code> now returns:</p>
  
  <pre><code>-bash: ./helloworld.o: Malformed Mach-o file
  </code></pre>
  
  <p>Now, for debugging sake, <code>where gcc</code> returns:</p>
  
  <pre><code>/usr/bin/gcc
  </code></pre>
  
  <p>So, you can see that I am not using a third party <code>gcc</code>.</p>
  
  <p>Does anyone know what could be the problem? I've tried searching around, but I couldn't find any working solutions. I was having this same problem earlier and have since freshly reinstalled OS X (for different reasons) and am still having this problem with a clean and organized development environment. For reference, I'm on OS X 10.8.2 and have Xcode 4.6 along with the latest version of Xcode Command Line tools (from Apple's developer website). I have not installed gcc from Homebrew or from any other third party source; it is the gcc that came with Xcode Command Line tools.</p>
  
  <p>Thank you very much for your help! I really appreciate you taking your time to read, diagnose and offer your help. :)</p>