How To Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD

How do I compile C program and create an executable file under Linux or UNIX operating systems?

You need GNU project C and C++ compiler for compiling C program and creating an executable file. Most Unix and Linux (*BSD) user start compiling their C program by the name cc. [donotprint][/donotprint]But you can use gcc command to compile program. First make sure you have gcc C compiler installed:

Make Sure Compiler Is Installed On a Unix-like System

Type the following which command or type command or command command to verify that gcc is installed:
$ type -a gcc
$ command -V gcc
$ which gcc

Sample outputs:

/usr/bin/gcc

Find out version of gcc, run:
$ gcc --version
Sample outpust:

gcc (GCC) 4.0.3 20060212 (prerelease) (Debian 4.0.2-9)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Syntax

The syntax is as follows to compile a C program on a Unix-like operating system:
gcc program.c -o program-output
OR
cc program.c -o program-output
OR
make program

Writing Your First C Program Under a Linux / UNIX-like system

Use a text editor such as vi or gedit or nano to create a C program called first.c:
$ vi first.c
Type the following lines (program):

#include <stdio.h>
int main(void){
  printf("My first C programn");
  return 0;
}

How Do I Compile My C Program?

To compile C program first.c, and create an executable file called first, enter:
$ gcc first.c -o first
OR
$ cc first.c -o first
To execute program first, enter:
$ ./first
Output:

My first C program

However, both FreeBSD and Linux support direct make (GNU make utility to maintain groups of programs) command on C program without writing a make file. Remove, first program using the rm command:
$ rm first
$ make first

Output:

cc   first.o   -o first

Execute program first:
$ ./first
Please note that above hack works with GNU/make only.

See also:
This entry is 9 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:

  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.