10

I am trying to compile a program which includes the graphics.h header file for C. I have added the graphics.h and winbgim.h header files in the include folder and also libbgi.a to lib folder.

Just for testing, I made a simple hello world program and included the graphics.h header file.

But on compiling I got the following error:

In file included from firstc.c:2:0: c:\mingw\bin../lib/gcc/mingw32/4.7.1/../../../../include/graphics.h:30:59: fatal error: sstream: No such file or directory compilation terminated.

I tried to search in other forums as well, where the same question had been asked, but could not get an answer.

Another question, I came across other graphic options for C and C++ like openGL and DirectX. Should I learn these instead of graphics.h?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
  • 1
    `` is operating system specific. You might consider [libsdl](http://libsdl.org/) or [gtk](http://gtk.org/) (or in C++ [Qt](http://qt-project/)...) since they all probably run on more operating systems. – Basile Starynkevitch Jan 08 '15 at 12:03
  • @BasileStarynkevitch : Thanks! As a beginner, which one should I begin with, and also which one would be more beneficial in the long run? – Ayushi Jha Jan 08 '15 at 12:18
  • 1
    It depends what you want to do with it. My suggestion actually would be to install Linux (most distributions have `libsdl`,`gtk`,`Qt` already) on your machine. Don't forget to enable all warnings and debug info in your compiler (`gcc -Wall -Wextra -g`) and learn how to use the debugger (`gdb`) – Basile Starynkevitch Jan 08 '15 at 12:20
  • 1
    The interesting thing about Linux is that it is mostly (or fully, if you are careful) made of [free software](http://en.wikipedia.org/wiki/Free_software) whose source code you can study and improve. You'll learn a lot. – Basile Starynkevitch Jan 08 '15 at 12:26
  • @BasileStarynkevitch : Thanks a lot! Yeah, as a computer science student I have got to learn all those things you mentioned. I already have Ubuntu along with Windows. I'll try my best :) – Ayushi Jha Jan 08 '15 at 12:37
  • 1
    Take the habit of studying the source code of some free software you are using (perhaps start with your shell, or `/bin/ls`) ; you'll learn a lot – Basile Starynkevitch Jan 08 '15 at 12:38
  • Does this answer your question? [How I can get and use the header file in my C++ program?](https://stackoverflow.com/questions/7860569/how-i-can-get-and-use-the-header-file-graphics-h-in-my-c-program) – tripleee Sep 14 '20 at 10:53

6 Answers6

11

graphics.h is a non-standard header. Most likely it refers to the old BGI graphics library of the Turbo C DOS compiler. It will only work on that particular compiler. And of course DOS is a completely obsolete OS nowadays.

If you are interested in 3D graphics programming, then OpenGL and/or DirectX are indeed better, modern alternatives, supported by many compilers.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 2
    Notice that DirectX is Windows specific. – Basile Starynkevitch Jan 08 '15 at 12:22
  • 2
    Thanks! So I can't use graphics.h (I have a gcc compiler). I tried searching for how to use OpenGL / DirectX, but there were two different types of solutions - either use them directly (using some ) header files or download glut/ freeglut / flgw etc. and use those header files (like ). Which one would be easier, considering I don't know the basics of any graphic programming, and wish to use them only for my C/C++ programs? – Ayushi Jha Jan 08 '15 at 12:28
  • 1
    @BasileStarynkevitch : Yes, which has now left me wondering if I should use it or OpenGL. Is it true that programs made using OpenGL are not much effective on a Windows Platform? I have both Windows and Ubuntu. – Ayushi Jha Jan 08 '15 at 12:31
  • 1
    The main issues with OpenGL on Windows is that by default Windows only comes with a software-only OpenGL 1.x API. To have hardware support or any newer version of OpenGL requires installing a 3rd-party ICD driver. That said, many driver packages from AMD/ATI, NVidia, and Intel come with these ICDs. – Chuck Walbourn Jan 09 '15 at 19:08
5

If you try to compile the source code with including “graphics.h” in code::blocks IDE you have to setup winBGIm library.

  • Download WinBGIm from http://winbgim.codecutter.org/ or use (direct link)
  • Extract it.
  • Open graphics.h, go to line 302 change int right=0 to int top=0
  • Copy graphics.h and winbgim.h files in include folder of your compiler directory.
  • Copy libbgi.a to lib folder of your compiler directory
  • In code::blocks open Settings >> Compiler and debugger >> linker settings
  • Click Add button in link libraries part, browse and select libbgi.a file
  • In right part (ie. other linker options) paste commands
    -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
  • Click Ok

You can also check this video tutorial.

Compiler options

Panos Kalatzantonakis
  • 12,525
  • 8
  • 64
  • 85
1

When you're compiling a C source code having graphics.h header file, you'll need to change the file extension to .cpp. Without doing that, you'll get “fatal error: sstream : no such file directory” error.

So, simply change the .c extension to .cpp. Here's a step-by-step procedure to compiling graphics.h source code if you're using Code::Blocks to run the code.

1

The sstream error occurs only when you are compiling using gcc not g++ , try using g++ or converting the program to c++ , as far as i know ( since i'm new to this language but i've faced this error before ) so goodluck with that

0

If top answer doesn't work, and you are getting:

  • error: narrowing conversion of 'x' from 'int' to 'short unsigned int'

  • your graphics window just doesn't appear

    following this tutorial and using the bug fixed headers/libraries (winBGIm(bug-free).rar) in the provided winBGIm source link, worked for me in Windows 10.

Mirror to winBGIm(bug-free_.rar)

compiling with: g++ example.cpp -Wall -m32 -std=c++11 -pedantic -g -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 -o example.exe

Community
  • 1
  • 1
jackw11111
  • 1,457
  • 1
  • 17
  • 34
-1

this process is abosulitly right. doing this process step by step your code is compile and execute. change the extension of your program to .c to .cpp step1. https://youtu.be/CHFyEnlMnxg through this video follow all step.