Search code examples
cmakefileallegro5

Allegro5 makefile error


note: this is the final exercise in the Head First C book.

I have the following problem. I am trying to make a game using the allegro5.2 libraries. I want to use multiple .c files in order to organize everything neatly. However, I have problems compiling my programs using a makefile. I am trying to compile this easy program:

#include <stdio.h>
#include <allegro5/allegro.h>

const int disp_h = 640;
const int disp_w = 480;

int main(int argc, char **argv) {

ALLEGRO_DISPLAY *display;

if(!al_init()) {
    fprintf(stderr, "failed to initialize allegro!\n");
    return -1;
}

display = al_create_display(disp_h,disp_w);
if(!display) {
    fprintf(stderr, "failed to create display!\n");
    return -1;
}

al_rest(0.4);
al_destroy_display(display);

printf("bye bye!!!\n");

return 0;
}

The makefile is:

Blasteroids.o: allegro.h Blasteroids.c
    gcc -Wall -c Blasteroids.c

Blasteroids: Blasteroids.o allegro.h
    gcc -Wall -I/usr/include/allegro5 -L/usr/lib -lallegro -lallegro_main Blasteroids.o -o Blasteroids

Now, when I use the terminal this compiles fine, but now I seem to have a problem. The error given by the terminal (using the command make Blasteroids) is:

cc   Blasteroids.o   -o Blasteroids
Undefined symbols for architecture x86_64:
  "_al_create_display", referenced from:
      __al_mangled_main in Blasteroids.o
  "_al_destroy_display", referenced from:
      __al_mangled_main in Blasteroids.o
  "_al_install_system", referenced from:
      __al_mangled_main in Blasteroids.o
  "_al_rest", referenced from:
      __al_mangled_main in Blasteroids.o
  "_main", referenced from:
     implicit entry/start for main executable
     (maybe you meant: __al_mangled_main)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Blasteroids] Error 1

I don't know what I am doing wrong and I am very new to these things. I have searched for examples in makefiles, but they give me code like I am using now. I now I can just use a single line for the above program, but the idea is that I want to make my own .c files, make them into .o files and then link them together. Hence the makefile.


Solution

  • The make program looks for files named makefile or Makefile, with no extension. If you name your makefile something else, such as makefile.txt, then make can't find it and it will just use its own built-in rules which don't know anything about extra flags ro libraries that may be needed.

    So either rename your makefile to makefile or Makefile, or else specify the name of your makefile explicitly on the command line when you run make, such as make -f makefile.txt Blasteroids.

    Secondarily, if you don't specify a target on the command line then make will always build the first target. So, if you re-order your targets so that the one you usually want to build (in this case Blasteroids) is first, then you can just run make with no arguments and it will build that target.

    Unlike a programming language, the order of target definition doesn't matter to make: e.g., you don't have to define rules for all the object files first before the link line. Make reads the entire file and constructs an internal graph of prerequisite relationships, and nodes and edges in this graph can be added in any order.