Search code examples
c++linkerlinker-errorsallegro5

Undefined reference using Allegro 5 with mingw-w64


I'm trying to compile an Allegro 5 program on Windows 10 with mingw-w64.

  • I already had installed mingw-w64. Output from g++ --version is:

    g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
    Copyright (C) 2017 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
  • I downloaded windows binaries for Allegro 5 from https://github.com/liballeg/allegro5/releases (File: allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip) and unzipped the file into C:/allegro5 so now I have C:/allegro5/bin, C:/allegro5/include, C:/allegro5/lib.

  • A small test program:

    #include <stdio.h>
    #include <allegro5/allegro.h>
    
    int main(int argc, char **argv)
    {
       al_init();
       return 0;
    }
    
  • And finally the command I run to compile: g++ test.cpp -I"C:/allegro5/include" -L"C:/allegro5/lib" -lallegro (There is a lib file called liballegro.dll.a under C:/allegro5/lib)

But there are some problems while linking:

C:\Users\xxxx\AppData\Local\Temp\ccg5z97Y.o:test.cpp:(.text+0x1e): undefined reference to `al_install_system'
collect2.exe: error: ld returned 1 exit status

A) What may be the reason for this ?

B) What should I do to compile in a static way ? Is changing -lallegro to -lallegro-static enough ?


Solution

  • This:

    g++.exe (i686-posix-dwarf-rev2, Built by MinGW-W64 project) 7.1.0
    

    is one of the the 32-bit GCC variants provided by MinGW-W64. You are attempting to link the 32-bit code it generates with the 64-bit libraries provided in:

    allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.0.zip
    

    which will not work. Replace your compiler with the appropriate 64-bit variant x86_64-posix-seh