11

I have recently started learning graphics in C++.

I tried #include <graphics.h> in my program in codeblocks but it shows error. Then I downloaded graphics.h header from a site and pasted in the include folder in codeblocks, yet it shows graphics.h:No such file or directory.

Can anyone teach me how to use graphics.h in codeblocks?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Jefree Sujit
  • 1,546
  • 5
  • 22
  • 38
  • 6
    It was a header that was available in old Borland compilers. Used strictly for graphics in MS-Dos apps. You are at least 7 operating systems, 16 bits, 20 years and untold compiler versions removed from using it. – Hans Passant Dec 01 '13 at 15:24
  • As for "Can anyone teach me how to use graphics in codeblocks?": the answer lies not in CodeBlocks (it's an IDE, *not* a compiler!), but in your target OS. If for Windows: use Windows APIs. If for DOS: check the documentation that came with the compiler you are using. – Jongware Dec 01 '13 at 16:13
  • 2
    @Jongware I'm in quite the same position as Jefree. I'm a newbie in C and after a couple of basic programs I found [a code](http://www.programmingcampus.com/2013/01/circle.html) that draws a circle. Cool! And it's dead simple - literally 5 lines long. That was the power of oold graphics.h - the simplicity. Now people suggested me to learn OpenGL. :-( – Jeyekomon Feb 21 '14 at 19:00
  • @Jeyekomon You sir, just voiced my exact thoughts. Thank you. – Quirk Aug 11 '15 at 18:00

6 Answers6

12
  1. First download WinBGIm from http://winbgim.codecutter.org/ Extract it.
  2. Copy graphics.h and winbgim.h files in include folder of your compiler directory
  3. Copy libbgi.a to lib folder of your compiler directory
  4. In code::blocks open Settings >> Compiler and debugger >>linker settings click Add button in link libraries part and browse and select libbgi.a file
  5. In right part (i.e. other linker options) paste commands -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
  6. Click OK

For detail information follow this link.

frogatto
  • 28,539
  • 11
  • 83
  • 129
Dinesh Subedi
  • 2,603
  • 1
  • 26
  • 36
  • 2
    thanks,i did as u said. but when i run my program, a window appears saying that the program has stopped working. what should i do? can u help me? – Jefree Sujit Dec 07 '13 at 15:26
  • 2
    I can't figure out the actual problem. Have you use getch() function? It doesn't work if you setup winbgim. – Dinesh Subedi Dec 08 '13 at 14:32
  • the library got an issue with the re declaration of an int right @ line 302 ....please do the needful... – Shreyan Mehta Jan 20 '17 at 05:03
  • It worked with the @Aditya Ankur's answer ```int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,``` Aditya Ankur's answer is below. –  Jun 04 '18 at 13:22
  • 1
    ***a window appears saying that the program has stopped working. what should i do? can u help me?*** I expect this means your compiler is too new for the 2005 binaries that are part of the download. This repository has a newer port to attempt to fix this: [https://github.com/Duthomhas/WinBGIm-fixed-sort-of-](https://github.com/Duthomhas/WinBGIm-fixed-sort-of-) – drescherjm Sep 15 '20 at 17:16
  • Another possibility is [SDL-BGI, a port of the BGI library that runs atop SDL](http://libxbgi.sourceforge.net/) – user4581301 Oct 30 '20 at 16:48
3

You don't only need the header file, you need the library that goes with it. Anyway, the include folder is not automatically loaded, you must configure your project to do so. Right-click on it : Build options > Search directories > Add. Choose your include folder, keep the path relative.

Edit For further assistance, please give details about the library you're trying to load (which provides a graphics.h file.)

John WH Smith
  • 2,743
  • 1
  • 21
  • 31
3

If you want to use Codeblocks and Graphics.h,you can use Codeblocks-EP(I used it when I was learning C in college) then you can try

Codeblocks-EP http://codeblocks.codecutter.org/

In Codeblocks-EP , [File]->[New]->[Project]->[WinBGIm Project]

Codeblocks-EP WinBGIm Project Graphics.h

It has templates for WinBGIm projects installed and all the necessary libraries pre-installed.

OR try this https://stackoverflow.com/a/20321173/5227589

Mukesh M
  • 2,242
  • 6
  • 28
  • 41
2

AFAIK, in the epic DOS era there is a header file named graphics.h shipped with Borland Turbo C++ suite. If it is true, then you are out of luck because we're now in Windows era.

nim
  • 384
  • 2
  • 14
2
  • Open the file graphics.h using either of Sublime Text Editor or Notepad++,from the include folder where you have installed Codeblocks.
  • Goto line no 302
  • Delete the line and paste int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX, in that line.
  • Save the file and start Coding.
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
2

It is a tradition to use Turbo C for graphic in C/C++. But it’s also a pain in the neck. We are using Code::Blocks IDE, which will ease out our work.

Steps to run graphics code in CodeBlocks:

  1. Install Code::Blocks
  2. Download the required header files
  3. Include graphics.h and winbgim.h
  4. Include libbgi.a
  5. Add Link Libraries in Linker Setting
  6. include graphics.h and Save code in cpp extension

To test the setting copy paste run following code:

#include <graphics.h>
int main( )
{
    initwindow(400, 300, "First Sample");
    circle(100, 50, 40);
    while (!kbhit( ))
    {
        delay(200);
    }
    return 0;
}

Here is a complete setup instruction for Code::Blocks

How to include graphics.h in CodeBlocks?

alkesh Miyani
  • 133
  • 1
  • 5