I haven't coded something this complex in C++ before and I haven't had time to really review. I'm making a game (well, foundations for a game anyway), with an external game library, Allegro. But I think the problems I'm having is more fundamental to C++ in general. Included is a .zip of my program but I'm curious how I should utilize the following: The code has a main and 2 headers. Both are meant to be objects. One object controls a player on the screen, specifically its behaviors. The other controls the animation itself. I don't see a need to have the player header
player > animator > main
program9handler: player
p9animationhandler: animator
program9rev1: main

Its explained more clearly here:

http://answers.yahoo.com/question/index;_ylt=AvJDNruIViuLxPz2KfNebEjsy6IX;_ylv=3?qid=20111207120941AAJSeWb

but I'm having linkage errors with the animator:

Im compiling using g++ on mac 10.5.8

jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program/program9rev1.cpp -o /users/jasondancks/Desktop/allegrostuff/program/p9rev `allegro-config --libs`
Undefined symbols:
  "p9animationhandler::setmargin(int)", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::~p9animationhandler()", referenced from:
      ___tcf_0 in ccO9Z6xX.o
  "p9animationhandler::animate()", referenced from:
      draw()    in ccO9Z6xX.o
  "p9animationhandler::go()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::stop()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::setbitmaps(BITMAP*)", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::setdelay(int)", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::turnleft()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::reverse()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::setuptanks()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::draw()", referenced from:
      draw()    in ccO9Z6xX.o
  "p9animationhandler::turnright()", referenced from:
      _mangled_main()     in ccO9Z6xX.o
  "p9animationhandler::p9animationhandler()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccO9Z6xX.o

Recommended Answers

All 11 Replies

How are you compiling this - using g++ in the console directly, using a Makefile, or using an IDE such as XCode? Are you certain that the library itself (not just the header file) is being included in you libraries?

I've never used Allegro, but look at Line 2 of your posted errors. It says that they are "Undefined Symbols". That would indicate to me that you are calling functions without declaring them first. For example:

#include <iostream>

using std::cout;

int main() {
  int someInt = someIntFunc();

  return 0;
}

int someIntFunc() {
  return 1;
}

This snippet shouldn't compile. It should produce an error similar to "Undeclared Identifier", or "identifier not found" because you are attempting to call a function that has not yet been declared. Your posted errors read like something similar to this is happening.

Are you sure you don't need the "player" header?

that much I understand. I guess what Im getting at is in what order does the compiler compile/define the headers and classes. I assumed it would start with program9handler because:

main.cpp:
#include "p9animationhandler.h"

jump to p9animationhandler.h

p9animation.h:
#ifndef P9_H
#define P9_H
#include "program9handler.h"

jump to program9handler.h:

program9handler.h:
#ifndef P9_H
#define P9_H
#include <allegro.h>
#include bunch of other headers

then go to program9handler.cpp, compile

then jump back to p9animationhandler.h, define it,

then jump to p9animationhandler.cpp (Where those classes are indeed defined)

compile, then jump back to main

and compile


Is this right? I don't know.

@Schoil-R-LEA

Yes I'm using the console directly. Sorry for the double post

The order that the compiler compiles your headers and sources is defined in a "makefile". You need to either create one on your own either create a project using a modern IDE and import these files in it, then compile.

The order that the compiler compiles your headers and sources is defined in a "makefile". You need to either create one on your own either create a project using a modern IDE and import these files in it, then compile.

What? So you're telling me to either make a makefile or use an IDE? You're a big help. What if I want to learn how to do this stuff without?

alright. Check out what something I tried after checking out Stack Overflow:
http://stackoverflow.com/questions/2980102/combine-two-gcc-compiled-o-object-files-into-a-third-o-file

help me find the black box on this disaster:

Last login: Thu Dec  8 12:55:54 on console
jdd:~ jasondancks$ cd Desktop/allegrostuff/program
jdd:program jasondancks$ g++ -c program9handler.cpp
jdd:program jasondancks$ g++ -c p9animationhandler.cpp
jdd:program jasondancks$ cd ../../..
jdd:~ jasondancks$ cd allegro-4.2.2
jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program9rev1.cpp -o /users/jasondancks/Desktop/allegrostuff/p9rev `allegro-config --libs`
i686-apple-darwin9-g++-4.0.1: /users/jasondancks/Desktop/allegrostuff/program9rev1.cpp: No such file or directory
jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program/program9rev1.cpp -o /users/jasondancks/Desktop/allegrostuff/program/p9rev `allegro-config --libs`
Undefined symbols:
  "p9animationhandler::setmargin(int)", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::~p9animationhandler()", referenced from:
      ___tcf_0 in ccws4bJB.o
  "p9animationhandler::animate()", referenced from:
      draw()    in ccws4bJB.o
  "p9animationhandler::go()", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::stop()", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::setbitmaps(BITMAP*)", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::setdelay(int)", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::turnleft()", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::reverse()", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::setuptanks()", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::draw()", referenced from:
      draw()    in ccws4bJB.o
  "p9animationhandler::turnright()", referenced from:
      _mangled_main()     in ccws4bJB.o
  "p9animationhandler::p9animationhandler()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccws4bJB.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
jdd:allegro-4.2.2 jasondancks$ ar rvs /users/jasondancks/Desktop/allegrostuff/program/animo.a /users/jasondancks/Desktop/allegrostuff/program/program9handler.o /users/jasondancks/Desktop/allegrostuff/program/p9animationhandler.o
ar: creating archive /users/jasondancks/Desktop/allegrostuff/program/animo.a
a - /users/jasondancks/Desktop/allegrostuff/program/program9handler.o
a - /users/jasondancks/Desktop/allegrostuff/program/p9animationhandler.o
jdd:allegro-4.2.2 jasondancks$ ld -r /users/jasondancks/Desktop/allegrostuff/program/program9handler.o /users/jasondancks/Desktop/allegrostuff/program/p9animationhandler.o -o /users/jasondancks/Desktop/allegrostuff/program/a.o
jdd:allegro-4.2.2 jasondancks$ g++ -c /users/jasondancks/Desktop/allegrostuff/program/program9rev1.cpp 
jdd:allegro-4.2.2 jasondancks$ g++ a.o program9rev1.o -o p9rev `allegro-config --libs`
i686-apple-darwin9-g++-4.0.1: a.o: No such file or directory
jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program/a.o /users/jasondancks/Desktop/allegrostuff/program/program9rev1.o -o /users/jasondancks/Desktop/allegrostuff/program/p9rev `allegro-config --libs`
i686-apple-darwin9-g++-4.0.1: /users/jasondancks/Desktop/allegrostuff/program/program9rev1.o: No such file or directory
jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program/a.o /users/jasondancks/Desktop/allegrostuff/program/program9rev1.cpp -o /users/jasondancks/Desktop/allegrostuff/program/p9rev `allegro-config --libs`

error message when I tried to run the executable:

Last login: Thu Dec  8 13:09:08 on ttys000
/Users/jasondancks/Desktop/allegrostuff/program/p9rev ; exit;
jdd:~ jasondancks$ /Users/jasondancks/Desktop/allegrostuff/program/p9rev ; exit;2011-12-08 13:25:49.867 p9rev[1204:351b] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
Shutting down Allegro due to signal #8
Floating point exception
logout

[Process completed]

it generated a crash report and everything. It doesn't look helpful though

BTW, I noticed that you are using Allegro 4.2.2, rather than 5.0. Did you compile the Allegro libraries yourself, or get the binaries from some other source? If the latter, where did you get them?

If the libraries were compiled some time ago, using the QuickDraw libraries rather than Quartz, that could explain one of the warnings you got while running the program. I don't know how much it would impact the overall problem with linking, however.

Also, you might look to the Allegro Wiki for information, if you haven't already.

BTW, I noticed that you are using Allegro 4.2.2, rather than 5.0. Did you compile the Allegro libraries yourself, or get the binaries from some other source? If the latter, where did you get them?

If the libraries were compiled some time ago, using the QuickDraw libraries rather than Quartz, that could explain one of the warnings you got while running the program. I don't know how much it would impact the overall problem with linking, however.

Also, you might look to the Allegro Wiki for information, if you haven't already.

I want to say I got it from an old mirror site of sourceforge. I got to use 4.2.x to test some code I got. And yeah I compiled from source code: I read the readme and it went swimmingly.

I'm pretty sure QuickDraw is not the problem since I got a much shittier program to compile and run fine. For awhile at least but I know what the problem was.

Alright. I edited program9handler.cpp because I thought it might be the source of the floating point exception:

float b = (float)((speed*speed)/2);
int angle = (int)sqrt(b);

it looked something like this before:

int angle = (speed*speed)/2;
angle = (int)sqrt(angle);

and yes that actually compiled like that.

Then I re-compiled the whole thing:

