2

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...

New project options

...and building it with these ones...

C++ options.

Builder extra options.

...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.

Community
  • 1
  • 1
xvlaze
  • 837
  • 1
  • 10
  • 30
  • Can you share with us what is displayed in the build console? The shows exactly what command line is actually being used to run the compiler/etc and may help indicate where the problem is. – Jonah Graham Sep 19 '16 at 09:24
  • @JonahGraham Sure! Also updating w/ farther info :) – xvlaze Sep 19 '16 at 19:01
  • Please consider editing the question to ask just one question. The first part of your question should be closed as "a problem that can no longer be reproduced". By leaving it in there it makes the question of poorer quality. – Jonah Graham Sep 20 '16 at 21:57
  • Oops, I hope this one's better. – xvlaze Sep 21 '16 at 07:27
  • BTW the question is the whole thing (title, body and tags), not just the title of the question :-) You are asking a very useful question, but it would be hard for any future reader to use it as a resource. – Jonah Graham Sep 21 '16 at 10:50
  • Updated the whole question. – xvlaze Sep 21 '16 at 21:05

1 Answers1

0

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.

Community
  • 1
  • 1
Jonah Graham
  • 7,890
  • 23
  • 55
  • If I remove the backquotes from pkg-config, an error message appears: `make all Building target: libPong Invoking: GCC C++ Linker g++ pkg-config --libs allegro-5 allegro_image-5 -shared -o "libPong" ./pong.o g++: error: pkg-config: No such file or directory g++: error: allegro-5: No such file or directory g++: error: allegro_image-5: No such file or directory g++: error: unrecognized command line option ‘--libs’ make: *** [libPong] Error 1` – xvlaze Sep 19 '16 at 21:25
  • Not sure why you removed the backquotes. Please review your C/C++ Linker -> Libraries setting. – Jonah Graham Sep 19 '16 at 21:28
  • Re-reading my answer I think I see what you did. I tried to be more explicit about the part you had right. – Jonah Graham Sep 19 '16 at 21:31
  • I'm sorry, I misunderstood you. What should I do then? There's nothing in my libraries setting. – xvlaze Sep 19 '16 at 21:39
  • I can't tell then where the extra settings `-lallegro5 -fPIC` is coming from from what you have posted. I have tried reproducing and I simply can't. The other symptom you have pointed out, the unexpected libPong name is a clue as to what may be wrong. But I am clutching at straws. Perhaps there is some issue with how the project was originally created? – Jonah Graham Sep 19 '16 at 22:36
  • Updated the question. – xvlaze Sep 20 '16 at 21:05
  • Updated the answer – Jonah Graham Sep 22 '16 at 11:47
  • Worked! How could I make this settings IDE-global? I've been trying to run similar applications, but I have to configure it every time I want to do it. Also, I find really strange that the linker flags of the project object of this question have suddenly disappeared, though it still works perfectly. However, creating an identical one will need the linker flags, and fail its execution if I remove them. – xvlaze Sep 23 '16 at 10:46
  • Great to hear it worked for you. If you have a new question, ask it as a new question (and don't forget to put in what you tried/[MCVE]/etc). By asking the question in the comments only I am likely to see it, by asking a new one it increases the chances of the whole SO community to see and respond. – Jonah Graham Sep 25 '16 at 10:42
  • Here you have the links to the question. Thanks for your advise! http://stackoverflow.com/questions/39692380/make-ide-wide-configuration-changes-in-eclipse – xvlaze Sep 25 '16 at 22:49