Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » What is the error in following little program

This thread is locked; no one can reply to it. rss feed Print
What is the error in following little program
toussaint1963
Member #16,622
January 2017

I started programming in C.
I started programming with allegro.
Following Programm:
#include <allegro.h>

int main(){
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
ALLEGRO_BITMAP *screen;

putpixel(screen, 10, 30, makecol(255, 255, 255));//macht einen Punkt
vline(screen, 30, 80, 300, makecol(0, 255, 0));//senkrechte Linie
hline(screen, 40, 20, 400, makecol(0, 0, 255));//waagrechte Linie
line(screen, 0, 0, 640, 480, makecol(255, 0, 0)); //Linie
triangle(screen, 0, 400, 0, 450, 100, 450, makecol(255, 0, 255));//gefülltes Dreieck
rect(screen, 100, 200, 200, 250, makecol(255, 255, 0));//leeres Viereck
rectfill(screen, 125, 210, 175, 240, makecol(255, 255, 0));//gefülltes Viereck
circle(screen, 500, 400, 50, makecol(255, 0, 0)); //leerer Kreis
circlefill(screen, 500, 400, 25, makecol(255, 255, 255));//gefüllter Kreis

readkey();
return 0;
}

I got the error from Gcc compiler:
johannes@linux-myvz:~/programme-c> gcc zeichnen.c -o zeichnen -I /usr/local/include/allegro5
zeichnen.c: In function ‘main’:
zeichnen.c:7:17: error: ‘GFX_AUTODETECT’ undeclared (first use in this function)
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
^
zeichnen.c:7:17: note: each undeclared identifier is reported only once for each function it appears in

What is wrong?
Sincerely Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

Your code is written using Allegro 4. You just installed Allegro 5. They are not compatible. I suggest using the tutorials for Allegro 5 on the wiki to get yourself started using Allegro 5.

See these for more details :

https://wiki.allegro.cc/index.php?title=Getting_Started#Official_Getting_Started_Guide

https://wiki.allegro.cc/index.php?title=Getting_Started#Tutorials

EDIT
Even if you were using Allegro 4, your program still has errors. Don't declare 'screen' - allegro 4 defines it for you, and if you declare one of your own, it will shadow the global screen variable. It's also undefined because you didn't initialize it, so it would crash if you tried to draw to it.

toussaint1963
Member #16,622
January 2017

The term:
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
is allegro5, why the error message?

Edgar Reynaldo
Member #8,592
May 2007
avatar

toussaint1963
Member #16,622
January 2017

I changed the program in allegro5:
#include <allegro.h>
#include <allegro5.h>
#include <allegro_primitives.h>
#include <allegro5/allegro_color.h>
#include <base.h>

int main(){

int w=640;
int h=480;
const char *white="white";
const char *red="red";
al_init();
al_install_keyboard();
set_color_depth(16);
al_create_display(w,h);
al_color_name(*white);
al_color_name(*red);



al_draw_line(0, 0, 640, 480, white,1); //Linie
al_draw_triangle( 0, 400, 0, 450, 100, 450, white,1);//gefülltes Dreieck
al_draw_rectangle(100, 200, 200, 250,white,1);//leeres Viereck
al_draw_filled_rectangle( 125, 210, 175, 240, red);//gefülltes Viereck
al_draw_circle( 500, 400, 50,white,1); //leerer Kreis
al_draw_filled_circle(500, 400, 25,white);//gefüllter Kreis

readkey();
return 0;
}
I got error messages with gcc. What ist wrong?
Sincerely Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

On the forums, please use <code>code goes here...</code> tags to post code. It makes it much easier to read and look at.

The highlighted lines (***) are incorrect :

toussaint1963 said:

 // Allegro 4 header
#include <allegro.h> ***

// incorrect allegro 5 include
#include <allegro5.h> ***

// incorrect allegro 5 include
#include <allegro_primitives.h> ***

// correct
#include <allegro5/allegro_color.h>

// What is this for?
#include <base.h> ***

Since all allegro 5 headers live in the allegro5 include directory, you need to preface every allegro 5 header include with allegro5/ like so :

