<commit_before>---
layout: post
title:   Compile/Link a Simple C Program 
description: GNU Toolchain, including GCC, is included in all Unixes.
excerpt: 'GNU Toolchain, including GCC, is included in all Unixes.'
date:   2021-07-14 12:00:00
last_updated: 2021-07-14 18:57:00
tags:   Linux
categories: Linux
permalink: /:categories/:title/
comments: false
published: true
lang: en
---

GNU Toolchain, including GCC, is included in all Unixes. It is the standard compiler for most Unix-like operating systems.
Open a Terminal, and enter "gcc --version". If gcc is not installed, the system will prompt you to install gcc.

$ gcc --version
// The GNU C and C++ compiler are called gcc and g++, respectively.
$ g++ --version 
// or

#### Compile/Link a Simple C Program - hello.c

Below is the Hello-world C program hello.c:

{% highlight bash %}
// hello.c
#include <stdio.h>
 
int main() {
    printf("Hello, world!\n");
    return 0;
}
{% endhighlight %}


$ chmod a+x a.out
$ ./a.out


#### Compile/Link a Simple C++ Program - hello.cpp

{% highlight bash %}
// hello.cpp
#include <iostream>
using namespace std;
 
int main() {
   cout << "Hello, world!" << endl;
   return 0;
}

{% endhighlight %}

// Compile-only with -c option

> g++ -c -Wall -g Hello.cpp

o: specifies the output executable filename.

-Wall: prints "all" Warning messages.

-g: generates additional symbolic debuggging information for use with gdb debugger.

The options are:

-c: Compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object files or libraries.

Linking is performed when the input file are object files ".o" (instead of source file ".cpp" or ".c"). GCC uses a separate linker program (called ld) to perform the linking.

#### Compile and Link Multiple Source Files

Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command:

{% highlight bash %}
> g++ -o myprog file1.cpp file2.cpp 
{% endhighlight %}

GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a "gcc -o hello hello.c" is carried out as follows:

Pre-processing: via the GNU C Preprocessor (cpp), which includes the headers (#include) and expands the macros (#define). 

{% highlight bash %}
// The resultant intermediate file "hello.i" contains the expanded source code.
> cpp hello.c > hello.i 
{% endhighlight %}

You need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name.

![compile](/images/gcc/GCC-CompilationProcess.png "compile"){:height="77%" width="77%"}

<commit_msg>commit<commit_after>---
layout: post
title:   Compile/Link a Simple C Program 
description: GNU Toolchain, including GCC, is included in all Unixes.
excerpt: 'GNU Toolchain, including GCC, is included in all Unixes.'
date:   2021-07-14 12:00:00
last_updated: 2021-07-14 18:57:00
tags:   Linux
categories: Linux
permalink: /:categories/:title/
comments: false
published: true
lang: en
---

GNU Toolchain, including GCC, is included in all Unixes. It is the standard compiler for most Unix-like operating systems.
Open a Terminal, and enter "gcc --version". If gcc is not installed, the system will prompt you to install gcc.

$ gcc --version
// The GNU C and C++ compiler are called gcc and g++, respectively.
$ g++ --version 
// or

#### Compile/Link a Simple C Program - hello.c

Below is the Hello-world C program hello.c:

{% highlight bash %}
// hello.c
#include <stdio.h>
 
int main() {
    printf("Hello, world!\n");
    return 0;
}
{% endhighlight %}


$ chmod a+x a.out
$ ./a.out


#### Compile/Link a Simple C++ Program - hello.cpp

{% highlight bash %}
// hello.cpp
#include <iostream>
using namespace std;
 
int main() {
   cout << "Hello, world!" << endl;
   return 0;
}

{% endhighlight %}

// Compile-only with -c option

> g++ -c -Wall -g Hello.cpp

o: specifies the output executable filename.

-Wall: prints "all" Warning messages.

-g: generates additional symbolic debuggging information for use with gdb debugger.

The options are:

-c: Compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object files or libraries.

Linking is performed when the input file are object files ".o" (instead of source file ".cpp" or ".c"). GCC uses a separate linker program (called ld) to perform the linking.

#### Compile and Link Multiple Source Files

Suppose that your program has two source files: file1.cpp, file2.cpp. You could compile all of them in a single command:

{% highlight bash %}
> g++ -o myprog file1.cpp file2.cpp 
{% endhighlight %}

GCC compiles a C/C++ program into executable in 4 steps as shown in the above diagram. For example, a "gcc -o hello hello.c" is carried out as follows:

Pre-processing: via the GNU C Preprocessor (cpp), which includes the headers (#include) and expands the macros (#define). 

{% highlight bash %}
// The resultant intermediate file "hello.i" contains the expanded source code.
> cpp hello.c > hello.i 
{% endhighlight %}

You need to use g++ to compile C++ program, as follows. We use the -o option to specify the output file name.

![compile](/images/gcc/GCC-CompilationProcess.png "compile"){:height="77%" width="77%"}


- References
1. https://sites.google.com/a/case.edu/hpcc/guides-and-training/helpful-references/programming/compiling--linking
2. https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

{'fraction_non_alphanumeric': 0.11373314407381122, 'fraction_numerical': 0.014371894960965224, 'mean_word_length': 3.701417848206839, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1679443', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1654, 'n_words': 761}