Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 22, 2006, 10:43 PM   #1
IntellEJent
macrumors member
 
Join Date: Oct 2006
Opening Files Compiled By GCC

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!
Spencer

P.S. I'm a nub at everything Mac.
IntellEJent is offline   0 Reply With Quote
Old Dec 23, 2006, 07:03 AM   #2
Wes
macrumors 68020
 
Wes's Avatar
 
Join Date: Jun 2001
Location: London
Send a message via ICQ to Wes Send a message via AIM to Wes Send a message via MSN to Wes Send a message via Yahoo to Wes
By open do you mean execute? If so you could do something like:

./file_to_be_executed

I don't know anything xcode though.
Wes is offline   0 Reply With Quote
Old Jan 3, 2007, 05:43 PM   #3
mbabauer
macrumors regular
 
Join Date: Feb 2006
Quote:
Originally Posted by IntellEJent View Post
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!
Spencer

P.S. I'm a nub at everything Mac.
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, etc...it 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 .
__________________
mbabauer
8-Bits me!
http://www.codechimp.net
mbabauer is offline   0 Reply With Quote
Old Jan 3, 2007, 05:59 PM   #4
Mernak
macrumors 6502
 
Join Date: Apr 2006
Location: Boston, MA
Quote:
Originally Posted by Wes View Post
By open do you mean execute? If so you could do something like:

./file_to_be_executed

I don't know anything xcode though.
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
__________________
MB, 2.0Ghz, 2GB Ram, 80GB
iPhone, 8GB
Mernak is offline   0 Reply With Quote
Old Jan 3, 2007, 07:57 PM   #5
bronxbomber92
macrumors regular
 
Join Date: Nov 2006
Quote:
Originally Posted by Mernak View Post
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
Quote:
How do I open compiled C files?
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
bronxbomber92 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
GCC compiled program not working zt0ne Mac Programming 24 Feb 5, 2014 02:28 PM
Opening .lnk files??? davidra Windows, Linux & Others on the Mac 0 Aug 27, 2013 07:12 AM
Preview keeps opening old files walknice OS X 5 Jul 17, 2013 03:35 AM
Searching for text within pdf files -- without opening the files? Fishrrman Mac Applications and Mac App Store 7 Jul 3, 2013 02:20 PM
Opening Files Stumpy25 OS X 10.8 Mountain Lion 2 Apr 24, 2013 12:32 PM

Forum Jump

All times are GMT -5. The time now is 03:01 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC