Game Engine Architecture

(Ben Green) #1

3.2. Data, Code, and Memory in C/C++ 109


Unresolved Reference???

???

Multiply-Defined Symbol

???

foo.cpp

U32 gGlobalA ;
U32 gGlobalB ;
void f ()
{
// ...
gGlobalC = 5.3f;
gGlobalD = -2;
// ...}

extern U 32 gGlobalC ;

bar.cpp

F32 gGlobalC ;
void g ()
{
// ... U32 a = gGlobalA ;
// ...
f();
// ...
gGlobalB = 0;
}

extern U 32 gGlobalA ;
extern U 32 gGlobalB ;
extern void f ();

spam.cpp
U32 gGlobalA ;
void h()
{
// ...
}

Figure 3.9. The two most common linker errors.


foo.cpp

U32 gGlobalA ;
U32 gGlobalB ;
void f()
{
// ...
gGlobalC = 5.3f;
// ...}

extern U 32 gGlobalC ;

bar.cpp

F32 gGlobalC ;
void g ()
{
// ...
U32 a = gGlobalA ;
// ... f();
// ...
gGlobalB = 0;
}

extern U 32 gGlobalA ;
extern U 32 gGlobalB ;
extern void f ();

Figure 3.7. Unresolved external references in two translation units.


foo.cpp

U32 gGlobalA ;
U32 gGlobalB ;
void f ()
{
// ...
gGlobalC = 5.3f;
// ...}

extern U 32 gGlobalC ;

bar.cpp

F32 gGlobalC ;
void g()
{
// ...
U32 a = gGlobalA ;
// ... f();
// ...
gGlobalB = 0;
}

extern U 32 gGlobalA ;
extern U 32 gGlobalB ;
extern void f ();

Figure 3.8. Fully resolved external references after successful linking.

Free download pdf