gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > undefined Reference

#163917 - Bong - Wed Oct 15, 2008 2:16 am

I am trying to compile my game with the irq functions and am getting the error undefined reference to irq_init(), irq_add(), and VBlankIntrWait() which is part of the tonc.h header(tonc library). Here is my code:
Code:

#include <tonc.h>

//---------------------------------------------------------------------------------
// Prototypes
//---------------------------------------------------------------------------------
void ballMove();
void getInput();
bool hasWon( u8 score );
bool intersectsBottomBoundry( u16 y );
bool intersectsLeftBoundry( u16 x );
bool intersectsRighttBoundry( u16 x );
bool intersectsTopBoundry( u16 y );
void paddleMoveDown( u8 paddleid );
void paddleMoveUP( u8 paddleid );

//---------------------------------------------------------------------------------
// Inlines
//---------------------------------------------------------------------------------


//---------------------------------------------------------------------------------
// Structures
//---------------------------------------------------------------------------------

// Player Structure
struct Player {
   u16 y;
   u8 score;
}player[2];

// Ball Structure
struct Ball {
   u16 x;
   u16 y;
   s8 vx;
   s8 vy;
}ball;

//---------------------------------------------------------------------------------
// Enumuationss
//---------------------------------------------------------------------------------
enum paddle{ left, right };

//---------------------------------------------------------------------------------
// Program entry point
//---------------------------------------------------------------------------------
int main() {
//---------------------------------------------------------------------------------

   //set the display mode to Mode 3 and background to 2
   REG_DISPCNT = DCNT_MODE3 | DCNT_BG2;

   // enable isr switchboard and VBlank interrupt
   //irq_init(NULL);
   //irq_add(II_VBLANK, NULL);

   // start of main game loop
   while(1)
   {
   
   
   // Wait for VSync
   //VBlankIntrWait();
   }
}


//---------------------------------------------------------------------------------
// Function: ballMove()
// Purpose: Moves the ball based on it velocities
//---------------------------------------------------------------------------------
void ballMove()
{
   ball.x += ball.vx;
   ball.y += ball.vy;
}

//---------------------------------------------------------------------------------
// Function: getInput()
// Purpose: performs actions based on the users input
//---------------------------------------------------------------------------------
void getInput()
{
   if(key_is_down(KEY_UP))
   {
   }
   else if(key_is_down(KEY_DOWN))
   {

   }
}

//---------------------------------------------------------------------------------
// Function: hasWon()
// Purpose: Returns if a player has won
//---------------------------------------------------------------------------------
bool hasWon( u8 score )
{
 if( score > 11)
 {
 return true;
 }
 else
 {
 return false;
 }
}

//---------------------------------------------------------------------------------
// Function: intersectsBottomBoundry()
// Purpose: Returns if an object has intersected the bottom boundry
//---------------------------------------------------------------------------------
bool intersectsBottomBoundry( u16 y )
{
   if( y > 160)
   {
   return true;
   }
   else
   {
   return false;
   }
}

//---------------------------------------------------------------------------------
// Function: intersectsLeftBoundry()
// Purpose: Returns if an object has intersected the left boundry
//---------------------------------------------------------------------------------
bool intersectsLeftBoundry( u16 x )
{
   if( x < 0)
   {
   return true;
   }
   else
   {
   return false;
   }
}

//---------------------------------------------------------------------------------
// Function: intersectsRightBoundry()
// Purpose: Returns if an object has intersected the right boundry
//---------------------------------------------------------------------------------
bool intersectsRightBoundry( u16 x )
{
   if( x > 240)
   {
   return true;
   }
   else
   {
   return false;
   }
}

//---------------------------------------------------------------------------------
// Function: intersectsTopBoundry()
// Purpose: Returns if an object has intersected the top boundry
//---------------------------------------------------------------------------------
bool intersectsTopBoundry( u16 y )
{
   if( y > 0)
   {
   return true;
   }
   else
   {
   return false;
   }
}

//---------------------------------------------------------------------------------
// Function: paddleMoveDown()
// Purpose: Move the paddle down the screen on its y position
//---------------------------------------------------------------------------------
void paddleMoveDown( u8 paddleid )
{
   player[paddleid].y++;
}

//---------------------------------------------------------------------------------
// Function: paddleMoveUp()
// Purpose: Move the paddle up the screen on its y position
//---------------------------------------------------------------------------------
void paddleMoveUp( u8 paddleid )
{
   player[paddleid].y--;
}


Just uncomment the irq functions and you'll see what i mean. Any ideas?

Sincerely,

Bong

#163922 - no2pencil - Wed Oct 15, 2008 6:25 am

Rather than

Code:

#include <tonc.h>


Try
Code:

#include "tonc.h"


The file tonc.h would need to be in your include path for the 1st instance to be correct. Otherwise you will need to provide the absolute path for the file. As long as it's in the current directory, the 2nd example should work fine for you.
_________________
-#2pencil

#163925 - Cearn - Wed Oct 15, 2008 7:36 am

It's not a header problem (that's the "undeclared identifier" error); it's a linker problem. The actual code for tonc's functions is in libtonc.a, and this file should be linked to the project. Assuming you're using one of the template makefiles, add "-ltonc" to LIBS and "path_to_tonc/tonclib" to LIBDIRS.

Sidenotes: get into the habit of not using smaller datatypes (s8/u8 and s16/u16) for local variables and function arguments. In this case it won't really matter, but in calculation-heavy routines it can increase their runtimes by 10% to 100% (example). Prefer 32-bit types (int s32/u32) over the smaller ones.

The left and top collision functions will always return true because the datatype of u16 is unsigned and therefore >=0 by definition. The routines can also be reduced to something like
Code:
bool intersectLeftBoundary(int x)
{
    return (x<0);
}

as the result of a comparison is a bool anyway. That said, the longer version is probably clearer.