Search code examples
c++macoslinkerprogram-entry-pointallegro

_main not defined on Mac OS X 10.11 in c++


I am currently writing a c++ game using allegro 5.0 and c++. I will eventually get it working on mac, linux, windows, and possibly android and iPhone. Currently it compiles and runs on linux (tested on arch linux latest), but on mac there is a slight problem:

Using the apple provided g++, it complains about a linking error, _main not defined. However, I have checked and checked, and main is definitely defined in my file. It must be anyway because it compiles and runs on linux, but I can't figure it out.

I have googled many many days and many many terms (too many to list) and haven't found anything to do with mac and main not being defined. Could there be something I'm missing? Something i need to do special on mac? One last thing to note is when I link with allegro_main.a it links, but then crashes. Linking with this library is not necessary on linux. I am using allegro5 through macports. It is also useful to note that the sh script used to compile on mac is comp_mac.sh, and linux is simply comp.sh. Note that you may need to modify the sh script(s) to compile on your environment. These are only tested on my environment (macports) so far.

My code can be found here: http://github.com/2mb-solutions/horseshoes/

Thanks for all the help I can get.

-Michael.


Solution

  • The "_main" undefined symbol error is occurring because you are not linking with the allegro_main addon.

    As the Allegro 5 manual specifies, in C and C++ projects, the file containing your main() function must include the core header allegro5/allegro.h. Additionally, you must link with the allegro_main addon.

    It might be easiest to use pkg-config:

    pkg-config --cflags --libs allegro-5 allegro_ttf-5 allegro_audio-5 allegro_acodec-5 allegro_font-5 allegro_main-5
    

    You can place that command within backticks ( ` ) to include the output of pkg-config in your build command.