How can I compile/run C or C++ code in a Unix console or a Mac terminal?
Mac OS X comes with C built into it, and Apple has used C while making every aspect of OS X and iOS. Because C is such a popular language, it even forms the basis of many other programming languages, including two big names: C++ and Objective-C.
If it is a simple single-source program,
make foo
where the source file is foo.c, foo.cpp, etc., you don’t even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus the 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
gcc main.cpp -o main.out
./main.out
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With