Adding <graphic.h> for :Graphic Programming Using C/C++ in Ubuntu 20.4

Ajit kumar
5 min readJan 20, 2021

I learn graphic programming long back when I used to be a Windows user (those days and even now students in India start with Windows ) and so C programming I learnt with Turbo C compiler. Over time I moved to Linux (Ubuntu and Fedora) and started using GCC for C and G++ for C++ but did not get any chance to work with graphic programming (I mean those line and circle drawing algorithms from text book).

Recently, one of my previous student ask me how to run graphic programs on Ubuntu (because I pushed them to Ubuntu since very beginning i.e. 1st semester :) , although later things rolled back to Windows after me) . This was new to me also so I decided to looked into it. I asked for his program and found he was trying to run program written for Windows machine (still lots of examples on Internet are for Windows ) on his Ubuntu machine. So, I decided to setup environment on my Ubuntu and test those programs with modification. But it turn out due to change in OS version and modules the setup is not so direct. There are tutorial on the installation and setup but either many of them are outdated or did not worked for. So, I tried to solved all issues and collected the information at one place, this post is based on that collection. So, basically this is collected from various overflow links and Internet tutorial. (My contribution is just make it work on my Ubuntu 20.4 machine for GCC 9.3.0 version). Make sure to check version on your system ($gcc — version)

For graphic programming using C in a Linux OS (Ubuntu ), we need to install <graphics.h> libarary that is not by default in most of C compiler (gcc).

Below are summary of various tips/tricks available on Internet to get the working environment ready.

Many of them are old so can not be apply as it so need little bit change.
Source: https://www.geeksforgeeks.org/add-graphics-h-c-library-gcc-compiler-linux/

This is one of good source for installing graphics.h library but while I was trying with Ubnunt 20.4, it was not working as it is (because of update/change in few dependency) So, Basically one need to install libgraph on Ubuntu to get graphics.h working, “If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the implementation of turbo c graphics API on Linux using SDL.”

Source of libgraph: http://download.savannah.gnu.org/releases/libgraph/libgraph-1.0.2.tar.gz
One can follow the steps from below link with below suggested changes. For making process easy I have copied steps from there and presented here with required changes.

Source: https://www.geeksforgeeks.org/add-graphics-h-c-library-gcc-compiler-linux/

Step1: First install build-essential by typing
$sudo apt install build-essential

Step 2: Install some additional packages by typing (In this libesd0-dev is is deprecated from Ubuntu 18.04 onwards so running the command directly will break the installation so run below command instead of original command given in the Geeksforgeeks link.)
Error message: Unable to locate package libesd0-dev

$sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 \
guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev \
libdirectfb-dev libdirectfb-extra libfreetype6-dev \
libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev \
libslang2-dev libasound2 libasound2-dev

Step2.1: Installation of libesd0-dev

Source: https://askubuntu.com/questions/1082722/unable-to-locate-package-libesd0-dev-on-ubuntu-18-04
For this you need to add repositories of xenial, i.e. repository of a previous Ubuntu release which is still supported, in sources.list

Note: nano and gedit are just text editor you can use any text editor of your choice.
Step 2.1.1 : open sources.list with sudo command
$sudo nano /etc/apt/sources.list
or
$sudo gedit /etc/apt/sources.list
Step 2.1.2: past the source of the module in opened file and save. (Below lines are not command but you just have to paste this into the opened file.

deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe

Step 2.1.3 Install libesd0-dev
$sudo apt update && sudo apt install libesd0-dev
STEP 3: Now extract the downloaded libgraph-1.0.2.tar.gz file.
STEP 4: Goto extracted folder and run following command (use cd from terminal to goto the unzip file or use open terminal from the open folder.)
Here the running the make after ./configure will give an error about guile-libgraph.c, so you need to configure the path.
Error: guile-libgraph.c:25:10: fatal error: libguile.h: No such file or directory
Source: https://askubuntu.com/questions/942863/trying-to-install-libgraph
4.1 Install or check for guile-2.0-dev installation
$sudo apt install guile-2.0-dev

4.2 configure the path of the module in configuration script.

$CPPFLAGS=”$CPPFLAGS $(pkg-config — cflags-only-I guile-2.0)” \
CFLAGS=”$CFLAGS $(pkg-config — cflags-only-other guile-2.0)” \
LDFLAGS=”$LDFLAGS $(pkg-config — libs guile-2.0)” \
./configure

4.3 Run make command after success of previous command
$make

//if make result success then go to next step else try to debug that first.
4.4 after configuring all the installation requirement now, install the moudle
$sudo make install
4.5 Now copy the source to lib folder
$sudo cp /usr/local/lib/libgraph.* /usr/lib

This will make the environment ready to work with graphics.h file but if you want to run the code, for example below code,you will get error like:
undefined reference to `initgraph’ ubuntu 20.4 and for all called function.
This is due to some path issue so you can get through it just passing some argument (-lgraph) while compiling the source code. Same is applicable for both C/C++. see the next step for compiling and executing the programming.
Note:
1. Many of example code on Internet are for windows based TurboC compiler so that will not run in Linux environment as it.
2. Drop conio.h else you need to install this also for linux , you can use delay() instead of getch() to stop the screen after execution of code.

Example programs: (Note: Not written by but adopted from Internet source, links given at last)

Other link for example code:
https://www.thecrazyprogrammer.com/2017/01/dda-line-drawing-algorithm-c-c.html
https://www.includehelp.com/code-snippets/c-graphics-program-to-draw-a-line.aspx
http://vbaviskar.blogspot.com/2016/11/program-in-c-in-ubuntu-line-drawing.html

Thank you reading. Hope this will be useful. Let me know if you gets any errors or successfully able to run your program. You can check included links for more details or debug your specific errors.

--

--