I am trying to run a really simple GUI application in C++ with Eclipse (Neon): the program starts, shows a red display and closes itself after 10 seconds.
To achieve this I am running the Allegro 5.0.10 game engine, its source code installs some libs inside /usr/local/include/allegro5
. My program looks like this:
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(255,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
Creating a new project from scratch with the following options...
...and building it with these ones...
...when selecting 'Build All', an error message appears in the console:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: pang
Invoking: GCC C++ Linker
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
./main.o: In function `main':
/home/xvlaze/workspace/pang/Debug/../main.cpp:14: undefined reference to `al_install_system'
/home/xvlaze/workspace/pang/Debug/../main.cpp:19: undefined reference to `al_create_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_map_rgb'
/home/xvlaze/workspace/pang/Debug/../main.cpp:25: undefined reference to `al_clear_to_color'
/home/xvlaze/workspace/pang/Debug/../main.cpp:27: undefined reference to `al_flip_display'
/home/xvlaze/workspace/pang/Debug/../main.cpp:29: undefined reference to `al_rest'
/home/xvlaze/workspace/pang/Debug/../main.cpp:31: undefined reference to `al_destroy_display'
collect2: error: ld returned 1 exit status
make: *** [pang] Error 1
EXTRA: I have already reproduced this answer, but it's still not working.
The problem you are now having is that the special flags you have added are coming before the object that depends on them.
What you should do is change the GCC C Linker -> Command line pattern to have ${FLAGS}
after the ${INPUTS}
.
Doing that will change the compile line from:
g++ `pkg-config --libs allegro-5 allegro_image-5` -o "pang" ./main.o
to:
g++ -o "pang" ./main.o `pkg-config --libs allegro-5 allegro_image-5`
See https://stackoverflow.com/a/409470/2796832 for some more info on link order and why it matters.