-2

I have this program in C language: http://rajababuman.blogspot.com/p/graphics-in-turbo-c.html.

It works fine if I use DOSBOX on my Win7 machine and using TurboC++ and shows me what it's doing. But, how can I run the following graphics program on a Linux machine (where we don't have DOSBOX or turboC++)? PS: DISPLAY environment variable is already set to my local machine's IP address to SHOW me GUI/Graphics on Linux box i.e. if I run "xclock", the clock shows up on my machine successfully.

I know TURBO C is a Windows tool and uses Windows API.

I don't have to use graphics.h header file, if I can get a simple C program on a Linux machine, which when I compile, gives me the same output (as this program is giving me on a Windows machine) on a Linux machine (without me intsalling/using DOSBOX or TurboC).

/////////////////////////////////////////////////////////////////////////////////////////
//Diagram of a car
///////////////////////////////////////////////////////////////////////////////////////

#include<stdio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
rectangle(100,200,200,250);
rectangle(220,200,320,250);
rectangle(90,190,330,290);

circle(150,290,30);
circle(270,290,30);

getch(); 
}

When I compile this program on a Linux machine, I get the following errors:

[koba@server1 lory]$ gcc g.c

g.c:2:21: error: graphics.h: No such file or directory
g.c: In function âmainâ:
g.c:5: error: âDETECTâ undeclared (first use in this function)
g.c:5: error: (Each undeclared identifier is reported only once
g.c:5: error: for each function it appears in.)
g.c:4: warning: return type of âmainâ is not âintâ

[koba@server1 lory]$

[koba@server1 lory]$ cc g.c

g.c:2:21: error: graphics.h: No such file or directory
g.c: In function âmainâ:
g.c:5: error: âDETECTâ undeclared (first use in this function)
g.c:5: error: (Each undeclared identifier is reported only once
g.c:5: error: for each function it appears in.)
g.c:4: warning: return type of âmainâ is not âintâ

[koba@server1 lory]$

genpfault
  • 51,148
  • 11
  • 85
  • 139
