27

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?

6
  • What exactly you need to know? The compilers are supposed to find all the headers if you put the correct.. umm... hearers in your source file.
    – Braiam
    Sep 17, 2014 at 13:27
  • 1
    @Braiam The compiler won't magically find a header file just because you include it. Firstly, clearly that file needs to be installed on you system (typically header files come with *-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 (for gcc or g++). Additionally you usually need the runtime libs for the linking stage (except for header-only libs)... Sep 17, 2014 at 14:34
  • @MalteSkoruppa well, but what you need to use it? Put it in your headers, no?
    – Braiam
    Sep 17, 2014 at 14:37
  • 2
    @Braiam Err... what are you asking? Look, the OP's question is just this: what do I need to do so as to be able to use the 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. Sep 17, 2014 at 14:54
  • @MalteSkoruppa are you reading the body?
    – Braiam
    Sep 17, 2014 at 14:58

2 Answers 2

36

There are several option available to do graphics programming using Ubuntu.

Using SDL

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.

  1. First add the Universe repository (since some required packages are not available in main repository):

    sudo add-apt-repository universe
    sudo apt-get update
    
  2. 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
      
  3. Now extract the downloaded libgraph-1.0.2.tar.gz file.

  4. Go to the extracted folder and run the following command:

    ./configure
    make
    sudo make install
    sudo cp /usr/local/lib/libgraph.* /usr/lib
    
  5. 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
    

Output of Demo 1

Using OpenGL (via GLUT)

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.

  • To install GLUT, open terminal and type sudo apt-get install freeglut3-dev.
  • Here is a simple graphics program using GLUT
/*  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

Output of Demo 2

6
  • 1
    I am using SDL, but i did all stuffs as said, intall libgraph too, but after program run, it halts and ends suddenly with an error [xcb]Unknown sequence number while processing queue Nov 10, 2014 at 17:10
  • which version of Ubuntu you are using?
    – g_p
    Nov 11, 2014 at 1:57
  • i am using ubuntu 14.04 Dec 14, 2014 at 10:40
  • @LuzanBaral, It seems problem with GTK, but you can still run the program.
    – g_p
    Jan 24, 2015 at 14:52
  • 2
    For errors experienced while installing libgraph using SDL refer askubuntu.com/a/995265/719469 and askubuntu.com/a/717262/719469 (will save a lot of your time, saying this by personal experience :P)
    – Kewal Shah
    Jul 28, 2018 at 18:30
4

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!!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .