我正在编写 CCITT Group 4 编码器,看来我的链接器有问题。
我有一些原始二进制数据存储为static const
类成员(Fakskod.h
):
class Fakskod {
public:
// ...
static const unsigned char codesTerminateWhite[64][3];
static const unsigned char codesTerminateBlack[64][3];
static const unsigned char codesMakeUpWhite[40][3];
static const unsigned char codesMakeUpBlack[40][3];
private:
//...
} ;
我不得不在别处初始化它们,因为大括号封闭的初始化器不能在类定义中使用。更不用说标题的清晰度了。因此,在另一个文件 ( Fakskod-codes.cpp
) 中,我分配了数据:
const unsigned char codesTerminateBlack[64][3] = { {0x0d, 0xc0, 0xa},
{0x40, 0x0, 0x3}, {0xc0, 0, 0x2}, {0x60, 0, 0x3}, {0x30, 0, 0x4},
{0x20, 0, 0x4}, {0x18, 0, 0x5}, {0x14, 0, 0x6}, {0x10, 0, 0x6},
//... (~200 hex bytes)
} ;
这就是我得到的:
/tmp/ccm0ijoi.o: In function `Fakskod::makeCode(int, fkColor)':
rectangle_test.cpp:(.text+0x19b0): undefined reference to `Fakskod::codesTerminateWhite'
rectangle_test.cpp:(.text+0x1a02): undefined reference to `Fakskod::codesMakeUpWhite'
rectangle_test.cpp:(.text+0x1a7b): undefined reference to `Fakskod::codesTerminateWhite'
rectangle_test.cpp:(.text+0x1b0f): undefined reference to `Fakskod::codesTerminateBlack'
rectangle_test.cpp:(.text+0x1b5e): undefined reference to `Fakskod::codesMakeUpBlack'
rectangle_test.cpp:(.text+0x1bce): undefined reference to `Fakskod::codesTerminateBlack'
collect2: error: ld returned 1 exit status
看来麻烦的根源是这种方法(Fakskod-Horizontal.cpp
):
std::vector<unsigned char> Fakskod::makeCode(int num, fkColor color) {
#define FAKSKOD_MKCD(TERM, MKUP) \
if(num < 64) \
return vectorize( TERM [num]); \
else { \
for(int i=40; i>0; i--) \
if(num/64 == i) { \
code = vectorize( MKUP [i-1]); \
num -= 64 * i; \
} \
return mergeStreams(code, vectorize( TERM [num])); \
}
std::vector<unsigned char> code;
if(color==WHITE) { FAKSKOD_MKCD(codesTerminateWhite, codesMakeUpWhite) }
else { FAKSKOD_MKCD(codesTerminateBlack, codesMakeUpBlack) }
} // Fakskod::makeCode
我确信使用宏一切正常。预处理器 ( g++ -E
) 正确解析它,当我将扩展版本粘贴到我的代码时,消息是相同的。
我也尝试#include
了头文件和Fakskod-codes.cpp
,或将这些文件添加到 g++ 命令。没有帮助。即使我将作业移到标题,也会引发错误。