Mac Terminal

Joined
Nov 1, 2005
Messages
46
Reaction score
0
Points
6
Location
texas
Your Mac's Specs
powerbook g4
How do I compile a program on terminal. I wrote a program and saved it but I tried to compile using the g++ or gcc command but its not working.(Command not found). I saved the file as *.cpp.

Need help.
Thank you.
 
Joined
Aug 27, 2005
Messages
2,406
Reaction score
210
Points
63
Location
Fayetteville, AR
Your Mac's Specs
15" Powerbook G4 • 24" iMac • iPhone 3Gs
Is gcc installed?

If not, you can get it off of the Apple's Developer site, I believe. I think it may be on your install disk, too.
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
Assuming gcc is installed, the simplest way may be to compose a make file and use the make utility.

Here is an example. Assume you have file program.c that you want to compile and link so that the result is the executable "program".

In the same directory as program.c, place a file called Makefile - the "M" should be uppercase. Create this file with your favorite editor and put the following lines in it:

Code:
# Make file to create the "program" executable

program : program.c
	cc -o program program.c

Now, to compile and link in one step, just issue the command:

Code:
make
That's it! Much simpler than manually typing in the whole "cc" command.

BTW, cc is normally aliased to gcc, hence the use of the "cc" command vs. the "gcc" command. Using "cc" instead makes the Makefile more portable, since many *nix systems have a native "cc" and don't need gcc.
 
Joined
Feb 12, 2007
Messages
552
Reaction score
8
Points
18
Location
Northeast US
Your Mac's Specs
MacBook 2GHz/2GB/CD
Install gcc. Navigate to your folder where the .cpp or .h files are stored.

To compile a prog.cpp, for example:

make prog

... That should make an executable.. then type:

./prog

The ./ denotes current directory
 
Joined
Jan 4, 2006
Messages
1,385
Reaction score
146
Points
63
Location
Hamburg, Germany
Your Mac's Specs
MacBook Pro | iMac(2.1 G5) | MacBook(2.16 C2D) | MacMini (1.67 CD) | iPhone 4 | iPad (3rd Gen)
Download and install xcode(It comes with loads of developer tools including gcc). Assume your app. is called hello.cpp

In terminal,
$ g++ hello.cpp -o hello -----> this will create an executable file called hello
$ ./hello -----> this will execute the newly created executable file.
 
Joined
Apr 29, 2006
Messages
4,576
Reaction score
378
Points
83
Location
St. Somewhere
Your Mac's Specs
Mac Studio, M1 Max, 32 GB RAM, 2 TB SSD
stonecold, will "make prog" work if you don't define a Makefile file? I thought that at least a rudimentary Makefile, per my post above, was needed?
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top