Advertisement

"error LNK2001: unresolved external symbol" for static methods

Started by January 07, 2016 12:03 PM
3 comments, last by vstrakh 8 years, 11 months ago

Hi I have a weird error.

Here's my code with a bunch of unimportant stuff cut out:


#pragma once

struct PFNode
{
	int gCost;
	int hCost;
	int fCost;
	int x, z;
	int id;
	PFNode* parent;
	bool blocked;
	bool checked;
};

extern PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE];


static void InitPathFinder(World* world)
{
	worldW = world->getTilesWide();
	worldL = world->getTilesLong();

	for (int z = 0; z < worldL; ++z)
	for (int x = 0; x < worldW; ++x)
	{
		map[x][z].x = x;
		map[x][z].z = z;
		map[x][z].id = x + z * worldW;
		map[x][z].blocked = world->getTile(x, z)->isBlocked;
		map[x][z].checked = false;
	}
}

So I get errors saying "1>EM_Component_Mover.obj : error LNK2001: unresolved external symbol "struct PFNode (* map)[256]" (?map@@3PAY0BAA@UPFNode@@A)

1>Game.obj : error LNK2001: unresolved external symbol "struct PFNode (* map)[256]" (?map@@3PAY0BAA@UPFNode@@A)"
I have this file included in 2 other files (one being EM_Component_Mover.h) so I'm guessing structs or static methods don't work how I think they do.
Any help is greatly appreciated.
If you define a variable (like in this case 'map') as extern you need to put the definition into exactly one implementation file. Currently you tell the compiler "there is a variable called 'map' of such-and-such type somewhere else" but the linker cannot find it anywhere because you never put it anywhere.
Advertisement


so I'm guessing structs or static methods don't work how I think they do.

There is one struct, but there's no static methods.

There's function declared static. It's not static method, and its body will be duplicated/inlined in every compilation unit.

As for linker error - you've declared map[][] array, but you didn't define it.

One of .cpp files must have PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE]; line, without "extern" keyword.

Having "extern" means "there's array, defined in one of object files. Use it from there, do not create instantiate of it".


so I'm guessing structs or static methods don't work how I think they do.

There is one struct, but there's no static methods.

There's function declared static. It's not static method, and its body will be duplicated/inlined in every compilation unit.

As for linker error - you've declared map[][] array, but you didn't define it.

One of .cpp files must have PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE]; line, without "extern" keyword.

Having "extern" means "there's array, defined in one of object files. Use it from there, do not create instantiate of it".


so I'm guessing structs or static methods don't work how I think they do.

There is one struct, but there's no static methods.

There's function declared static. It's not static method, and its body will be duplicated/inlined in every compilation unit.

As for linker error - you've declared map[][] array, but you didn't define it.

One of .cpp files must have PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE]; line, without "extern" keyword.

Having "extern" means "there's array, defined in one of object files. Use it from there, do not create instantiate of it".

map array is in cpp, and thats not the error im getting. "static PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE];"


"static PFNode map[LARGEST_MAP_SIZE][LARGEST_MAP_SIZE];"

Do not add "static" there. Static sets internal linkage for map[], so linker won't see that variable available for linking.

This topic is closed to new replies.

Advertisement