Monday, September 19, 2011

How to Setup WinBGIm library in Codeblocks IDE

BGI (Borland Graphics Interface) is a very old graphics library available in Borland C++ and Turbo C++ IDE. Borland c++ and Turbo C++ both were discontinued. BGI graphics library was very popular, and still, students in our schools and colleges want to use it in their projects. Codeblocks IDE with MinGW compiler is widely used as C/C++ especially to develop cross-platform open-source software but BGI is not available in Codeblocks. Thanks to Michael Main, who created WinBGIm at the University of Colorado to use the BGI graphics library in windows with other compilers. There are many alternatives to winBGIm, But BGI is good for beginners because you don’t have to worry about whole new fancy functions, classes, etc.

However, the latest version of WinBGIm was released in Nov 2005 which is very old and no longer supported in a newer version of the MinGW compiler. We have to compile source code to make it run in the latest Codeblocks. In this article, I will provide instructions to compile source code and configure the library in Codeblocks IDE. I have also created a video about it. You can watch below:


If you don’t want to compile source code and fix errors by yourself then copy project files from my github https://github.com/smokindinesh/WinBGIm and go to configure library section.

Lets get started: Create Static Library Project
  1. First download WinBGIm Source Code from http://winbgim.codecutter.org/
  2. Extract it. (Note: Make sure you have download source code)
  3. Open Codeblocks and create a new project. (File -> New -> Project)
  4. From the Project template window select "Static Library" and fillup all necessary information to finish the project creation wizard.
  5. Go to project properties and select "Build targets" option and rename output filename with libbgi.a for both debug and release targets.
  6. Copy all source files that you have extracted in the earlier step and paste them to the project directory.
  7. Remove "main.c" file from the project.
  8. Add source files in the project (Select project in Workspace -> Right Click -> Add Files recursively). Select both Build and Release options in the Target window which pops-up after adding files.
  9. Now, a project is ready to build but there are errors in the source code. Please follow the instructions provided below to fix errors.

Instruction to fix errors:
  • Source file "bgiout.cxx" 
    • Line 1: Replace '#include <stringstream>' with '#include <sstream>'
    • Line 2: Remove ': virtual std::basic_ostringstream'
  • Source file "winbgim.h"
    • Line 302: Replace "right=0" with "top=0"
  • Source file "graphics.h"
    • Line 302: Replace "right=0" with "top=0"
  • Source file "misc.cxx"
    • Line 595 to 599: Remove "~". e.g Replace ~0xE0 with 0xE0.
  • Source file "winthread.cxx"
    • Line 103: Replace "SetWindowLong( hWindow, GWL_USERDATA, (LONG)pWndData )" with "SetWindowLongPtr( hWindow, GWLP_USERDATA, (LONG_PTR)pWndData )"
  • Source file "drawing.cxx"
    • Line 66: Replace "return (WindowData*)GetWindowLong( hWnd, GWL_USERDATA )" with "return (WindowData*)GetWindowLongPtr( hWnd, GWLP_USERDATA );"
  • Note: In above steps 5 and 6, if GWLP_USERDATA didnot work then try with GWL_USERDATA
  • Finally, compile project in Release target. I hope you will not encounter any other errors.

Configure winbgim library in Codeblocks.
  1. Create a folder in any directory and give the name "WinBGIm" and create include and lib folder inside it. (eg. c:\WinBGIm\include and c:\WinBGIm\lib).
  2. From the project created above, copy "graphics.h" and "winbgim.h" header files and paste it inside include directory, and "libbgi.a" file from bin/release project folder and paste it inside lib directory that you have created in step 2. (eg. c:\WinBGIm\include and c:\WinBGIm\lib). 
  3. Open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
  4. From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to  add main.cpp file.
  5. Go to menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under "Compiler" tab provide the path of WinBGIm include folder (e.g. c:\WinBGIm\include) and under "Linker" tab provide the path of WinBGIm lib folder (e.g. c:\WinBGIm\lib).
  6. Select "Linker Setting" tab and add "libbgi.a" file e.g path c:\WinBGIm\lib\libbgi.a. In "Other linker options" text box add linkers -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
  7. Now click ok.

Finally, you can now compile source code using "graphics.h". To test configuration please copy source code from the sample winBGIm graphics program from here. I hope, you found this guide helpful, and in case you need my help just leave a message.

56 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
    4. My program is terminated unexpectedly after giving a warning saying that :
      Deprecated conversion from string constant to 'char*' [-Wwrite-strings]
      What can i do?

      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. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. My problem is different, it says: " cannot find -lole32? " and it comes from ld.exe

    File Line Message
    ld.exe cannot find -lol32?

    ReplyDelete
  20. 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
  21. 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
  22. help me:
    ld.exe cannot find -lbgi|
    === Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    ReplyDelete
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. I do have the same problem. Could anybody help us please?!

    ReplyDelete