extern and unresolved external : I'm lost


Hello all

I'm facing a very puzzling issue :

an array of const std::string values is never seen by the linker :

source1.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <string>

extern const std::string foo[5];

void main(int n, char *v[])
{
	for (int i=0;i<_countof(foo);i++)
	{
		printf("\"%s\"\n",foo[i].c_str());
	}
}


source2.cpp

1
2
3
#include <string>

const std::string foo[5]={"bar","1","2"};


expected result:
"bar"
"1"
"2"
""
""

but that does not compile : liker chokes on

1>Linking...
1>source1.obj : error LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const * const foo" (?foo@@3QBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B)
1>D:\Dev\underCvs\addins\syncagx\trunk\Debug\test.exe : fatal error LNK1120: 1 unresolved externals

(Of course, both files are compiled and linked)

Can anyone help ?
Thanks !
Try using the extern keyword in both source1.cpp and source2.cpp and see what happens.
You are not linking both source1.obj and source2.obj into the executable.
Topic archived. No new replies allowed.