jdd:program jasondancks$ g++ -c program9handler.cpp
jdd:program jasondancks$ g++ -c p9animationhandler.cpp
jdd:program jasondancks$ ld -r programhandler.o p9animationhandler.o -o a.o
ld: file not found: programhandler.o
jdd:program jasondancks$ ld -r program9handler.o p9animationhandler.o -o a.o
jdd:program jasondancks$ cd ../../../allegro-4.2.2
jdd:allegro-4.2.2 jasondancks$ g++ /users/jasondancks/Desktop/allegrostuff/program/a.o /users/jasondancks/Desktop/allegrostuff/program/program9rev1.cpp -o /users/jasondancks/Desktop/allegrostuff/program/p9rev2 `allegro-config --libs`
jdd:allegro-4.2.2 jasondancks$

and I got this:

Last login: Thu Dec  8 13:25:49 on ttys001
/Users/jasondancks/Desktop/allegrostuff/program/p9rev2 ; exit;
jdd:~ jasondancks$ /Users/jasondancks/Desktop/allegrostuff/program/p9rev2 ; exit;
2011-12-08 14:42:10.592 p9rev2[3638:351b] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
p9rev2(3638,0xb00a1000) malloc: *** error for object 0x315130: double free
*** set a breakpoint in malloc_error_break to debug
Shutting down Allegro due to signal #8
Floating point exception
logout

[Process completed]

So how do I set a breakpoint and how do I check where the error is coming from? Is this something I can check with gdb?

gdb detected an arithmetic exception in program9handler::initializesprite():

void program9handler::initializeSprite()
{
	char buf[1024];
	sprintf(buf,"%s.bmp",file);
	BITMAP* original = 0;
	BITMAP* rotated = 0;
	original = load_bitmap(buf,NULL);
	int w = original->w;
	int h = original->h;
	int length = (int) ( sqrt( (float) (w*w+h*h) ) ) + 1;
	rotated = create_bitmap(length,length);
	width = length;
	height = length;
	int et = 256/numturns;
	for(int i=0;i<numturns;i++)
	{
		//clear_to_color(rotated, bitmap_mask_color(rotated));
		clear_to_color(rotated,makecol(255,0,255));
		pivot_sprite(rotated,original,length/2,length/2,w/2,h/2,itofix(i*et));
		sprites[i] = rotated;
		sprintf(buf,"%s_%i.bmp",file,i);
		save_bitmap(buf,rotated,NULL);
	}
	destroy_bitmap(rotated);
	destroy_bitmap(original);
}

gdb got an ARITHMETIC_EXC: which turned out to be, once again, a floating point exception:

(gdb) run
Starting program: /Users/jasondancks/Desktop/allegrostuff/program/p9rev4 
Reading symbols for shared libraries +++++++++++............................................................................ done
Reading symbols for shared libraries .. done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
2011-12-09 04:00:30.837 p9rev4[1879:351b] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done

Program received signal EXC_ARITHMETIC, Arithmetic exception.
[Switching to process 1879 thread 0x351b]
0x000024c6 in program9handler::initializeSprite ()
(gdb) backtrace
#0  0x000024c6 in program9handler::initializeSprite ()
#1  0x00002621 in program9handler::setSprite ()
#2  0x00003387 in p9animationhandler::setuptanks ()
#3  0x000033f9 in p9animationhandler::setmargin ()
#4  0x0000396e in _mangled_main ()
#5  0x00003fd9 in call_user_main ()
#6  0x95ff89a4 in __NSThread__main__ ()
#7  0x91aba055 in _pthread_start ()
#8  0x91ab9f12 in thread_start ()

initializesprite:

void program9handler::initializeSprite()
{
	char buf[1024];
	sprintf(buf,"%s.bmp",file);
	BITMAP* original;
	BITMAP* rotated;
	original = load_bitmap(buf,NULL);
	int w = original->w;
	int h = original->h;
	int length = (int) ( sqrt( (float) (w*w+h*h) ) ) + 1;
	rotated = create_bitmap(length,length);
	width = length;
	height = length;
	int et = 256/numturns;
	for(int i=0;i<numturns;i++)
	{
		clear_to_color(rotated,makecol(255,0,255));
		pivot_sprite(rotated,original,length/2,length/2,w/2,h/2,itofix(i*et));
		sprites[i] = rotated;
		sprintf(buf,"%s_%i.bmp",file,i);
		save_bitmap(buf,rotated,NULL);
	}
	destroy_bitmap(rotated);
	destroy_bitmap(original);
}

Its looks good from here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.