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

597,160

Solution 1

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

Solution 2

gcc main.cpp -o main.out
./main.out

Solution 3

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

Solution 4

Two steps for me:

First:

make foo

Then:

./foo

Solution 5

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
Share:
597,160

Related videos on Youtube

P-A
Author by

P-A

I love passion, code, milk and my dudette.

Updated on April 04, 2022

Comments

  • P-A
    P-A about 2 years

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

    • Peter Mortensen
      Peter Mortensen almost 2 years
      Associated error messages are often "command not found", "a.out: command not found", or "/a.out: No such file or directory"
  • unwind
    unwind over 15 years
    Good 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.
  • Branan
    Branan over 15 years
    I didn't realize the builtin rules propagated to targets specified when invoking make. Learned something new today =)
  • funk-shun
    funk-shun over 13 years
    as a noob i had so much grief for not including the "./" when executing
  • bitmask
    bitmask over 12 years
    Any chance you mean gcc hello.c -o a.out? Which does the same as gcc hello.c.
  • Ryan
    Ryan over 11 years
    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?
  • Admin
    Admin over 11 years
    add a comment about make wont work because there is no makefile.
  • nerdwaller
    nerdwaller over 11 years
    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.
  • sepp2k
    sepp2k over 11 years
    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.
  • dmgig
    dmgig about 10 years
    "-bash: make: command not found" <-- You have to have the developer tool and the extra components downloaded.
  • Craig McMahon
    Craig McMahon over 9 years
    It's not make main.cpp, but make main.
  • Rachel
    Rachel almost 9 years
    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.
  • Shog9
    Shog9 about 7 years
    Merged from stackoverflow.com/questions/13714436/… (trying to consolidate some basic instructions for this)
  • rotoava
    rotoava about 6 years
    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/…
  • cmarangu
    cmarangu over 3 years
    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
  • Fabian Amran
    Fabian Amran almost 3 years
    What does the ./ do in ./foo?
  • camh
    camh almost 3 years
    @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.
  • HarshDarji
    HarshDarji over 2 years
    I am sorry I am an absolute noob. What is foo here?
  • Peter Mortensen
    Peter Mortensen about 2 years
    Why the space between "-" and "Wall"? Isn't it -Wall?
  • Peter Mortensen
    Peter Mortensen about 2 years
    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
    Peter Mortensen about 2 years
    The link is broken (404).
  • Peter Mortensen
    Peter Mortensen about 2 years
    c++ is actually an executable (at least on Ubuntu MATE 20.04) (Focal Fossa), in the default configuration). It is /usr/bin/c++ -> /etc/alternatives/c++ on that system. Perhaps elaborate 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
    Peter Mortensen about 2 years
    What is the difference between gcc and g++? Perhaps elaborate in your answer? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)
  • Peter Mortensen
    Peter Mortensen about 2 years
    g++ instead of gcc may be required if it is using std::cout (e.g., a Hello, World! program).
  • Peter Mortensen
    Peter Mortensen about 2 years
    macOS changed the default shell to Z shell in macOS v10.15 (Catalina). Does it work for that?
  • Peter Mortensen
    Peter Mortensen about 2 years
    As noted here.