Gcc how to compile

Gcc how to compile

How to Compile, Link and Build a C Program Using GCC

Prerequisites:
— gcc is installed in your machine
— gcc is registered to PATH
— familiarity with some basic terminal commands since gcc is a CLI tool

Running the program:

Compiling and Linking in a single command:

Hello World!
For our example, let us first create a simple c program and call it hello.c

Now that we’re done coding, we are now ready to build our program.
To build program, we need to first compile our source file to object files
and then link them together into one executable file.

Compiling
To compile, open the terminal and go to the folder where you have saved hello.c
Then type:

After running this command you should have another file in your directory called hello.o.
hello.o is an object file. GCC creates object files for each source file before linking
them all into the final executable. In our case we only have one source file which is hello.c.

If you have multiple source files you can compile by

and you will have an object file for each of your source file.
(In the example above it will be filename1.o and filename2.o)

Linking
In the terminal type:

You should now have an executable file in your directory. By default it is named as a.out on Mac and Linux, and
a.exe on Windows.

In the example above the executable that gcc will produce will be named helloworld instead of the default a.out or a.exe.

Compiling and Linking All in One

Compiling and linking can be done with a single command. Let’s try it with our source file, hello.c.

And that’s it. After running this command we will have an executable named helloworld.

Running the Program

That’s it. You can now use gcc to build your C programs.

Compiling C files with gcc, step by step

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

In order to explain all the steps of compilation, we need to clarify a few programming concepts beforehand. In this article, we will cover what the C language is, how to compile it with a tool like gcc, and what happens when we compile it.

The C programming language

All the softwares, programs, websites and apps are written in a certain programming language. Basically, everything we see on the screen of our computers or smartphones are just a lot of code written in different languages and assembled together in a certain way. Every programming language has a different use, and today we are going to focus on C.

C is a programming language invented by Dennis Ritchie that first appeared in 1972. It’s what we call a low-level language, meaning there is only little abstraction between C and machine language, so it can be considered to be closer to the computer’s hardware. C is also a compiled language, as opposed to interpreted, meaning the source files written in C should be compiled in order for them to be executable.

The tools

Before anything else, let’s talk about the tools we’ll be using in our example. We will be working on a Unix-like operating system, so the examples may vary from Windows. We need to have access to the shell, which is “a program that takes commands from the keyboard and gives them to the operating system to perform” according to http://linuxcommand.org. For this, we need a terminal, or terminal emulator, which is just a window that lets us interact with the shell. Inside the terminal, we should see the shell prompt, that contains your user name and the name of the machine, followed by the PS1 environment variable that is often the character “$”. We are able to input commands after this character in what we call the command line. We also need a text editor, like vi or emacs, to create a source file.

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

Compilation

Compilation is the translation of source code (the code we write) into object code (sequence of statements in machine language) by a compiler.

The compilation process has four different steps:

The compiler we will be using as an example is gcc which stands for GNU Compiler Collection. The GNU project is an free-software and mass-collaboration project launched by Richard Stallman in 1983, allowing developers to have access to powerful tools for free.

Gcc supports various programming languages, including C, is completely free and is the go-to compiler for most Unix-like operating systems. In order to use it, we should make sur we install it on our computer, if it’s not already there.

The source code

For our example, let’s take a look at a source code inside a file called “main.c”, where “.c” is a file extension that usually means the file is written in C. This picture is inside the text editor vi:

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

In pink is the preprocessor directive #include that tells the compiler to include the stdio.h header file, but we will come back to it later.

In blue are comments about the code, these are useful for remembering what your code actually does months after having creating it. We don’t really need them in such a small program, but it’s good practice to put them.

Next we have our entry point, the main() function. It means the program will start by executing the statements that are inside this function’s block, that is between the curly brackets. Here, there are only two statements: one that will print the sentence “Hello, World” on the terminal, and another one that tells the program to “return” 0 if it exited, or ended, correctly. So once we compiled it, if we run this program we will only see the phrase “Hello, World” appearing.

In order for our main.c code to be executable, we need to enter the command “gcc main.c”, and the compiling process will go through all of the four steps it contains. Of course gcc has options that allow us to stop the compiling process after each step. Let’s take a look at them.

The steps

1. The preprocessor

The preprocessor has several roles:

The output of this step will be stored in a file with a “.i” extension, so here it will be in main.i.

In order to stop the compilation right after this step, we can use the option “-E” with the gcc command on the source file, and press enter.

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

This is what the end of the main.i file should look like:

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

2. The compiler

The compiler will take the preprocessed file and generate IR code (Intermediate Representation), so this will produce a “.s” file. That being said, other compilers might produce assembly code at this step of compilation.

We can stop after this step with the “-S” option on the gcc command, and press enter.

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

This is what the main.s file should look like:

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

3. The assembler

The assembler takes the IR code and transforms it into object code, that is code in machine language (i.e. binary). This will produce a file ending in “.o”.

We can stop the compilation process after this step by using the option “-c” with the gcc command, and pressing enter.

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

Our main.o file should look like this (no, it’s not human readable):

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

4. The linker

The linker creates the final executable, in binary, and can play two roles:

By default, after this fourth and last step, that is when you type the whole “gcc main.c” command without any options, the compiler will create an executable program called a.out, that we can run by typing “./a.out” in the command line.

We can also choose to create an executable program with the name we want, by adding the “-o” option to the gcc command, placed after the name of the file or files we are compiling, and pressing enter:

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

