4

Today I installed the Allegro game programming library for C and I’ve tried to include one of the header files but when I try to execute gcc -I./include example.c -o a.exe in the terminal, I keep on getting this error:

Undefined symbols for architecture x86_64:
  "_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)

Any ideas? I installed Allegro 5 using the instructions here: https://wiki.allegro.cc/index.php?title=Install_Allegro5_From_GIT/OSX

example.c code:

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

int main(int argc, const char *argv[]){
    puts(“Hello, world!”);
    return 0;
}
Calculus5000
  • 427
  • 6
  • 17

1 Answers1

8

You need to link your executable to Allegro.

According to the FAQ, you should add -lallegro to your compile command, or -lallegro -lallegro_main on OSX

You may need other flags, and Allegro 5 uses pkg-config instead of allegro-config, so do pkg-config allegro-5.0 allegro_main-5.0 --cflags --libs to find out.

You can combine this into a compiler command by using backticks, e.g.

$CC -W -Wall `pkg-config allegro-5.0 allegro_main-5.0 --cflags --libs` foo.c -o foo.exe
Iskar Jarak
  • 5,136
  • 4
  • 38
  • 60
  • It works, thanks! However, I noticed that from the `pkg-config` command that allegro's header files are in `/usr/local/include`. Do I still need to include that as a flag when compiling or is that folder included in `PATH` seeing as it contains many other header files for other libraries? – Calculus5000 Sep 18 '15 at 00:12
  • 2
    @Calculus5000 It depends on the search path of your compiler - for example, GCC on a normal *nix platform will look in `/usr/local/include` (and various other locations) by default unless you tell it to look elsewhere. I believe clang is the same. So if you're using using GCC or clang you don't need to include it as a flag. – Iskar Jarak Sep 18 '15 at 00:16
  • Also would it be possible to get a brief explanation on how that `pkg-config` command works? – Calculus5000 Sep 18 '15 at 00:16
  • 1
    @Calculus5000 `pkg-config` is a pretty standard tool that looks in certain locations for metadata with information about installed packages and libraries. Usually it is used for version info, linker flags, etc. When you did `make install` (or equivalent xcode command or whatever), one of the things that happened is the metadata for Allegro was copied to where `pkg-config` checks for that stuff (e.g. `/usr/share/pkgconfig/`, `/usr/lib/pkgconfig/`, any paths in `PKG_CONFIG_PATH`). – Iskar Jarak Sep 18 '15 at 00:19
  • 2
    Awesome answer. In case it's helpful for anyone else, this helped me compile a similar script with g++. I was able to compile by running `g++ foo.cpp -o foo -lallegro_main -lallegro`. I had to have both -`lallegro` and `-lallegro_main`. – Patrick Williams Jun 25 '16 at 03:51