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.)
To compile C or C++ programs, there is a common command:
make filename
./filename
make will build your source file into an executable file with the same name. But if you want to use the standard way, You could use the gcc compiler to build C programs & g++ for c++
For C:
gcc filename.c
./a.out
For C++:
g++ filename.cpp
./a.out
Add following to get best warnings, you will not regret it. If you can, compile WISE (warning is error)
- Wall -pedantic -Weffc++ -Werror
This is the command that works on all Unix machines... I use it on Linux/Ubuntu, but it works in OS X as well. Type the following command in Terminal.app.
$ g++ -o lab21 iterative.cpp
-o
is the letter O not zero
lab21
will be your executable file
iterative.cpp
is your c++ file
After you run that command type the following in terminal to run your program:
$ ./lab21
Two steps for me:
first:
make foo
then:
./foo
In order to compile and run a cpp source code from Mac terminal one needs to do the following:
gcc main.cpp -o main.out
./main.out