How can I compile/run C or C++ in Unix console or a Mac terminal?
(I know it, forget it, and relearn it again. Time to write it down.)
For running c++ files run below command, Assuming file name is "main.cpp"
1.Compile to make object file from c++ file.
g++ -c main.cpp -o main.o
2.Since #include <conio.h>
does not support in MacOS so we should use its alternative which supports in Mac that is #include <curses.h>
. Now object file needs to be converted to executable file. To use curses.h
we have to use library -lcurses
.
g++ -o main main.o -lcurses
3.Now run the executable.
./main
If it is a simple single source program:
make foo
where the source file is foo.c or foo.cpp, etc.
You dont even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus extension.
Running the executable just built is the same as running any program - but you will most often need to specify the path to the executable as the shell will only search what is in $PATH
to find executables, and most often that does not include the current directory (.
).
So to run the built executable foo
:
./foo
All application execution in a Unix (Linux, Mac OS X, AIX, etc.) environment depends on the executable search path.
You can display this path in the terminal with this command:
echo $PATH
On Mac OS X (by default) this will display the following colon separated search path:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
So any executable in the listed directories can by run just by typing in their name. For example:
cat mytextfile.txt
This runs /bin/cat
and displays mytextfile.txt to the terminal.
To run any other command that is not in the executable search path requires that you qualify the path to the executable. So say I had an executable called MyProgram in my home directory on Mac OS X I can fully qualify it like so:
/Users/oliver/MyProgram
If you are in a location that is near the program you wished to execute you can qualify the name with a partial path. For example, if MyProgram
was in the directory /Users/oliver/MyProject
I and I was in my home directory I can qualify the executable name like this, and have it execute:
MyProject/MyProgram
Or say I was in the directory /Users/oliver/MyProject2
and I wanted to execute /Users/oliver/MyProject/MyProgram
I can use a relative path like this, to execute it:
../MyProject/MyProgram
Similarly if I am in the same directory as MyProgram
I need to use a "current directory" relative path. The current directory you are in is the period character followed by a slash. For example:
./MyProgram
To determine which directory you are currently in use the pwd
command.
If you are commonly putting programs in a place on your hard disk that you wish to run without having to qualify their names. For example, if you have a "bin" directory in your home directory for regularly used shell scripts of other programs it may be wise to alter your executable search path.
This can be does easily by either creating or editing the existing .bash_profile
file in your home directory and adding the lines:
#!/bin/sh
export PATH=$PATH:~/bin
Here the tilde (~) character is being used as a shortcut for /Users/oliver. Also note that the hash bang (#!) line needs to be the first line of the file (if it doesn't already exist). Note also that this technique requires that your login shell be bash (the default on Mac OS X and most Linux distributions). Also note that if you want your programs installed in ~/bin
to be used in preference to system executables your should reorder the export statement as follows:
export PATH=~/bin:$PATH
Running a .C file using the terminal is a two-step process. The first step is to type gcc in the terminal and drop the .C file to the terminal, and then press Enter:
username$ gcc /Desktop/test.c
In the second step, run the following command:
username$ ~/a.out
Ryan, I am changing this to be an answer instead of a comment, since it appears I was too brief. Do all of this in "Terminal".
To use the G++ compiler, you need to do this:
Navigate to the directory in which you stored the *.cpp file.
cd ~/programs/myprograms/
(the ~ is a shortcut for your home, i.e. /Users/Ryan/programs/myprograms/, replace with the location you actually used.)
Compile it
g++ input.cpp -o output.bin
(output.bin can be anything with any extension, really. bin is just common on unix.)
There should be NOTHING returned if it was successful, and that is okay. Generally you get returns on failures.
However, if you type ls
, you will see the list of files in the same directory. For example you would see the other folders, input.cpp and output.bin
From inside the directory, now execute it with ./outbut.bin
Assuming the current directory is not in the path, the syntax is ./[name of the program]
.
For example ./a.out