我正在研究一个大型项目的一小部分。该项目最初是用C编写的,大约在6年前转换为C ++(我在3周前第一次听说过这个项目......)
一切都很好。我遇到的错误来自链接器:
libBlah.so.0.0: undefined reference to `Extent::structArray'
collect2: error: ld returned 1 exit status
首先尝试链接的任何可执行文件都会发生错误;有一些可执行文件,每个可执行文件都取决于Extent
类和structArray
数据成员。在尝试(和失败)链接其他可执行文件之前,它成功链接整个libBlah
库。
在Extent.hpp
中,在public
类声明的Extent
部分中,structArray
被声明(并且被hackily初始化):
struct structThing
{
const char *name;
int compress_flag;
bool (*func1)( byte*, int32, ByteArray&, int );
bool (*func2)( byte*, byte*, int32, int32& );
};
// This isn't actually a magic number
static const int num_things = 7;
static structThing structArray[ num_things ];
struct structArray_init
{
structArray_init()
{
structThing init[] =
{
{ "none", 0, NULL, NULL },
{ "thingA", 1, funca1, funca2 },
{ "thingB", 2, funcb1, funcb2 },
{ "thingC", 4, funcc1, funcc2 },
{ "thingD", 8, funcd1, funcd2 },
{ "thingE", 16, funce1, funce2 },
{ "thingF", 32, funcf1, funcf2 }
};
for( int i = 0 ; i < num_things ; ++i )
{
structArray[i] = init[i];
}
}
};
static structArray_init thingy_init;
所有12个函数(funca1到funcf2)都是Extent
的静态函数,稍后在标题的公共部分中声明。
构建由CMake管理。基本上,在CMake中,每个单独的依赖程序都被赋予整个libBlah
库作为依赖项。我试过玩连接顺序,但没有用。
在此链接器错误之前,由于在structArray
的非静态函数中初始化Extent
而导致编译错误,这显然存在问题。
答案 0 :(得分:1)
我认为您的问题可能是您忘记在CPP文件中实际定义static
成员,如下所示:(在Extent.cpp
)
Extent::structThing Extent::structArray [Extent::num_things];
Extent::structArray_init Extent::thingy_init;