Let us learn how to download and install Graphics.h in Ubuntu operating system. This step by step guide will enable you to install graphics.h package libgraph in Linux Ubuntu OS.
If you want to execute C programs with graphics.h in Ubuntu, you will have to compile and install libgraph package.
What is graphics.h header file?
The graphics.h header file is used to display different kinds of visualisations in C programming.
It can help display 2-dimensional images, different types of fonts, animations and much more on the graphical side.
There are different pre-defined functions available in graphics.h header file such as:
- drawImage(filename, x, y)
- initGraphics(width, height)
- drawLine(x0, y0, x1, y1)
- drawArc(x, y, width, height, start, sweep)
- drawRect(x, y, width, height)
- fillArc(x, y, width, height, start, sweep)
- drawOval(x, y, width, height)
What is libgraph package?
The libgraph package is a simple and easy 2-dimensional graphics interface used to perform visualisations. It is also known as WinBGIM library.
The libgraph library is an implementation of the Turbo C graphics API which is graphics.h on Linux using Simple DirectMedia Layer (SDL).
If you encounter SDL version 1.2.0 not found error, visit Simple DirectMedia Layer official website. Please find how to install SDL library in Ubuntu at the bottom.
The GNU GCC compiler, which is the default C and C++ Compiler in Ubuntu does not include graphics.h header file in its directory by default.
So, the inclusion of the preprocessor directive #include<graphics.h> does not work. It will give an error – graphics.h not found in Ubuntu.
Therefore, we need to know how to add graphics.h in Ubuntu OS to enable graphics in C programming language.
The graphics.h is a header file in C programming that helps you to include methods related to graphics in your C Program.
In addition to the graphics.h library in C programming, there are many libraries that offer functionalities to develop graphics programs in Linux.
The libgraph library, to be frank, is an age-old library. You must use the latest graphics libraries such as OpenGL, which is a high-performance graphics library.
How To Run Graphics.h in Linux Ubuntu?
In short, download and copy the libgraph-1.0.2.tar.gz to your USR directory. Extract it and run your C program with #include<graphics.h> statement. Find step by step guide below.
Steps to Install graphics.h in Ubuntu in C programming
1. Download libgraph package file for Ubuntu operating system
Click on the button to download libgraph package for Ubuntu operating system.
2. Install build-essentials
sudo apt-get install build-essential
3. Install packages to run graphics.h header file
You need to install an entire list of packages which can be executed in a single statement as follows.
sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev
4. Extract the libgraph.tar.gz package file
You can extract a file in Linux using two methods:
- Right-click on the compressed file and extract it in the same folder
- Execute a Linux command to untar the libgraph file
tar -xvzf libgraph.tar.gz
5. Copy extracted libgraph folder into USR directory
sudo cp -r libgraph-1.0.2 /usr/local/lib
6. Compile a C program with graphics.h header file
The following line of code will help you to understand how to compile graphics.h in Ubuntu 14.04 operating system.
gcc test.c -lgraph
Note: This graphics C program is compiled with GNU GCC compiler on Ubuntu 14.04 operating system. However, these codes are compatible with all other operating systems.
How To Install SDL Library in Linux Ubuntu?
It is very easy to download and install the SDL graphics library in your Ubuntu operating system. Please follow the steps below.
Step 1: Start your Linux terminal and type the following command.
sudo apt-get install libsdl2-dev
Step 2: Run your C program with the following statement.
gcc armstrong_number.c -lSDL2
Sample Graphics Program in C Programming Language
int gd = DETECT, gm;
int left = 100, right = 200, top = 100, bottom = 200;
printf("\nLet's build a sample rectangle using Graphics in C\n");
initgraph(&gd, &gm, "C:\\TC\\BGI");
rectangle(left, top, right, bottom);
Let’s discuss more on how to install graphics.h in Ubuntu OS in the comment section below if you have any compilation errors or doubts about the same.