Is there any Linux based compiler which supports graphics.h
library?
I want to implement graphic programs, so please kindly let me know if there is any such software.
If not then how can I use it?
There are several option available to do graphics programming using Ubuntu.
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.
It is not very powerful and suitable for production quality application, but it is simple and easy-to-use for learning purpose.
You can download it from here.
First add the Universe repository (since some required packages are not available in main repository):
sudo add-apt-repository universe
sudo apt-get update
Second install build-essential and some additional packages:
For versions prior to 18.04:
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 build-essential
For 18.04: From Ubuntu 18.04 guile-2.0
works and libesd0-dev
is deprecated. For this you need to add repositories of xenial
in sources.list
.
sudo nano /etc/apt/sources.list
Add these lines:
deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe
Run sudo apt-get update
.
Then install packages using:
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
Now extract the downloaded libgraph-1.0.2.tar.gz
file.
Go to the extracted folder and run the following command:
./configure
make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib
Now you can use #include<graphics.h>
on Ubuntu and the following line in your program:
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
Here is a sample program using graphics.h
:
/* demo.c */
#include <graphics.h>
int main()
{
int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x= 300,y=150,radius=50;
initgraph(&gd,&gm,NULL);
rectangle(left, top, right, bottom);
circle(x, y, radius);
bar(left + 300, top, right + 300, bottom);
line(left - 10, top + 150, left + 410, top + 150);
ellipse(x, y + 200, 0, 360, 100, 50);
outtextxy(left + 100, top + 325, "C Graphics Program");
delay(5000);
closegraph();
return 0;
}
To compile it use
gcc demo.c -o demo -lgraph
To run type
./demo
Although OpenGL is basically made for 3D programming, drawing 2D shapes gives the basic outline and introduction to OpenGL and gives the idea about how to start drawing objects in OpenGL.
sudo apt-get install freeglut3-dev
./* demo.c */
#include <GL/gl.h>
#include <GL/glut.h>
#include <GL/glu.h>
void setup() { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); }
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f, 0.0f, 0.0f);
glRectf(-0.75f,0.75f, 0.75f, -0.75f);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800,600);
glutCreateWindow("Hello World");
setup();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Compile it using
gcc demo.c -o demo -lglut -lGL
Run it using
./demo
If you want to use graphics.h in ubuntu or any other linux distro, then I prefer libxbgi. It can do almost all the things that you expect from the graphics.h for windows. You can download it from here: http://libxbgi.sourceforge.net/
Otherwise if you want to do some high end graphics then you are there for SDL(which is mostly for programming video games) and OpenGL(which is for 3D graphics). You can also use a mixture of the two. One example is the game briquolo(spelling may be wrong).
HAPPY GRAPHICS PROGRAMMING!!
*-dev
packages on Linux distros) - the compiler won't download it for you. Secondly, that header file needs to either be in some standard location where a compiler will look for it (such as/usr/include
), or else that location has to be specified with-I
on the command line (forgcc
org++
). Additionally you usually need the runtime libs for the linking stage (except for header-only libs)...graphic.h
library on Ubuntu? It's perfectly clear. An appropiate answer will explain what packages are needed and how to install them (as the existing answer does), and possibly what parameters are needed on the command line at compile time.