#include "allegro5/allegro.h"
#include "allegro5/allegro_primitives.h"
#include "allegro5/allegro_color.h"

Note that I used "" and not <>. That means they are user headers, and not system headers. If you use <> it will only search system header directories and not user specified ones.

Also, you're using al_color_name incorrectly. If you look at the documentation, you'll see that al_color_name returns an ALLEGRO_COLOR object. You pass that to your drawing functions, like so :

al_draw_line(0 , 0 , 640 , 480 , al_color_name("white") , 1); //Linie

See al_color_name_to_rgb for details on which color names you can specify.

Quote:

set_color_depth(int) and readkey() are allegro 4 functions. You generally don't need to change the color depth. Just let allegro 5 set it for you. It will usually use 32 or 24 bit color depending on the desktop color depth.

The equivalent code for readkey() in allegro 5 would be :

ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue();
al_register_event_source(queue , al_get_keyboard_event_source());

do {
   ALLEGRO_EVENT ev;
   al_wait_for_event(queue , &ev);
} while (ev.type != ALLEGRO_EVENT_KEY_DOWN);

Try and stop mixing allegro 4 and 5 functions together. They are not compatible. The manual for Allegro 5 is here :

Allegro 5 online manual

toussaint1963
Member #16,622
January 2017

I got the error:
zeichnen.c: In function ‘main’:
zeichnen.c:31:10: error: ‘ev’ undeclared (first use in this function)
} while (ev.type != ALLEGRO_EVENT_KEY_DOWN);
^
zeichnen.c:31:10: note: each undeclared identifier is reported only once for each function it appears in

I don't know, what the programming means.
Sincerely Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

toussaint1963
Member #16,622
January 2017

I got following error messages:
/tmp/ccjg5tJd.o: In function `main':
zeichnen.c:(.text+0x22): undefined reference to `al_install_system'
zeichnen.c:(.text+0x27): undefined reference to `al_install_keyboard'
zeichnen.c:(.text+0x36): undefined reference to `al_create_display'
zeichnen.c:(.text+0x40): undefined reference to `al_color_name'
zeichnen.c:(.text+0x93): undefined reference to `al_draw_line'
zeichnen.c:(.text+0x9d): undefined reference to `al_color_name'
zeichnen.c:(.text+0xfe): undefined reference to `al_draw_triangle'
zeichnen.c:(.text+0x108): undefined reference to `al_color_name'
zeichnen.c:(.text+0x165): undefined reference to `al_draw_rectangle'
zeichnen.c:(.text+0x16f): undefined reference to `al_color_name'
zeichnen.c:(.text+0x1c4): undefined reference to `al_draw_filled_rectangle'
zeichnen.c:(.text+0x1ce): undefined reference to `al_color_name'
zeichnen.c:(.text+0x223): undefined reference to `al_draw_circle'
zeichnen.c:(.text+0x22d): undefined reference to `al_color_name'
zeichnen.c:(.text+0x27a): undefined reference to `al_draw_filled_circle'
zeichnen.c:(.text+0x27f): undefined reference to `al_create_event_queue'
zeichnen.c:(.text+0x288): undefined reference to `al_get_keyboard_event_source'
zeichnen.c:(.text+0x29a): undefined reference to `al_register_event_source'
zeichnen.c:(.text+0x2ad): undefined reference to `al_wait_for_event'
collect2: error: ld returned 1 exit status

What is wrong?
Sincerely Johannes fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

toussaint1963
Member #16,622
January 2017

Haw have I to use pkg-config.
Allegro.h is in the directory /usr/local/include/allegro5.
Sincerely Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

You have to set the include search directory with -I and then specify your source files and after that you have to link using pkg-config. Note the backticks around the pkg-config call.

gcc -Wall -g -o main.exe -I usr/local/include main.c `pkg-config --libs allegro-5.0 allegro-primitives-5.0`

Run this command to see the different libraries for allegro :
pkg-config --list-all | grep allegro

toussaint1963
Member #16,622
January 2017

I got following error message:
johannes@linux-myvz:~/programme-c> gcc -Wall -g -o zeichnen -I usr/local/include zeichnen.c `pkg-config --libs allegro-5.0 allegro-primitives-5.0` -I /usr/local/include/allegro5
Package allegro-5.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `allegro-5.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'allegro-5.0' found
Package allegro-primitives-5.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `allegro-primitives-5.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'allegro-primitives-5.0' found
and then the former listed errors
MfG Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