AKS
  • 16,482
  • 43
  • 166
  • 258
  • The compiler seems to have difficulties to find "graphics.h" – kiwixz Dec 05 '14 at 23:15
  • Well where's graphics.h? – Trevor Hickey Dec 05 '14 at 23:16
  • graphics.h is a nonstandard header. See this question http://stackoverflow.com/questions/7860569/how-i-can-get-and-use-the-header-file-graphics-h-in-my-c-program – David Woo Dec 05 '14 at 23:21
  • @iKiWiXz yes. there's no graphics.h on Linux machine. As David mentioned, it's nonstandard and doesn't ship as it's OLD. But, can you share one simple Graphics C program that I can run on a Linux box (without installing any cruft). – AKS Dec 05 '14 at 23:23
  • http://cairographics.org/ or https://www.libsdl.org or even https://www.gnu.org/software/ncurses/ – didierc Dec 05 '14 at 23:31
  • 1
    I would recommend using a platform-independent solution such as SDL (as didierc mentioned). That way, you won't need to rewrite your code for different platforms. If you really aren't able to include any 3rd party libraries, you'll have to look into drawing things using your operating system's display libraries (e.g. [Xlib/libX11](http://tronche.com/gui/x/xlib/))... [here is an example](http://digitalfoo.net/posts/drawing-rectangles-on-the-screen-with-xlib-c-library/) of drawing a rectangle. – Julian Dec 05 '14 at 23:34
  • @AtlasC1 thanks. I'll try that. How about this link, i think this says the same (provided if I install things): http://www.slideshare.net/tusharkute/graphics-programming-in-c-under-gnu-inux-ubuntu-distribution – AKS Dec 05 '14 at 23:44

3 Answers3

1

The "graphics.h" is the header file of one of the libraries supported by Turbo C++, so it's dedicated for DOS.

If you want to use DOSBOX in Linux, please try downloading "dosbox-0.74.tar.gz" from its project website to your Linux machine, and compile and then run it.

Alan Zhiliang Feng
  • 1,774
  • 18
  • 20
  • Hi Konca, I agree. My main ? is I dont want to install DOSBOX or TurboC Linux compatible version on Linux machine. I don't even have to use "graphics.h" header file. Do you know / write a simple Linux C program, which if I run on my Linux machine runs fine showing a rectangle/circle (without requiring me to download/install any extra software/utilities)? – AKS Dec 05 '14 at 23:27
  • In other words, can i get a sample C program in Linux which generates Graphics (circle or some shape)? – AKS Dec 05 '14 at 23:28
  • instead of DOSBOX etc, I like this better if i have to install extra things: http://www.slideshare.net/tusharkute/graphics-programming-in-c-under-gnu-inux-ubuntu-distribution – AKS Dec 05 '14 at 23:36
  • It's up to your original purpose - studying graphics class in school, or develop an application which runs on DOS? If for the former one, try other environment, or if you are fond of DOS, try: Linux + (VirtualBox + DOS + TC). – Alan Zhiliang Feng Dec 05 '14 at 23:51
  • Thanks Konca, i got you. – AKS Dec 06 '14 at 00:11
0

If you want to compile a graphics program under Linux, first you have to assume you are working in some graphical environment, like X11R6 or Xorg, or Wayland, etc. Those are graphical servers, which perform all graphics-related operations on your display. You know that one of them is installed if you are using windows - they are provided by one of many window managers, like gnome, kde/plasma, xfce, fluxbox, and lots of others. Anyway, if you are using window-related system, in which you can open new windows, then I assume you will be able to write a program which is using pure graphics system by simple calls to something like X-windows api - which is provided by Xlib libraries like X11.h and others. Those libs should be installed on most systems, because in normal usage cases they are not needed. Also, you have to install gcc and other build tools, so installing 'something' is necessary. But X11 is a simple (not easy!) library, with which you can create simple (and even more complicated) progams which draws rectangles etc.

However, installing bare X11 toolset and libs seems to be little to harsh, and really, using something more high-level will suffice you better, like SDL mentioned before. You can also try to get into a complicated and reach world of gui-related tools/libs, like Gtk or Qt worlds, but they are overkill for drawing single shapes.

If you are not up to learn a lot (and X11 is quite huge and nasty/complicated sometimes, same as Gtk and Qt, which forces you to program in their ways), you can try some primitive drawing libs, like (quickly googled) gfx lib: https://www3.nd.edu/~dthain/courses/cse20211/fall2013/gfx/

I think they are more like this in the world, you can just look for some of them trying 'simple graphics library for c' in google, etc. Good luck!

Another story is when you have your Linux without graphics, and you see only text console. Then, you will have to use something which called framebuffer, and sometimes this requires a quite complicated setup, including enabling/compiling specific kernel modules. This way is probably not recommended for you, because the interference with the system is even bigger than when you already have graphics gui, and you try only use them.

Yet another story is when you are in fact on graphics system, BUT you are logged in on another system, which may, or may not have graphics installed. Because you mentioned DISPLAY variable, I suspect that you like to run your program on another host, being logged in on some graphic workstation. This is also complicated. SSH is able to tunnel X11 server requests but your program is using local graphics resources to display it's window. So for some cases I suspect that runnig it on remote which does not have graphics libs could be possible in some way. The easiest case here is when on both systems you have full graphical workstation, but sometimes there are other differences interfering with displaying the picture or main window - like other DPI or resolution, other resources, other themes loaded by window manager, etc.

piotao
  • 267
  • 3
  • 12
-3

http://www.slideshare.net/tusharkute/graphics-programming-in-c-under-gnu-inux-ubuntu-distribution - Slide 16/17/18 shows what I wanted (without installing DOSBox) after I install some goodies.

Here's the PDF if someone needs it (another link): http://s000.tinyupload.com/index.php?file_id=00628356691036251698 OR see the attached file for steps. enter image description here

It's useful if the slide share link is broken / gone. Thanks to Tushar B Kute for his online contribution.

AKS
  • 16,482
  • 43
  • 166
  • 258
  • In case this link dies, could you include the relevant details from slides 16, 17, and 18 as part of this answer? – josliber Aug 11 '15 at 17:53
  • Would it be possible to just state the relevant information instead of requiring people to read through a bunch of slides? – josliber Aug 11 '15 at 18:21