Linker Errors

Why does the linker always seem to stall any attempt at progress at every possible moment! I have an extern Three-dimensional array and I (after review of course) cannot find what is causing the problem.

~ I have Remove Completely irrelevant code ~

Map.hpp
1
2
3
4
5
6
7
8
9
#pragma once

#define CHUNK_SIZE 20

#define WORLD_X 300
#define WORLD_Y 300
#define WORLD_Z 100

extern int World[WORLD_X][WORLD_Y][WORLD_Z];


Map.cpp
 
Not Included


RenderBlock.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once

#include "Window.hpp"

#define BLOCK_SIZE 10

class Render{

   int RenderBlock(double, double, double, int, int, bool, int);

	public:

   int GlPresets(void);
   int RenderChunk(int,int,int,int,int, bool);

	Window Render_Window;
};


RenderBlock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int Render::RenderChunk(int posX, int posY, int posZ, int ViewPointX, int ViewPointY, bool WireFrame )
{
	int CurrentBlock;

	for(LoopCount x = 0; x <= CHUNK_SIZE + 1; x++)
   {
     for(LoopCount y = 0; y <= CHUNK_SIZE + 1; y++)
    {
      for(LoopCount z = 0; z <= CHUNK_SIZE + 1; z++)
     {
		  CurrentBlock = World[x][y][z];
	      RenderBlock(x + CHUNK_SIZE*posX, y + CHUNK_SIZE*posX , z + CHUNK_SIZE*posX , ViewPointX, ViewPointY, WireFrame, CurrentBlock);
		  
	 } // Z
	   
    } // Y

   } // X

	return 0;
}


Why can I not use this external array in other functions/files?
Note* The includes are all correct, that is not the error.

The errors are:


1>RenderBlock.obj : error LNK2001: unresolved external symbol "int (* World)[300][100]" (?World@@3PAY1BCM@GE@HA)
1>SomeOtherRandomFiles.obj : error LNK2001: unresolved external symbol "int (* World)[300][100]" (?World@@3PAY1BCM@GE@HA)
Last edited on
I would guess it's because you don't actually define the array anywhere. It should be defined in the completely irrelevant "Map.cpp" you didn't post.

extern int World[WORLD_X][WORLD_Y][WORLD_Z]; is only a declaration.
extern is used to declare a variable it binds a variable which is already defined or later.

You must define a variable before building your application.

If you are building a library it is not necessary to define that variable, When you are going to use that library in any other application you must define that variable.
The variable has been defined:
1
2
3
4
5
6
7
8
9
10
11

typedef short int LoopCount;

	for(LoopCount Xloop = 0; Xloop < WORLD_X; Xloop++){
		for(LoopCount Yloop = 0; Xloop < WORLD_Y; Yloop++){
			for(LoopCount Zloop = 0; Xloop < WORLD_Z; Zloop++){
	World[WORLD_X][WORLD_Y][WORLD_Z] = 1;
			}
		}
	}


This code runs directly after the array was declared.
What would cause the linker to give an unresolved external.
What would cause the linker to give an unresolved external.

The variable not being defined. It is not defined there, it is only used. How can you use it without it being defined? You can't. Thus, the linker error.

In Map.cpp you need the following line somewhere at global scope:

int World[WORLD_X][WORLD_Y][WORLD_Z];
Alright that makes sense, however how would I go about the problem of using the same array in multiple files? I would get an already defined error from the linker.

WorldGenerator.obj : error LNK2005: "int (* World)[300][100]" (?World@@3PAY1BCM@GE@HA) already defined in RenderBlock.obj

To be exact...

On a side note is there any problem with #pragma once at the start of cpp's?
Last edited on
Alright that makes sense, however how would I go about the problem of using the same array in multiple files?



There is no problem. You simply #include the header file with it's declaration and there are no redefinition issues (because the variable is not defined in the header.)


On a side note is there any problem with #pragma once at the start of cpp's?



Since you shouldn't #include cpp files, there is no reason to.
Topic archived. No new replies allowed.