"Undefined reference to" error.

Discussion about everything. New games, 3d math, development tips...
Post Reply
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

"Undefined reference to" error.

Post by 3DModelerMan »

I'm trying to define a C style function inside a namespace.

Code: Select all

////////////////////////////////////////////////////////////
// Author: Tanner Mickelson
// Description: Random utility functions and classes
// Implementation file: utils.cpp
////////////////////////////////////////////////////////////
#ifndef UTILS_H
#define UTILS_H
#include <Irrlicht.h>

namespace engine
{
 namespace utils
 {
  //Generates a hash number for input string.
  irr::u32 stringHash(irr::c8* string);
 }
}

#endif

Code: Select all

#include "include/utils.h"

//===========================================================================
//Generates a unique hash number for a string
//===========================================================================
irr::u32 stringHash(irr::c8* string)
{
 irr::u32 hash = 5381;
 int c;

 while (c = *string++)
 hash = ((hash << 5) + hash) + c;// hash * 33 + c 

 return hash;
}
That's the code I'm using. The library that it's in builds fine and everything. But when I try to use the library in another project, I get this error: error LNK2001: unresolved external symbol "unsigned int __cdecl engine::utils::stringHash(char *)" (?stringHash@utils@engine@@YAIPAD@Z). Does anyone know what's wrong?
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

i bet you are building into a dll and that you are using msvc.
then you have to tell the compiler to export your function.

So do this and then tell your compiler to define EXPORT

Code: Select all

////////////////////////////////////////////////////////////
// Author: Tanner Mickelson
// Description: Random utility functions and classes
// Implementation file: utils.cpp
////////////////////////////////////////////////////////////
#ifndef UTILS_H
#define UTILS_H
#include <Irrlicht.h>

#ifdef EXPORT
#define EXPORT_YOURENGINENAMEGOESHERE __declspec(dllexport)
#else
#define EXPORT_YOURENGINENAMEGOESHERE
#endif

namespace engine
{
 namespace utils
 {
  //Generates a hash number for input string.
  EXPORT_YOURENGINENAMEGOESHERE irr::u32 stringHash(irr::c8* string);
 }
}

#endif 
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Luben
Posts: 568
Joined: Sun Oct 09, 2005 10:12 am
Location: #irrlicht @freenode

Post by Luben »

Shouldn't it be

Code: Select all

#include "include/utils.h"

//===========================================================================
//Generates a unique hash number for a string
//===========================================================================
namespace engine
{
 namespace utils
 {
irr::u32 stringHash(irr::c8* string)
{
 irr::u32 hash = 5381;
 int c;

 while (c = *string++)
 hash = ((hash << 5) + hash) + c;// hash * 33 + c 

 return hash;
}
 }
}
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Post by 3DModelerMan »

I'm building it into a static library, and yes I'm using Visual C++. It works now though. Thanks.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

I had the same problem about a day ago :wink:
Working on game: Marrbles (Currently stopped).
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

oh yeah^^
haha didn't see that.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Post Reply