How to add "graphics.h" C/C++ library to gcc compiler in Linux

In C programming, the graphics.h library provides functions for creating graphics programs. However, this library is not part of the standard C library and requires special installation on Linux systems. Here we'll show how to install and use the graphics library with gcc compiler.

Installation Requirements

Before using graphics.h, you need to install the required packages and compile the libgraph library on your Linux system.

First, install the build essentials and required dependencies −

sudo apt-get install build-essential

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 libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev

After installing dependencies, compile and install the libgraph library −

sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib

Example: Drawing a Circle

Once the graphics library is installed, you can create graphics programs. Here's a simple example that draws a circle −

#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>

int main() {
    int gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);
    circle(40, 40, 30);
    delay(4000);
    closegraph();
    return 0;
}

Compilation

To compile programs using graphics.h, use the following command −

gcc filename.c -lgraph -lm

Key Functions

  • initgraph() − Initializes the graphics system
  • circle() − Draws a circle at specified coordinates with given radius
  • delay() − Pauses execution for specified milliseconds
  • closegraph() − Closes the graphics system
Graphics Output: Circle at (40,40) with radius 30

Conclusion

The graphics.h library enables creation of graphical applications in C on Linux. After proper installation, you can use various drawing functions to create shapes, lines, and other graphics elements in your programs.

Updated on: 2026-03-15T12:45:04+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements