Mac Forums

Mac Forums (
-   OS X - Development and Darwin (
-   -   Mac Terminal (

nolgster 02-23-2007 11:19 PM

Mac Terminal
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.

surfwax95 02-24-2007 01:14 AM

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.

mac57 02-24-2007 07:14 PM

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:


# 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:


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.

DoubleCap 03-01-2007 11:03 PM

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:


The ./ denotes current directory

novicew 03-02-2007 02:27 AM

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.

mac57 03-02-2007 10:35 AM

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?

All times are GMT -4. The time now is 09:13 PM.

Powered by vBulletin
Copyright ©2000 - 2015, Jelsoft Enterprises Ltd.