Gcc how to compile. Смотреть фото Gcc how to compile. Смотреть картинку Gcc how to compile. Картинка про Gcc how to compile. Фото Gcc how to compile

The GNU Compiler Collection offers many more great tools to compile and operate our programs that would deserve to write about, but this article was about the basic steps of compilation. Maybe next time!

How to Compile and Run C Program in Linux Using gcc?

Steps to Compile and Execute C Program in Linux Using Gcc

Before talking of compiling and running C program in Linux let’s see why C is so popular ever since it was created. He was the Dennis Ritchie who developed C language in 1969 to 1973. C was developed from the beginning as the system programming language for UNIX. Most of the UNIX kernel, and all of its supporting tools and libraries, were written in C. Initially, C was designed to implement the UNIX operating system. Later other folks found it useful for their programs without any hindrance, and they began using it. Even today, C is the first choice for system-level programming. This tutorial explains compilation and execution of C program is in detail.

Kernighan and Ritchie (K & R) in their classic book on C programming language acquaint readers to C language by compiling and executing «Hello World!» C program as follows.

Now, let’s perform all four steps to compile and run C program one by one.

1. Preprocessing

This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the following command internally.

]# cpp helloworld.c > helloworld.i

The result is a file helloworld.i that contains the source code with all macros expanded. If you execute the above command in isolation then the file helloworld.i will be saved to disk and you can see its content by vi or any other editor you have on your Linux box.

2. Compilation

3. Assembly

4. Linking

The actual link command executed by linker is rather complicated. But still, if you passionate enough you can execute the following command to produce the executable file helloworld by yourself.

And, you can greet the universe as follows:

I executed the above command on an x86_64 system having gcc 4.1.2. It may be the above command does not work on your system as it is. It all matters that where the libraries located?

During the whole compilation process there are other files also in role along with the source code file. If you see the very first statement of helloworld.c it is #include (includes header file). Likewise, while compiling a C program you have to work with following types of files.

Program Translation

Last Word

In this tutorial we explained compilation and execution process steps and stages of C program in Linux using gcc. Various phases during compilation and execution process of a C program take place, such as, preprocessing, compilation, assembly, and linking. Hope you have enjoyed reading this tutorial. Please write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

How to Compile and Run C Program in Linux Using gcc?

Before talking of compiling and running C program in Linux let’s see why C is so popular ever since it was created. He was the Dennis Ritchie who developed C language in 1969 to 1973. C was developed from the beginning as the system programming language for UNIX. Most of the UNIX kernel, and all of its supporting tools and libraries, were written in C. Initially, C was designed to implement the UNIX operating system. Later other folks found it useful for their programs without any hindrance, and they began using it. Even today, C is the first choice for system-level programming. This tutorial explains compilation and execution of C program is in detail.

Kernighan and Ritchie (K & R) in their classic book on C programming language acquaint readers to C language by compiling and executing “Hello World!” C program as follows.

Now, let’s perform all four steps to compile and run C program one by one.

1. Preprocessing

This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the following command internally.

]# cpp helloworld.c > helloworld.i

The result is a file helloworld.i that contains the source code with all macros expanded. If you execute the above command in isolation then the file helloworld.i will be saved to disk and you can see its content by vi or any other editor you have on your Linux box.

2. Compilation

3. Assembly

4. Linking

The actual link command executed by linker is rather complicated. But still, if you passionate enough you can execute the following command to produce the executable file helloworld by yourself.

And, you can greet the universe as follows:

I executed the above command on an x86_64 system having gcc 4.1.2. It may be the above command does not work on your system as it is. It all matters that where the libraries located?

During the whole compilation process there are other files also in role along with the source code file. If you see the very first statement of helloworld.c it is #include (includes header file). Likewise, while compiling a C program you have to work with following types of files.

Program Translation

Last Word

In this tutorial we explained compilation and execution process steps and stages of C program in Linux using gcc. Various phases during compilation and execution process of a C program take place, such as, preprocessing, compilation, assembly, and linking. Hope you have enjoyed reading this tutorial. Please write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

How To Compile C and C++ Applications with GCC?

GCC is de facto compiler UNIX and LINUX Operating Systems. GCC is the acronym of the GNU Compiler Collection. As the name Collection suggest GCC supports C, C++, Java, Ada, Go, etc. In this post, we will look at how to compile C and C++ applications.

Installing

By default compiler, related tools are not installed. We can install them from repositories easily like below.

Ubuntu, Debian, Mint, Kali:

As we see GCC is already installed.

CentOS, Fedora, Red Hat:

What Is Compiling A Source Code or Application

Compiling is the process of creating executable files from source code. There are some intermediate states but we do not dive into them. For example to print some messages to the standard output a program is written which is consists of source codes. Then the program is compiled with GCC to create a new executable file that can be run in the Linux. Here is our source code:

Compile C Source Code with GCC

We have the following source code to compile which just prints some text to the standard output.

Set Output Filename

By default after compile operation, the created executable file name will be a.out as we have seen in the previous example. We can specify the executable file name after compilation like below.

Debug Executable with GCC

If our program has some problems with performance or errors we need to debug it. To debug an application it must be compiled with debug options to add some debug data into the binary files. Keep in mind debug information will be made binary slower and bigger in size.

Optimize Code with GCC

Include Libraries During Compilation with GCC

Check Code Quality with GCC

Show GCC Version

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *