How can I compile/run C or C++ code in a Unix console or a Mac terminal?
19 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

- 30,738
- 21
- 105
- 131

- 40,988
- 13
- 62
- 70
-
I didn't realize the builtin rules propagated to targets specified when invoking make. Learned something new today =) – Branan Oct 21 '08 at 17:04
-
"-bash: make: command not found" <-- You have to have the developer tool and the extra components downloaded. – dmgig Apr 30 '14 at 15:20
-
27
-
1
-
3@FabianAmran It refers to the current directory. The shell will look only in the directories listed in the `$PATH` environment variable for programs to execute unless a path is specified when running the program. `.` (current directory) is often not in `$PATH` for security reasons. – camh Jul 09 '21 at 05:54
gcc main.cpp -o main.out
./main.out

- 30,738
- 21
- 105
- 131

- 2,135
- 1
- 12
- 21
-
36as a noob i had so much grief for not including the "./" when executing – funk-shun Jan 26 '11 at 06:00
-
15I used "gcc main.cpp -o main.out", and get this error, Undefined symbols for architecture x86_64: "std::__1::locale::use_facet(std::__1::locale::id&) const", ... turns out the reason is, gcc default-links is libc. while using g++ will link with libstdc++. So use "g++ main.cpp -o main.out" may be better. – Rachel Jul 28 '15 at 03:18
-
4About ```Undefined symbols for architecture x86_64 ``` issue, I modify the command as follows: ```gcc -lstdc++ main.cpp -o main.out```, and that works on my Mac. via link:https://stackoverflow.com/questions/11852568/gcc-4-8-on-mac-os-x-10-8-throws-undefined-symbols-for-architecture-x86-64?lq=1 – rotoava Mar 06 '18 at 09:08
-
`g++` instead of `gcc` may be required if it is using `std::cout` (e.g., a *Hello, World!* program). – Peter Mortensen Apr 03 '22 at 13:44
-
As noted [here](https://stackoverflow.com/questions/28236870/undefined-reference-to-stdcout#comment44834869_28236870). – Peter Mortensen Apr 03 '22 at 14:02
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 the terminal to run your program:
./lab21

- 30,738
- 21
- 105
- 131

- 3,662
- 7
- 33
- 57
Two steps for me:
First:
make foo
Then:
./foo

- 30,738
- 21
- 105
- 131

- 2,406
- 24
- 20
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

- 30,738
- 21
- 105
- 131

- 13,234
- 14
- 63
- 73
-
looks like a great idea EDIT: nvm me being ironic. just all the $PATH stuff remind me of Windows "environment variables" which you're not supposed to mess around with too much – cmarangu Jan 29 '21 at 02:22
-
Untangled: *"It looks like a great idea. EDIT: never mind - it was me being ironic. Just all the $PATH stuff remind me of Windows "environment variables" which you're not supposed to mess around with too much."* – Peter Mortensen Feb 19 '22 at 11:27
-
macOS changed the default shell to [Z shell](https://en.wikipedia.org/wiki/Z_shell) in [macOS v10.15](https://en.wikipedia.org/wiki/MacOS_Catalina) (Catalina). Does it work for that? – Peter Mortensen Apr 03 '22 at 13:47
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. Extension .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.binFrom inside the directory, now execute it with
./outbut.bin

- 30,738
- 21
- 105
- 131

- 1,813
- 1
- 18
- 19
-
Thanks this worked. Would you happen to know of any tutorials that would explain these things? Like what exactly G++ means, what the 'o' switch is and anything else that might come up? – Ryan Dec 04 '12 at 23:41
-
-
Ryan, you can type "man g++" in the terminal to get the "man pages" (i.e. manuals, which I would guess Apple has embedded). I haven't seen any great tutorials though. Although, the man page on g++ may get pretty in depth with CFLAGS and all sorts of advanced compiling options. – nerdwaller Dec 04 '12 at 23:45
-
Nitpick: "There should be NOTHING [printed] if it was successful, and that is okay. Generally you get [output] on failures." There will always be a return value, 0 on success, non-0 on failure. – sepp2k Dec 05 '12 at 00:42
-
Merged from http://stackoverflow.com/questions/13714436/hello-world-in-c-says-nothing-more-to-be-done (trying to consolidate some basic instructions for this) – Shog9 Feb 12 '17 at 18:07
A compact way to go about doing that could be:
make foo && ./$_
It is nice to have a one-liner so you can just rerun your executable again easily.

- 30,738
- 21
- 105
- 131

- 4,391
- 2
- 25
- 38
Assuming the current directory is not in the path, the syntax is ./[name of the program]
.
For example ./a.out
-
2Good for you! The dot and slash are there because on many systems, the current directory ("." in Unix terms) is not part of the path searched by the shell. Thus, adding it makes it explicit which program you want to run. – unwind Oct 21 '08 at 08:49
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 and g++
for C++.
For C:
gcc filename.c
./a.out
For C++:
g++ filename.cpp
./a.out

- 30,738
- 21
- 105
- 131

- 1,026
- 9
- 12
Add the following to get the best warnings, and you will not regret it. If you can, compile using WISE (warning is error).
- Wall -pedantic -Weffc++ -Werror

- 30,738
- 21
- 105
- 131

- 8,502
- 4
- 40
- 42
-
Why the space between "-" and "Wall"? Isn't it [`-Wall`](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall)? – Peter Mortensen Feb 19 '22 at 11:17
Step 1 - create a cpp file using the command
touch test.cpp
Step 2 - Run this command
g++ test.cpp
Step 3 - Run your cpp file
./a.out

- 30,738
- 21
- 105
- 131

- 111
- 1
- 7
I am on a new MacBook Pro with the Apple M1 Pro chip. I have my Xcode installed - both IDE and command line tools. This is how it worked for me:
g++ one.cpp -o one
./one

- 30,738
- 21
- 105
- 131

- 51
- 1
Use a makefile
. Even for very small (= one-file) projects, the effort is probably worth it because you can have several sets of compiler settings to test things. Debugging and deployment works much easier this way.
Read the make
manual. It seems quite long at first glance, but most sections you can just skim over. All in all, it took me a few hours and made me much more productive.

- 30,738
- 21
- 105
- 131

- 530,221
- 131
- 937
- 1,214
Just enter in the directory in which your .c/.cpp file is.
For compiling and running C code.
gcc filename.c
./a.out filename.c
For compiling and running C++ code.
g++ filename.cpp
./a.out filename.cpp

- 30,738
- 21
- 105
- 131

- 172
- 1
- 11
In order to compile and run C++ source code from a Mac terminal, one needs to do the following:
- If the path of .cpp file is somePath/fileName.cpp, first go the directory with path somePath
- To compile fileName.cpp, type
c++ fileName.cpp -o fileName
- To run the program, type
./fileName

- 30,738
- 21
- 105
- 131

- 131
- 1
- 3
-
`c++` *is* actually an executable (at least on [Ubuntu MATE 20.04](https://en.wikipedia.org/wiki/Ubuntu_MATE#Releases)) (Focal Fossa), in the default configuration). It is `/usr/bin/c++ -> /etc/alternatives/c++` on that system. Perhaps [elaborate](https://stackoverflow.com/posts/56603676/edit) a little bit in your answer, e.g. linking to official documentation (as it hasn't been covered in other answers - they use `g++`)? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Feb 19 '22 at 12:47
I found this link with directions:
http://www.wesg.ca/2007/11/how-to-write-and-compile-c-programs-on-mac-os-x/
Basically you do:
gcc hello.c
./a.out (or with the output file of the first command)

- 30,738
- 21
- 105
- 131

- 609
- 1
- 6
- 13
-
2Any chance you mean `gcc hello.c -o a.out`? Which does the same as `gcc hello.c`. – bitmask Dec 22 '11 at 18:53
-
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:
gcc /Desktop/test.c
In the second step, run the following command:
~/a.out

- 30,738
- 21
- 105
- 131

- 4,779
- 2
- 36
- 29
For running C++ files, run the below command, assuming the file name is "main.cpp".
Compile to make an object file from C++ file.
g++ -c main.cpp -o main.o
Since
#include <conio.h>
is not supported on macOS, we should use its alternative which is supported on Mac. That is#include <curses.h>
. Now the object file needs to be converted to an executable file. To use file curses.h, we have to use library-lcurses
.g++ -o main main.o -lcurses
Now run the executable.
./main

- 30,738
- 21
- 105
- 131

- 2,219
- 23
- 28
You need to go into the folder where you have saved your file.
To compile the code: gcc fileName
You can also use the g++ fileName
This will compile your code and create a binary.
Now look for the binary in the same folder and run it.

- 41
- 1
- 6
-
What is the difference between `gcc` and `g++`? Perhaps [elaborate](https://stackoverflow.com/posts/65272226/edit) in your answer? (But ***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today.) – Peter Mortensen Feb 19 '22 at 13:21