Compiling Allegro 5 Program through command line on a Mac - c++

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.

Related

unidentified reference to glutInit

I tried to compile a basic OpenGL program, simply just a blank window.
I'm using CodeLite with the g++ compiler on Linux Mint 18.1 (Ubuntu 16.04 LTS).
The code so far is:
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
printf("hello world\n");
return 0;
}
At first my compiler (G++ in CodeLite) gave the error
/home/USER/Projects/CodeLite/Graphical/main.cpp:7: undefined reference to `glutInit'
I downloaded all the up to date GLEW and GLUT includes from their respective websites and unpacked them to /usr/include/GL, I edited the project settings linker like this.
Now it gives the error message:
/usr/bin/ld: cannot find -lglut
Makefile:4: recipe for target 'All' failed
What can I do to fix this?
You should install the libraries from the package manager instead: libglew-dev, freeglut3-dev, libgl1-mesa-dev and libglu1-mesa-dev. The includes and binary files will be placed in the appropriate location.
If the linker cannot find the .so files, locate them with dpkg -L freeglut3-dev and add this directory in the linker command line
g++ -L/path/to/libglut.so *.o -o programname

Can't access SDL library when trying to compile using g++

I'm getting started with SDL -- that is, I have no experience and am basically just trying to get something to compile at this point.
I have a basic program:
#include "sdl/SDL.h"
int main(){
// Fire up SDL, this starts all subsystems; audio video etc.
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// Now Shut it down
atexit(SDL_Quit);
return 0;
}
And I have this which I'm typing into the terminal:
g++ 'sdl-config --cflags --libs' -o sdltest sdltest.cpp
where sdltest.cpp is the name of the file.
This gives me the error:
g++: error: stl-config --cflags --libs: No such file or directory
Now, previously I have (I think) installed SDL like so:
sudo apt-get install libsdl2-dev
The issue might have to do with a faulty installation -- this might not be the package I was supposed to install, or I might be completely forgetting a step here.
I'm using Ubuntu, and compiling using g++, if it wasn't apparent from context.

Compile OpenGL program in Mac Terminal

I am using Mac Os X 10.9.5 fully updated with XCode version 6.0.1. I have also installed the command line utilities that have to be installed after installing XCode. I am using GLFW and GLEW in my openGL libraries. GLEW was installed manually while GLFW was installed with Macports.
I want to do coding without XCode as I have been doing it on Windows (with MingW) and Linux. I have googled how to compile the program and also searched on stackoverflow.
So, currently my compile command looks like this
(This is a C++ program)
g++ -framework Cocoa -framework OpenGL -framework CoreFoundation -framework CoreVideo -framework IOKit -L/usr/lib -L/usr/local/lib -I/usr/include/GLFW -I/usr/local/include/GL main.cpp -o main
I keep on getting the following error:
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
_main in main-ba7a57.o
"_glfwInit", referenced from:
_main in main-ba7a57.o
"_glfwMakeContextCurrent", referenced from:
_main in main-ba7a57.o
"_glfwTerminate", referenced from:
_main in main-ba7a57.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The program is:
#define GLEW_STATIC
#include <glew.h>
#include <glfw3.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
GLFWwindow* window;
if(!glfwInit()) exit(EXIT_FAILURE);
window = glfwCreateWindow(1024, 768, "glfw", NULL, NULL);
if(!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
const GLubyte* version = glGetString(GL_VERSION);
cout << "OpenGL version: " << version << endl;
glfwTerminate();
return 0;
}
It's only a guess, but try adding `-lglfw' to the end of your command-line. This tells g++ to compile against libglfw.
The fact that the error says Undefined symbols for architecture x86_64 makes me think perhaps the versions of the libraries you installed were built to target 64 bit architectures. Remember when choosing libraries to install that you should be choosing 32 bit or 64 bit versions based on which architectures you intend to target, not which architecture your development machine uses.
See this other question for reference.

Linker Error when building Xcode project using external library

I am attempting to build a very simple command line application, in Xcode, that will print out basic information about MXF video files. In order to do this I need to use the libmxf, libbmx, and libbmx libraries available for download here:
http://sourceforge.net/p/bmxlib/home/Home/
My C++ code is incredibly simple at this point:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <vector>
#include <bmx/mxf_reader/MXFFileReader.h>
#include <bmx/mxf_reader/MXFGroupReader.h>
#include <bmx/mxf_reader/MXFSequenceReader.h>
#include <bmx/mxf_reader/MXFFrameMetadata.h>
#include <bmx/MXFUtils.h>
#include <bmx/Utils.h>
using namespace std;
using namespace bmx;
#define MXF_OPEN_READ(fn, pf) mxf_disk_file_open_read(fn, pf)
int main(int argc, const char * argv[])
{
std::vector<const char *> filenames;
std::cout << "mxfheader: execution beginning...\n";
for (int cmdln_index = 0; cmdln_index < argc; cmdln_index++) {
if (!check_file_exists(argv[cmdln_index])) {
if (argv[cmdln_index][0] == '-') {
fprintf(stderr, "Unknown argument '%s'\n", argv[cmdln_index]);
} else {
fprintf(stderr, "Failed to open input filename '%s'\n", argv[cmdln_index]);
}
return 1;
}
filenames.push_back(argv[cmdln_index]);
}
std::cout << filenames[0] << "\n";
return 0;
}
When I compiled the BMX library, I was sure to run configure with 64-bit support, like so:
./configure --build=x86_64-apple-darwin11.4.2 --host=x86_64-apple-darwin11.4.2 CFLAGS="-arch x86_64" CXXFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" CC=clang CXX=clang++
In the XCode Project under Build Settings, I have added /usr/local/lib to my Search Paths. Under Build Phases, I have added "libbmx-0.1.3.dylib", "libMXF-1.0.4.dylib", and "libMXF++-1.0.4.dylib" to the "Link Binary With Libraries" section.
I have verified that these libraries are, indeed, 64-bit ( file libbmx-0.1.3.dylib returns libbmx-0.1.3.dylib: Mach-O 64-bit dynamically linked shared library x86_64 ).
Every time I try and build the application, I get the following linker error:
Ld /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader normal x86_64
cd /Users/ned/Documents/src/mxfheader
setenv MACOSX_DEPLOYMENT_TARGET 10.7
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -L/usr/local/lib -F/Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug -filelist /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Intermediates/mxfheader.build/Debug/mxfheader.build/Objects-normal/x86_64/mxfheader.LinkFileList -mmacosx-version-min=10.7 -stdlib=libc++ -lbmx-0.1.3 -lMXF-1.0.4 -lMXF++-1.0.4 -o /Users/ned/Library/Developer/Xcode/DerivedData/mxfheader-bkwawmplsoqpdadfxartceqkbolo/Build/Products/Debug/mxfheader
Undefined symbols for architecture x86_64:
"bmx::check_file_exists(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any help would be appreciated. Thanks!
Your problem is the option: -stdlib=libc++ in the command line. It's causing a link to the wrong libc++, you need to make it -stdlib=libstdc++, as this is the stdlib that the libbmx library is compiled against.
under the Apple LLVM compiler options for the C++ standard library, select: libstdc++, or pick compiler default (which should choose libstdc++ also)

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

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.