How to compile and run C/C++ in a Unix console/Mac terminal?


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.)

1/11/2018 8:35:44 AM

Accepted Answer

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:

4/9/2018 8:47:22 PM

gcc main.cpp -o main.out  

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow