How can I compile and run C/C++ code in a Unix console or Mac terminal?
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
How can I run a C program on Mac OS X using Terminal?
First save your program as program.c
.
Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How can you find App Store? Do a "Spotlight Search" by typing ⌘Space and start typing App Store
and hit Enter when it guesses correctly.
App Store looks like this:
Xcode looks like this on App Store:
Then you need to install the command-line tools in Terminal. How can you start Terminal? You need to do another "Spotlight Search", which means you type ⌘Space and start typing Terminal
and hit Enter when it guesses Terminal
.
Now install the command-line tools like this:
xcode-select --install
Then you can compile your code with by simply running gcc
as in the next line without having to fire up the big, ugly software development GUI called Xcode:
gcc -Wall -o program program.c
Note: On newer versions of OS X, you would use clang
instead of gcc
, like this:
clang program.c -o program
Then you can run it with:
./program
Hello, World!
If your program is C++, you'll probably want to use one of these commands:
clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
How to compile and run C program on Mac OS X
You need to add a dot to indicate that the executable is in the current directory, as the current directory is not in the path:
./a.out
How to compile a C program with make on Mac OS X Terminal
Create a file called Makefile on the same path with this content:
CC = cc
CFLAGS = -std=c99 -pedantic -Wall
OBJECTS = filename.o
all: appname
filename.o: filename.c
$(CC) $(CFLAGS) -c filename.c
appname: $(OBJECTS)
$(CC) $(OBJECTS) -o appname
clean:
rm -f *.o appname
Then run:
make
Of course, replace appname
with the name of your program.
Note: There must be a "tab" (not spaces) before
$(CC) $(CFLAGS) -c filename.c
and
$(CC) $(OBJECTS) -o appname
and
rm -f *.o appname
Run and compile C program using ncurses.h in Mac Terminal from Eclipse
The default Eclipse terminal does not support ANSI escapes; this question was asked before in Support ANSI terminal color escape sequences in XML?, indicating that there is a plugin which may help (see Eclipse plugin – ANSI Escape in Console).
Run a c program in terminal on mac
The default filename is a.out
. If you want to run it, just type ./a.out
.
If you want a different filename use -o
.
That is, run gcc -o MyOutName <filename>
.
C++ Compile code on Mac OS X and run it on Linux x86
You don't need to install Boost in order to use it; especially if you're using Boost libraries that are entirely contained in header files, which is the majority of Boost's libraries. You only need to include those Boost headers with your source code and it should compile just fine.
For an easy way to bundle the needed Boost headers together with your source code, have a look at Boost's BCP tool.
Easy: Passing data to c program from terminal (Mac)
It sounds like you are looking for argv
, which I suppose is difficult to search for if you don't know what it is called! This isn't specific to Mac OS X's Terminal.
The argv
argument of a main()
function is an array of strings; its elements are the individual command line argument strings.
The path to the program being run is the first element of argv
, that is argv[0]
.
The number of elements in argv
is stored in argc
:
#include <stdio.h>
int main(int argc, char* argv[])
{
int arg;
for (arg = 0; arg < argc; ++arg)
{
printf("Arg %d is %s\n", arg, argv[arg]);
}
return 0;
}
Compile:
% gcc program_name.c -o program_name
Run:
% ./program_name 19982
Arg 0 is ./program_name
Arg 1 is 19982
Converting argv[1]
to an int
is left as an exercise.
Related Topics
Why Is It Allowed to Pass R-Values by Const Reference But Not by Normal Reference
Why Does Windows 10 Start Extra Threads in My Program
Using Class Name in a Class Template Without Template Parameters
Static Variables in an Inlined Function
C++11: How to Alias a Function
Difference in Performance Between Msvc and Gcc for Highly Optimized Matrix Multplication Code
A How to Create a Matrix in C++
Allocating More Memory Than There Exists Using Malloc
Linking Libstdc++ Statically: Any Gotchas
Is There a Range Class in C++11 for Use with Range Based for Loops
How to Run a Child Process That Requires Elevation and Wait
C++11 Inheriting Constructors and Access Modifiers
Cpp - Valgrind - Invalid Read of Size 8
Preparation for Std::Iterator Being Deprecated
Performance of Qsort VS Std::Sort
When Is Overloading Pass by Reference (L-Value and R-Value) Preferred to Pass-By-Value