Opening Files Compiled By GCC

Discussion in 'Mac Programming' started by IntellEJent, Dec 22, 2006.

  1. IntellEJent macrumors member

    Oct 30, 2006
    How do I open compiled C files? I compiled something with GCC in the terminal. Is it possible to open it? If not, how would I compile the same thing in XCode? Thanks!

    P.S. I'm a nub at everything Mac.
  2. 4409723 Suspended


    Jun 22, 2001
    By open do you mean execute? If so you could do something like:


    I don't know anything xcode though.
  3. mbabauer macrumors regular

    Feb 14, 2006
    This isn't really a "Mac" discussion as much as it is a compiler discussion.

    Let me try to lay some groumd work for you. Your computer only understands one thing...bits. This isn't a condecending statement, but its one that many people dont realize. To say it another way, your computer doesn't know jack about C, C++, Java, SmallTalk, Objective-C, Assembly, etc, only knows binary 1's and 0's.

    So, what you see as a "Language" is actually a human representation of what we want the computer to do. Saying something like:
    int a = 1 + b;
    May look like "Add 1 + b and put it into a" to you, but to the computer its a
    RED<address of b>,<register 1>
    RED<value 1>,<register 2>
    ADD<register 1>,<register 2>,<register 3>
    STO<register 3>,<address of a>
    and so on. And single line of C or C++ code could potentially create thousands and thousands of machine instructions that are moving address pointers, copying memory spots, allocating memory, etc.

    There are basically three types of languages:
    • Compiled languages in which the code is completly and utterly changed from Human-redable to machine code, ex: C/C++, Objective-C, Assembler
    • Interpreted languages that are left in their original state, and interpreted into machine code by some executable at runtime, ex: perl, PHP, Python, Ruby, JavaScript
    • Hybrids that are compiled to some intermediary state, then interpreted the rest of the way to machine code, ex: Java, C#

    In any case, the end goal is to translate the Human-readable text into machine-readable instructions.

    So, lets delve a little deaper into the original language of topic: C.

    C is a completly compiled language. It is compiled in three steps. First, the original file is "parsed", that is it is ran through some language rules to determine if you followed the rules of the language and to process any macros and such. Then the resulting files are "Linked", that is inter-dependecies are satified (your includes), variables are stripped out (computers dont know what 'int a' is, it only knows memory addresses and registers), code optimizations occur, and finally the simple lines you typed are converted into many lines of Assembly code, . Assembly is basically machine code, but in human readable form. Finally, the Assembly code is then futher optimized and "assembled" into the final form, which is an executable binary that can be understood by the computer.

    So, what started out as your file gets tossed and turned and churned several times over until it looks nothing like what you started with.

    There is hope! First off, there are programs called "decompilers", which will take a binary file and convert it back to Assembly, and some even all the way back to the original language. Coverting to Assembly is one thing, as its just readably machine code, optimazations aside, but to convert to the original language, well, that takes some "guess work".

    First problem is that all the variables names have been stripped. So, an 'int a' is no longer 'a', but some offset to a memory address. This is also the case for method names as well. Second, there are several levels of "optimazations" done, which basically swap poor code thats highly readable with faster code that highly cryptic. On top of that, things like for loops and if statements just dont exist to the computer, and end up being converted to memory jumps and compares. So, the decompiler has to "interpret" the machine code to look for patterns that could possibly be an if statement or a for.

    What you end up with is something that is sorta readable, but no where near what it started out with.

    I hope this helps you some. Or, in the least I hope it didn't put you to sleep :) .
  4. Mernak macrumors 6502

    Apr 9, 2006
    Kirkland, WA
    Or you could just double-click the file (the one w/o an extension, not the .c file)

    mbabauer, Wow, nice-mini essay:) very informative, just need to figure out exactly how it is relates to the question. I presume that it would be helpful if he wanted the code from the actual program, but it seems that he has the code because he asked if he should use Xcode.

    P.S. AFAIK Xcode uses gcc for compiling projects. just uses makefiles and stuff
  5. bronxbomber92 macrumors regular

    Nov 23, 2006
    He asked how to open a compiled C file (probably, so he can see the source, which suggests decompiling). So, mbabauer explained the whole process, which eventually led to how to open it, but why it wouldn't look so good ;)

Share This Page