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

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 :

31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Oct 21, 2008 at 9:10 42.2k 13 13 gold badges 63 63 silver badges 72 72 bronze badges

I didn't realize the builtin rules propagated to targets specified when invoking make. Learned something new today =)

Commented Oct 21, 2008 at 17:04

"-bash: make: command not found" Commented Apr 30, 2014 at 15:20 It's not make main.cpp , but make main . Commented Jan 5, 2015 at 15:51 What does the ./ do in ./foo? Commented Jul 9, 2021 at 2:54

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

Commented Jul 9, 2021 at 5:54
gcc main.cpp -o main.out ./main.out 
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Oct 21, 2008 at 8:46 Andrey Neverov Andrey Neverov 2,155 1 1 gold badge 12 12 silver badges 21 21 bronze badges as a noob i had so much grief for not including the "./" when executing Commented Jan 26, 2011 at 6:00

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

Commented Jul 28, 2015 at 3:18

About 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:stackoverflow.com/questions/11852568/…

Commented Mar 6, 2018 at 9:08 g++ instead of gcc may be required if it is using std::cout (e.g., a Hello, World! program). Commented Apr 3, 2022 at 13:44 Commented Apr 3, 2022 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 
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Feb 24, 2011 at 18:02 3,764 7 7 gold badges 34 34 silver badges 57 57 bronze badges

Two steps for me:

make foo 
31.6k 22 22 gold badges 109 109 silver badges 132 132 bronze badges answered Jul 28, 2014 at 11:52 Victor Augusto Victor Augusto 2,416 25 25 silver badges 20 20 bronze badges I am sorry I am an absolute noob. What is foo here? Commented Jan 11, 2022 at 10:07

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:

On Mac OS X (by default) this will display the following colon separated search path:

So any executable in the listed directories can by run just by typing in their name. For example:

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:

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:

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:

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:

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