Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

How can I compile/run C or C++ code in a Unix console or a Mac terminal?

like image 391
P-A Avatar asked Sep 28 '22 01:09

P-A


People also ask

Can you run C on Mac?

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.


2 Answers

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
like image 227
camh Avatar answered Oct 09 '22 17:10

camh


gcc main.cpp -o main.out
./main.out
like image 128
Andrey Neverov Avatar answered Oct 09 '22 16:10

Andrey Neverov