Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
Hello all,

I have been using windows for all my programming (for school) for the pasat year, but now i want to keep everything on mac (I would like to install and dual boot ubuntu instead of XP), but Xcode is a pretty heavy program developement environment, and it really slows down my machine!

I hear poeple talking about compiling code in Darwin using GCC. Is there a way to compile and run code form the terminal? that would be so great! If there's a way to do this donot be afraid to elaborate the details.

Qbo

PS. ALeady got the man page for g++ and c++ but i can't make much out of them.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
A Google search would have answered this one quickly, but to get you started:

Create your files using whatever text editor you want. Put them in the same folder.

Open Terminal and type "cd" and press the spacebar. Then drag the folder that has all your code into Terminal. The path of the folder should be added to your input in Terminal. Press Return.

Then you can compile code like so:

Code:
g++ file1.cpp -o file1

g++ is the command line tool to compile C++ sources. If you're only doing C code, use gcc instead.

file1.cpp is the name of your filename.

-o file1 says to name the output file file1

Then you can run your program:

Code:
./file1

Quboid said:
I hear poeple talking about compiling code in Darwin using GCC.

I see dead people... :confused: :p
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Or, it could also be said...

"they're baaaaaack"

...quoting another scary movie, and, pointing out the days of homework are upon us again.

Todd
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Or, it could also be said...

"they're baaaaaack"

...quoting another scary movie, and, pointing out the days of homework are upon us again.

Todd

No worries. We have some new recruits to help handle all the newbies :)
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
Thanks alot guys. I have a slight problem though. When ever I run a program that i wrote in the terminal, after I run it the source code gets all wierd, it turns into somehting like a binary file (I amy not be maknig sence here). But thats what happens. here a sample of how my code looks after running it.

The wierd code doesn't upload properly on the forum. Does anybody have an idea as to what is going on?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
If your source is fine when you start, and it's fine after you compile, and then after you run your compiled program, your original source become corrupt... is it safe to guess that your compiled program is processing your original source file?

If not, what does your program do? Does it work with files at all?

Todd
 

Josh

macrumors 68000
Mar 4, 2004
1,640
1
State College, PA
When you compile your code, GCC is going to create one or more object files (file.o, etc).

Are you opening up one of these .o files when you see the strange code?

For simple command-line programs, I think using the terminal to create and compile code is the easiest/fastest, especially for smaller, less complex programs.

I'd also suggest learning to create and use a Makefile, which will make compiling your code, especially with multiple files, much more streamlined.
 

Quboid

macrumors 6502
Original poster
Oct 16, 2006
441
0
everywhere
I was mistakenly making my source code into the excutable object. Thanks guys. I figured that part out. Thanks guys. Compiling from the terminal rocks. May be coming back to this thread as the need arrises though.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.