Perhaps you should add the directory containing `allegro-5.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'allegro-5.0' found

This indicates pkg-config doesn't know where to find the allegro-5.0.pc file. CMake may not have detected pkg-config properly when you installed allegro 5. Was pkg-config installed before you compiled Allegro?

Did you try this?

pkg-config --list-all | grep allegro

It will tell you all the .pc files that contain allegro in their name.

You may need to re-run cmake after deleting CMakeCache.txt to properly detect pkg-config.

Otherwise, you need to find the directory containing allegro-5.0.pc and then add that directory to the PKG_CONFIG_PATH environment variable. Someone else will have to tell you how to do that.

toussaint1963
Member #16,622
January 2017

How can I set allegro-5.pc to the PKG_CONFIG_PATH? allegro-5.pc ist in the /bin/allegro-5.2.2.0/build/lib/pkgconfig directory.

Edgar Reynaldo
Member #8,592
May 2007
avatar

export OLD_PKG_CONFIG_PATH=$PKG_CONFIG_PATH

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/bin/allegro-5.2.2.0/build/lib/pkgconfig

However, you may have installed allegro incorrectly, as that should have already been done I think...

bamccaig
Member #7,536
July 2006
avatar

That's a rather strange path... This looks more like the source tree than the installation path to me... Perhaps he didn't install... The usual incantation is something like:

git clone https://domain/path/to/repo ~/src/allegro5
cd ~/src/allegro5
mkdir build
cd build
cmake ..
make
sudo make install

toussaint1963
Member #16,622
January 2017

I said it false
the directory was /home/johannes/bin/allegro-5.2.2.0/build/lib/pkgconfig
Sorry for this mistake .
there was only a file allegro_primitives-5.pc not allegro-primitives.pc
Sincerely Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

Is that the directory you built allegro in? You need to 'make install' allegro and then pkg-config should be able to find its entries for allegro. To do that, you have to have pkg-config installed before you run cmake. You may need to rebuild allegro and rerun cmake after deleting CMakeCache.txt.

toussaint1963
Member #16,622
January 2017

I habe installed pkg-config zhen made cmake .., make and make install and it runs.
There was shown a new display, but there was no line, triangle, rectangle and circle drawn. what is wrong?
Here the code of the program:

#SelectExpand
1#include "allegro5/allegro.h" 2#include "allegro5/allegro_primitives.h" 3#include "allegro5/allegro_color.h" 4 5 6int main(){ 7 8 int w=640; 9 int h=480; 10 al_init(); 11 al_install_keyboard(); 12 al_create_display(w,h); 13 14 15 16 17 18 al_draw_line(0, 0, 640, 480, al_color_name("white"),2); //Linie 19 al_draw_triangle( 0, 400, 0, 450, 100, 450, al_color_name("white"),2);//gefülltes Dreieck 20 al_draw_rectangle(100, 200, 200, 250,al_color_name("white"),2);//leeres Viereck 21 al_draw_filled_rectangle( 125, 210, 175, 240, al_color_name("red"));//gefülltes Viereck 22 al_draw_circle( 500, 400, 50,al_color_name("white"),2); //leerer Kreis 23 al_draw_filled_circle(500, 400, 25,al_color_name("white"));//gefüllter Kreis 24 25 ALLEGRO_EVENT_QUEUE* queue = al_create_event_queue(); 26al_register_event_source(queue , al_get_keyboard_event_source()); 27 28ALLEGRO_EVENT ev; 29do { 30 al_wait_for_event(queue , &ev); 31} while (ev.type != ALLEGRO_EVENT_KEY_DOWN); 32 33 return 0; 34}

Sincerely Johannes Fangmeyer

Edgar Reynaldo
Member #8,592
May 2007
avatar

toussaint1963
Member #16,622
January 2017

Thank You. Now it runs!
Thank You very much
Sincerely Johannes Fangmeyer

bamccaig
Member #7,536
July 2006
avatar

Go to: