Monday, September 19, 2011

How to setup winBGIm library in codeblock

BGI (Borland Graphics Interface) is ancient as is turbo c++ IDE but nevertheless they are still used in our schools and colleges. Plus they do not work in windows environment (support for native DOS has been completely removed in windows vista/7).If you try to compile the source code with including “graphics.h” in code::blocks IDE then is show error because “graphics.h” only run in turbo.For code::blocks you have to setup winBGIm library.
There are many alternative to winBGIm ,But BGI is good for beginners because you don’t have to worry about whole new fancy functions,classes etc.
WinBGIm is BGI graphics library ported to computer by by Michael Main at the University of Colorado.
Before i start, i must tell you that this guide is written in accordance with code::blocks IDE.

Lets get started:
  1. First download WinBGIm from http://winbgim.codecutter.org/
  2. Extract it.
  3. Copy graphics.h and winbgim.h files in include folder of your compiler directory
  4. Copy libbgi.a to lib folder of your compiler directory
  5. In code::blocks open Settings >> Compiler and debugger >>linker settings
  6. click Add button  in link libraries part and browse and select libbgi.a file
  7. In right part (ie. other linker options) paste commands
  8. -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
  9. click Ok
Finally, you can now compile source code using “graphics.h”.You can try this by creating "anlog clock" and you can copy source code from my post  here . The WinBGIm library gives you an alternative as I hope, you found this guide helpful and incase you need my help just leave a message.

59 comments:

  1. Thanks for this Howto. WinBGIm is still the easiest 2D graphics library available.

    The alternative to installing it yourself is to use the current version of Code::Blocks EDU-Portable (Codeblocks-EP) from http://codeblocks.codecutter.net
    It has templates for WinBGIm projects installed and all the necessary libraries pre-installed.
    in Codeblocks-EP just start [File]->[New]->[Project]->[WinBGIm Project]. You'll be able to choose a graphics-only project type, or one with an extra console for standard input/output if you wish.

    ReplyDelete
    Replies
    1. I get an issue with my code blocks: after i compile the code, i get the window but i doesnt draw normally. i mean, i draws the first line and then, if i resize the window with the mouse i can see the other lines. but with no delay. and i can not figure out what to do :( please help!

      Delete
    2. Thanks y again. reply here so i will get notified! :D

      Delete
    3. Cant get the output.... its opening the graphics.h header file

      Delete
  2. Thanks for information.Now I will install current version of code::blocks

    ReplyDelete
  3. thank you very much.................

    ReplyDelete
  4. Hi did this but got an error in building: "fatal error: sstream: No such file or directory||=== Build finished: 1 errors, 0 warnings ===|, will be looking this up on the internet...

    ReplyDelete
    Replies
    1. Me again, now not so anonymous. sstream is there! My Hello world file has the extension c and not cpp. Does this mean that I can only use graphics.h with c++ files?

      Delete
    2. Well its not the "Hello world" example of course but this one:

      #include

      int main()
      {
      int gd = DETECT, gm;
      int x = 320, y = 240, radius;

      initgraph(&gd, &gm, "C:\\TC\\BGI");

      for ( radius = 25; radius <= 125 ; radius = radius + 20)
      circle(x, y, radius);

      getch();
      closegraph();
      return 0;
      }

      Delete
    3. By the way, I don't know why but above the include does not mention graphics.h, and I'm using the mingw32-gcc.exe c compiler

      Delete
  5. I did this but it opened the graphics.h with error in it

    ReplyDelete
    Replies
    1. you may done some mistake in using graphics.h function so this error. compile this program http://codeincodeblock.blogspot.com/2011/09/analog-clock-source-code-using-c-and.html if error occurs then problem in your setup otherwise its works.

      Delete
  6. in build messengers here a problem, like "sstream: no such file or directory" , how to slove it.

    ReplyDelete
    Replies
    1. i too getting this error "sstream: no such file or directory". please help. how to solve it.thanks in advance.

      Delete
    2. Write using a cpp file.

      Delete
  7. paste the .h file in this path : C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1\include\c++

    but me there is an error at line 302 of graphics.h . it says redefinition of int right and when i look in the code there are two int right and no int top at the spécified line is it an error of the new version ?

    ReplyDelete
    Replies
    1. i have facing the same problem at line 302. if u could manage this problem let me know .

      Delete
    2. I tried the following steps after being suggested from
      http://stackoverflow.com/questions/20313534/how-to-use-graphics-h-in-codeblocks


      1. Open the file graphics.h using either of Sublime Text Editor or Notepad++,from the include folder where you have installed Codeblocks.
      2. Goto line no 302
      3. Delete the line and paste
      int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,
      in that line.
      4. Save the file and start Coding.

      Delete
  8. To learn c graphics programming visit programmingcampus.

    ReplyDelete
  9. i'm also getting the error "sstream: no such file or directory"

    ReplyDelete
    Replies
    1. graphics.h use in source.cpp

      Delete
  10. Hi Everybody,
    i've a problem with winbgim but i don't understand where i mistake.
    i've installed following the procedure but with any kind of example code the run stop at initgraph(&gdriver, &gmode, ""). i think probably it can't reveal something. i tried also using initwindow(300,200) and always the .exe stop at this line...someone has idea?
    thanks!
    Paolo

    ReplyDelete
    Replies
    1. I have no Idea what happen to you. You can download codeblocks-EP version from this link http://codeblocks.codecutter.org/ .In this version template for winBgim graphics is already installed so you don't have to setup winBgim. Just create the winBgim project and run the code.

      Delete
  11. Thank you Dinesh, but i don't like that versione of CB...i'd prefer to setup the latest version or directly change graphics library (as OpenGL)
    Thanks a lot
    Paolo

    ReplyDelete
  12. At me (on Code::Blocks) stop work when i want to int in graph mode

    ReplyDelete
  13. Is possible to work with graphics in c with ARM GNU GCC compiler..?? by using code blocks?? and using Winbgim library??

    ReplyDelete
  14. Thank You Very Much

    ReplyDelete
  15. i am getting some errors refering to missing references , such as 'settextalign@8'. Here is the source code that I am wanting to work:
    #include
    #include "graphics.h"
    #include "koolplot.h"

    using namespace std;

    int main()
    {
    initwindow(800,600);
    int k;
    float QT,Q1, Q2, Q3,Q4,Q5,Te,v1[200],v2[200],v3[200];
    cout<<"Q1=";cin>>Q1;
    cout<<"Q2=";cin>>Q2;
    cout<<"Q3=";cin>>Q3;
    cout<<"Q4=";cin>>Q4;
    cout<<"Q5=";cin>>Q5;
    cout<<"Te=";cin>>Te;

    for(k=1;k<=200;k++)
    {
    if(k==1)
    {v1[k]=1;
    v2[k]=0.5;
    v3[k]=1;
    }
    {if((v1[k]<=0.1) && (Q1=9.9)&&(Q1>Q2))
    Q1=Q2;
    if ((v1[k]<=0.1) && (Q3=9.9) && (Q3>Q4))
    Q3=Q4;

    QT=Q2+Q4;
    if ((v1[k]<=0.1) && (QT=9.9) && (QT>Q5))
    QT=Q5;
    }v1[k+1]=v1[k]+Te*(Q1-Q2);
    v2[k+1]=v2[k]+Te*(Q3-Q4);
    v3[k+1]=v3[k]+Te*(Q2+Q4-Q5);}
    for(k=1;k<=200;k++)
    {cout<<v1[k]<<" "<<v2[k]<<" "<<v3[k];
    plotdata x(0,int(v1));
    plotdata y(0,int(v2));
    plotdata z(0,int(v3));
    plot(x, y);
    plot(x,z);
    plot(y,z);}}

    ReplyDelete
    Replies
    1. this line: for(k=1;k<=200;k++) will access beyond the end of the v1, v2, v3 arrays. suggest: for(k=1;k<200;k++) In C, an array offset is in the range 0,,,(numberOfArrayEntries -1) so for a v1[200] the valid offsets are 0...199.

      Delete
    2. regarding this kind of line v1[k+1]=v1[k]+Te*(Q1-Q2);, v1[k+1] accesses the v1[] array beyond the end of the array, even if my prior comment is implemented. Accessing beyond the end of the upper bound of an array is undefined behaviour and can lead to a seg fault event.

      Delete
  16. hey, i got everything up 'n' runnin' but it shows me an error in the graphics.h - it says:redefinition of int right
    the other is:int right previously declared here

    ReplyDelete
  17. it opens graphic.h file when i built it.

    ReplyDelete
    Replies
    1. There must be some error in a function you are using.

      Post error properly.

      Delete
  18. for who has issues with graphic.h
    i.e.

    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\graphics.h|302|error: redefinition of 'int right'|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\graphics.h|302|error: 'int right' previously declared here|
    ||=== Build finished: 2 errors, 0 warnings ===|

    instead of repeat all here follow up http://forums.codeblocks.org/index.php/topic,15147.msg132950.html#msg132950

    PS: C:B EP has that file debugged i guess from his mantainer

    Ceers,
    Harry

    ReplyDelete
  19. getting error…:(
    please help…
    error: sstream: No such file or directory

    ReplyDelete
    Replies
    1. Change your file extension to .cpp from .c

      OR Always select C++ while making new project in Code Blocks.

      I hope that helps.

      Delete
  20. What should be the third parameter in initgraph() ? As it cannot be C:\\TC\\BGI for Codeblocks

    ReplyDelete
    Replies
    1. Doesn't matter. Just put C:\\TC\\BGI in there and it will work.

      If it cannot find drivers in this directory, it looks up in current directory by default.

      Delete
    2. It still doesn't work for me... what should I install in order to get C:\\TC\\BGI?

      Delete
    3. same is with me please tell a solution

      Delete
  21. My problem is different, it says: " cannot find -lole32? " and it comes from ld.exe

    File Line Message
    ld.exe cannot find -lol32?

    ReplyDelete
    Replies
    1. same problem of -lole32 :(

      Delete
  22. i try but is show agin error
    ----------------------------------------------------
    i try basic program using graphics.h
    ---------------------------------------------------------
    =====================================
    #include
    void main()
    {

    }

    =======================================================

    c:\program files (x86)\codeblocks\mingw\include\graphics.h|30|fatal error: sstream: No such file or directory|
    ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|

    ReplyDelete
    Replies
    1. Change your file extension to .cpp if it is .c

      Change it to .cpp

      It worked for me.

      Delete
  23. my problem is when I click on (Build and Run) Button my program generate some errors like as..
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\graphics.h|302|error: redefinition of 'int right'|

    but when I click on (Run) Button my program executed correctly...

    ReplyDelete
  24. help me:
    ld.exe cannot find -lbgi|
    === Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    ReplyDelete
  25. See the following Link:

    http://emahbub.blogspot.com/2015/03/how-to-run-c-graphics-program-in.html

    ReplyDelete
    Replies
    1. Before saying anything, i would like to apologize for not appreciating the hard work and effort that you put into teaching us lessons.
      Thank you for spending your valuable time teaching us even when we were not keen to learn.

      Delete
  26. to solve graphics.h error. U can see there is two times declared 'right'. One of those need to be changed into 'top' and that is it.

    ReplyDelete
  27. i have some error in DEV c++ . when i wanna complie a program i see these error
    $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
    above error for MAKEFile.win in the 25 line and section compiler write thise massage
    [Error] id returned 1 exit statuse
    plz help me

    ReplyDelete
  28. Is there a way to use initwindow() to open the window in the *center of the screen*? It is possible to specify the coords of the top-left corner of the window but how to read the size of the whole screen to be able to calculate the parameters?

    ReplyDelete
    Replies
    1. use the functions: getmaxx() and getmaxy() and divide the results by 2 to get the center of the screen.

      Delete
  29. Im using the new code blocks when i try to run my code in the graphic project i get this error : linking stage skipped (build target has no object file to link )
    Nothing to be done ( all items are up to date )

    ReplyDelete
  30. Im using the new code blocks when i try to run my code in the graphic project i get this error : linking stage skipped (build target has no object file to link )
    Nothing to be done ( all items are up to date )

    ReplyDelete
  31. All other programmes are running fine on mu codeblock 13.12 but when I runs a simple graphic programme compiler crashes giving pop up window that says "abc.exe has stopped working window is checking solution".
    It also giving a warning message on compilation

    warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]|

    How to resolve it.
    Advance Thanks

    ReplyDelete
    Replies
    1. I have the same problem - also using c::b 13.12.
      Did you find a solution?

      Delete
  32. I do have the same problem. Could anybody help us please?!

    ReplyDelete