标头中的结构数组出现链接器错误
Linker error with array of structs in header
我正在处理一个大型项目的一小部分。该项目最初是用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
中,在Extent
类声明的public
部分中,structArray
被声明(并被巧妙地初始化),因此:
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
库作为依赖。我试着摆弄链接顺序,但没有用。
在此链接器错误之前,我在Extent
的非静态函数中初始化structArray
时出现编译错误,这显然是有问题的。
我认为您的问题可能是您忘记在CPP文件中实际定义static
成员,如下所示:(在Extent.cpp
中)
Extent::structThing Extent::structArray [Extent::num_things];
Extent::structArray_init Extent::thingy_init;
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误