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 to `displaySphere_001()'"

#130893 - The_Perfection - Sat Jun 09, 2007 1:49 am

I've stubled across a problem while trying to make a function to display 3D objects. While I think that the context of the function has nothing to do with the problem, I am still having difficulties getting it to work. I'll try to post the minimal code.
In main.cpp:

Code:
#include <nds.h>

#include <stdio.h>

#include "Sphere_001.h"

// The rest of setting up the hardware
// The main game loop

int DrawGLScene()
{
   glColor(RGB5(25, 25, 25));

// Problematic line of code   
   displaySphere_001();
   
   glEnd();
   
   return TRUE;
}


In Sphere_001.h:

Code:
extern const float Sphere_001Points[12][3];

extern const unsigned short Sphere_001Triangles[20][3];

extern void displaySphere_001(void);


In Sphere_001.c:

Code:
#include <nds.h>
#include "Sphere_001.h"

const float Sphere_001Points[12][3] =
{
    // Point Data
};

const unsigned short Sphere_001Triangles[20][3] =
{
    // Triangle Data
};

void displaySphere_001(void)
{
    // Function Internals
}


The code compiles, it's at the linking step that things get messed up. I don't understand why; the proper files are included and I've tried everything I could think of to make the function visible and it still wont show up. If I brute force display the triangles it works fine meaning the symbols in the file are recognized by the main file, but not the function. Why?

#130898 - wintermute - Sat Jun 09, 2007 2:37 am

the C++ compiler mangles names to include information about the data types being referenced. This mangling won't take place in C files, you need to either compile everything as C++ or tell the compiler which functions and data are compiled as C.

Using extern "C" in In Sphere_001.h, like this is one method.

Code:


#ifdef __cplusplus
extern "C" {
#endif

extern const float Sphere_001Points[12][3];

extern const unsigned short Sphere_001Triangles[20][3];

extern void displaySphere_001(void);

#ifdef __cplusplus
}
#endif


_________________
devkitPro - professional toolchains at amateur prices
devkitPro IRC support
Personal Blog

#130959 - The_Perfection - Sat Jun 09, 2007 5:50 pm

That would explain it. Tested using extern "C", didn't work. Tested changing the file type, didn't work.

Then I ran make clean. Both techniques worked perfectly. Thank you for your help.

As a secondary question, are there any advantages of using .cpp files over .c files or vise versa?

#130972 - tepples - Sat Jun 09, 2007 8:06 pm

The_Perfection wrote:
As a secondary question, are there any advantages of using .cpp files over .c files or vise versa?

.cpp files let you use C++ constructs that aren't in C, such as inheritance, methods, and templates, while .c files let you use C99 constructs that aren't in C++, such as designated initializers. The rule for casting (void *) types between the two languages also differs.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.