Compiling C++ code with allegro 5 and g++ - c++

What flags do I need to add to g++ in order to compile code using allegro 5? I tried
g++ allegro5test.cpp -o allegro5test `allegro-config --libs`
but that is not working. I'm using ubuntu 11.04. I installed allegro 5 using the instructions at http://wiki.allegro.cc/index.php?title=Install_Allegro5_From_SVN/Linux/Debian
I tried:
g++ allegro5test.cpp -o allegro5test `allegro-config --cflags --libs`
And it also gives a bunch of undefined errors, like: undefined reference to `al_install_system'
allegro-config --cflags --libs outputs:
-I/usr/local/include
-L/usr/local/lib -lalleg

So you successfully installed allegro5 on your system from the SVN. One thing you should know is that this build doesn't come with allegro-config. If you have it on your system it means you have previously installed allegro4.
allegro5 brings many changes, including different initialization procedures, library and function names.
Here's a hello world application for new version:
#include <stdio.h>
#include <allegro5/allegro.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(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
Notice how the command to compile this application refers to another include directory and library names, which are different from the previous version of allegro:
g++ hello.cpp -o hello -I/usr/include/allegro5 -L/usr/lib -lallegro

Allegro 5 uses pkg-config.
pkg-config --libs allegro-5.0 allegro_image-5.0
And so on for each library you are using.

Related

SDL returns no output when I try to print statements using cout

I installed the MinGW version of SDL from their website.
I created a sample piece of code just to test if I could include the library without any errors.
#include <iostream>
#include <SDL.h>
using namespace std;
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL INIT FAILED" << endl;
return 1;
}
cout << "SDL INIT SUCCEEDED" << endl;
SDL_Quit();
return 0;
}
I also created a Makefile:
#OBJS specifies which files to compile as part of the project
OBJS = main.cpp
#CC specifies which compiler we're using
CC = g++
#INCLUDE_PATHS specifies the additional include paths we'll need
INCLUDE_PATHS = -Isrc\includes
#LIBRARY_PATHS specifies the additional library paths we'll need
LIBRARY_PATHS = -Lsrc\lib
#COMPILER_FLAGS specifies the additional compilation options we're using
# -w suppresses all warnings
# -Wl,-subsystem,windows gets rid of the console window
COMPILER_FLAGS = -w -Wl,-subsystem,windows
#LINKER_FLAGS specifies the libraries we're linking against
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2
#OBJ_NAME specifies the name of our exectuable
OBJ_NAME = main
#This is the target that compiles our executable
all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
If I don't include the int argc, char* argv[] inside of int main() and try to ming32-make, it throws an error:
C:\Users\username\Documents\Projects\C++\SDL_test> mingw32-make
g++ main.cpp -Isrc\includes -Lsrc\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o main
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: src\lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.26.2-source/foo-x64/../src/main/windows/SDL_windows_main.c:82: undefined reference to `SDL_main'
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [Makefile:26: all] Error 1
When I include int argc, char* argv[], it doesn't give any errors but doesn't print anything either.
C:\Users\username\Documents\Projects\C++\SDL_test> mingw32-make
g++ main.cpp -Isrc\includes -Lsrc\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o main
C:\Users\username\Documents\Projects\C++\SDL_test>
When I use make instead of mingw32-make, the output remains the same.
I am using VSCode and I have included the header files and lib files in an src folder in the same directory as my script and also moved the SDL2.dll file in the root folder:
My C++ Configuration on VSCode:
Compiler Path: C:\MinGW\bin\g++.exe
Compiler Arguments:
IntelliSense mode: gcc-x64 (legacy) // Because using anything else says the the mode is incompatible with the compiler path.
Include path:
${workspaceFolder}/**
${workspaceFolder}/src/includes
I had also recieved SDL.h: file or directory not found errors before this and I fixed them by creating the Makefile.
Is there something I'm missing? Does SDL not output to stdout, because I've seen tutorials online and they are able to get outputs from cout fine on them.
I am expecting cout to work when I run the script.
-Wl,-subsystem,windows (aka -mwindows) hides the console window, and with it all output. Remove it, and use it only in the release builds.
-w suppresses all warnings
This is extremely unwise. Prefer -Wall -Wextra -Wdeprecated to enable most common warnings, plus -std=c++20 -pedantic-errors to enforce standard compliance (replace 20 with the latest version supported by your compiler).
As suggested by #keltar, you might be able to get output even from a program built with -mwindows if you redirect it to a file, using my_program.exe >output.txt.
EDIT: The issue was that I hadn't installed SDL2 the right way. I installed MSYS2 and used it to install MinGW-w64.
I then used the Msys2 command-line interface to install SDL2 using these commands:
pacman -Syu
pacman -Su
pacman -S mingw-w64-x86_64-SDL2
This installed the header and lib files for SDL in their proper locations. I was then able to include those files in my code.
I changed the main function from int main(int argc, char* argv[]) to int WinMain(int argc, char* argv[]) because I'm on Windows and this helps get rid of the undefined reference to WinMain error.
My working code:
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
int WinMain(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "SDL INIT FAILED" << endl;
return 1;
}
cout << "SDL INIT SUCCESSFUL" << endl;
}

Compiling Allegro 5 Program through command line on a Mac

I followed to steps to build and install Allegro 5 from their wiki (found here: https://wiki.allegro.cc/index.php?title=Main_Page) and seemingly succeeded with no problems.
allegro was installed to the following (as the wiki suggests) /usr/local/include and usr/local/lib and I have confirmed allegro is there.
I then wrote the following code in vim:
#include <stdio.h>
#include <allegro5/allegro.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(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
I am new to using Unix and have only ever compiled c++ programs with g++ that were simple hello world files with no libraries needed.
Therefore after searching around on the internet I tried the following commands:
g++ hello.cpp -o hello `pkg-config --libs allegro-5`
resulting in the following:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: __al_mangled_main)
ld: symbols not found for architecture x86_64
clang: error: linker command failed with exit code 1
BTW, I used homebrew to install dependencies instead of macports
brew install pkg-config
brew install zlib
etc...
It seems like a linking problem.
What am I doing wrong?
try install allegro with homebrew and use gcc -o main main.c -lallegro -lallegro_main
because the allegro_main is a compatibility library that allows the main function to work on all compilers. Required only from OS X.

SDL library in linux

I'm trying to compile this code:
#include "SDL/SDL.h"
int main(void) {
SDL_Surface *Hello = NULL;
SDL_Surface *Screen = NULL;
SDL_Init( SDL_INIT_EVERYTHING );
return 0;
}
But it happens that the compiler says that:
undefined reference to SDL_Init
I dont know why this is happening. I'm using Debian Mint and Code::Blocks. Could you Help me?
It looks like you haven't got -lSDL on your link line.
sdl-config returns the compile and link flags for your installation of SDL.
Assuming the program is sdl.cpp
g++ -o sdl `sdl-config --cflags` sdl.cpp `sdl-config --libs`
Should give you the correct flags.
Go to project and then build options and select your project name.
Now go to the linker setting and type the following lines in the Other Linker options textbox:
-lSDLmain
-lSDL
SDL also requires command line arguments in the main function so you should change
int main(void)
to
int main(int argc, char **argv)
Now compile your project and it should work.

error with gtkmm 3 in ubuntu 12.04

i install libgtkmm-3.0-dev in ubuntu 12.04 and i try to learn and write program with c++ and gtkmm 3
i go to this link "http://developer.gnome.org/gtkmm-tutorial/unstable/sec-basics-simple-example.html.en" and try to compile simple example program :
#include <gtkmm.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv,
"org.gtkmm.examples.base");
Gtk::ApplicationWindow window;
return app->run(window);
}
my file name is "basic.cc" and i open terminal and type following command to compile:
g++ basic.cc -o basic `pkg-config gtkmm-3.0 --cflags --libs`
compile completed without any error but when i try to run program with type ./basic in terminal i get following error :
~$ ./simple
./simple: symbol lookup error: ./simple: undefined symbol:_ZN3Gtk11Application6createERiRPPcRKN4Glib7ustringEN3Gio16ApplicationFlagsE
~$
how can i solve this problem ?
i can cimpile any gtkmm 2.4 code with this command : " g++ basic.cc -o basic pkg-config gtkmm-3.0 --cflags --libs "
and this command : " g++ basic.cc -o basic pkg-config gtkmm-2.4 --cflags --libs "
thanks
I think you hit this gtkmm bug, apparently triggered by more recent versions of GTK+, and now fixed:
https://bugzilla.gnome.org/show_bug.cgi?id=681323
I have asked Ubuntu to update their package, but they are usually slow about that if they do it at all:
https://bugs.launchpad.net/ubuntu/+source/gtkmm3.0/+bug/1046469
You might want to try reinstalling libgtkmm-3.0-dev. The code compiles fine for me but I get a Seg Fault. It does work when I change Gtk::ApplicationWindow to Gtk::Window.
there is nothing wrong with your install. that code is bad.
try it again, using
Gtk::Window window;
instead of the ApplicationWindow. When the GNOME documentation for a given class has a description of "TODO", that's a bad thing.

what I'm including wrong ? undefined reference to al_init_image_addon error,

what I'm including wrong ?
I am using codeblocks + allegro5 + ubuntu 11.10
and getting this 2 errors
undefined reference to 'al_init_image_addon'
undefined reference to `al_init_primitives_addon'|
I did install allegro5 correctly with all the dependencies. The first tutorial on loading the allegro.h works fine, it creates a normal window apart from the xterm window.
I am following the "show in fullscreen tutorial" from the allegro wiki
#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include <allegro5/allegro_primitives.h>
#include "allegro5/allegro_native_dialog.h"
int main()
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_DISPLAY_MODE disp_data;
al_init();
al_init_image_addon(); // <---------ERROR HERE
al_init_primitives_addon();// < --------ERROR HERE TOO
al_set_new_display_flags(ALLEGRO_FULLSCREEN);
display = al_create_display(disp_data.width,disp_data.height);
al_rest(3);
al_destroy_display(display);
return 0;
}
In the event anyone has this issue, the fix is adding allegro_image-5.0 to your pkg-config path e.g.:
gcc game.c -o game $(pkg-config --cflags --libs allegro-5.0 allegro_image-5.0)
well, after doing a little google search and posting at allegro's homepage I got the correct answer,
I was missing the .so files in the linker section under TOOLS > Compiler&DEbugger > LINKER.
I had to add these lines
/usr/lib/liballegro_dialog.so
/usr/lib/liballegro_color.so
/usr/lib/liballegro_audio.so
/usr/lib/liballegro_image.so
/usr/lib/liballegro_physfs.so
/usr/lib/liballegro.so
/usr/lib/liballegro_font.so
/usr/lib/liballegro_acodec.so
/usr/lib/liballegro_main.so
/usr/lib/liballegro_memfile.so
/usr/lib/liballegro_primitives.so
/usr/lib/liballegro_ttf.so
and `pkg-config --libs allegro-5.0`
see this post for the screenshot.
http://hongouru.blogspot.com/2012/02/solved-allegro5-undefined-reference-to.html