多重定义[...]首次在此处定义

5

我正在编写一个H264解析器(非面向对象),但我试图使用一些模块来更好地组织这些内容。

我遇到了以下问题:

`CMakeFiles/server.dir/mainclass.cpp.o:(.bss+0x0): multiple definition of `I_Macroblock_Modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x0): first defined here  
`CMakeFiles/server.dir/mainclass.cpp.o:(.bss+0x300): multiple definition of `P_and_SP_macroblock_modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x300): first defined here  
`CMakeFiles/server.dir/mainclass.cpp.o:(.bss+0x680): multiple definition of `B_Macroblock_Modes' 
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x680): first defined here  
`CMakeFiles/server.dir/thRunAppl.cpp.o:(.bss+0x0): multiple definition of `I_Macroblock_Modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x0): first defined here  
`CMakeFiles/server.dir/thRunAppl.cpp.o:(.bss+0x300): multiple definition of `P_and_SP_macroblock_modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x300): first defined here  
`CMakeFiles/server.dir/thRunAppl.cpp.o:(.bss+0x680): multiple definition of `B_Macroblock_Modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x680): first defined here  
`CMakeFiles/server.dir/h.264parser.cpp.o:(.bss+0x0): multiple definition of `I_Macroblock_Modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x0): first defined here  
`CMakeFiles/server.dir/h.264parser.cpp.o:(.bss+0x300): multiple definition of `P_and_SP_macroblock_modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x300): first defined here  
`CMakeFiles/server.dir/h.264parser.cpp.o:(.bss+0x680): multiple definition of `B_Macroblock_Modes'  
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x680): first defined here  
`CMakeFiles/server.dir/moc_mainclass.cxx.o:(.bss+0x0): multiple definition of `I_Macroblock_Modes'
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x0): first defined here
`CMakeFiles/server.dir/moc_mainclass.cxx.o:(.bss+0x300): multiple definition of `P_and_SP_macroblock_modes'
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x300): first defined here
`CMakeFiles/server.dir/moc_mainclass.cxx.o:(.bss+0x680): multiple definition of `B_Macroblock_Modes'
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x680): first defined here
`CMakeFiles/server.dir/moc_thRunAppl.cxx.o:(.bss+0x0): multiple definition of `I_Macroblock_Modes'
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x0): first defined here
`CMakeFiles/server.dir/moc_thRunAppl.cxx.o:(.bss+0x300): multiple definition of `P_and_SP_macroblock_modes'
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x300): first defined here
`CMakeFiles/server.dir/moc_thRunAppl.cxx.o:(.bss+0x680): multiple definition of `B_Macroblock_Modes'
`CMakeFiles/server.dir/main.cpp.o:(.bss+0x680): first defined here
`CMakeFiles/server.dir/h.264parser.cpp.o: In function `MbPartPredMode(int, int)':  
`h.264parser.cpp:(.text+0xf0): undefined reference to `B_macroblockmodes(int, int)'  
`h.264parser.cpp:(.text+0x104): undefined reference to `B_macroblockmodes(int, int)'  
`h.264parser.cpp:(.text+0x111): undefined reference to `I_macroblockmodes(int, int)'  
`h.264parser.cpp:(.text+0x124): undefined reference to `P_and_SP_macroblockmodes(int, int)'  
`h.264parser.cpp:(.text+0x134): undefined reference to `P_and_SP_macroblockmodes(int, int)' 

我的情景是:

macroblocktypes.h. 只有定义

macroblocktables.h:

#include "macroblocktypes.h"

int I_Macroblock_Modes[27][7];
int P_and_SP_macroblock_modes[32][7];
int B_Macroblock_Modes[50][7];

int I_macroblockmodes(int line, int column);
int P_and_SP_macroblockmodes(int line, int column);
int B_macroblockmodes(int line, int column);

macroblocktables.cpp文件中,每个数组和函数都在.h文件中定义,如下所示。

#include "macroblocktables.h"
int I_macroblock_modes[27][7] ={
{0, I_4x4, 0, Intra_4x4, NA, NA, NA},
//If this line was to be commented out, the MbPartPredMode macro would have to be changed
//since it relies on the linear rise of the value in the first column.
//{0,   I_NxN,          1,  Intra_8x8,   NA, NA, NA},
{1, I_16x16_0_0_0, NA, Intra_16x16, 0, 0, 0},
{2, I_16x16_1_0_0, NA, Intra_16x16, 1, 0, 0},
{3, I_16x16_2_0_0, NA, Intra_16x16, 2, 0, 0},
{4, I_16x16_3_0_0, NA, Intra_16x16, 3, 0, 0},
{5, I_16x16_0_1_0, NA, Intra_16x16, 0, 1, 0},
{6, I_16x16_1_1_0, NA, Intra_16x16, 1, 1, 0},
{7, I_16x16_2_1_0, NA, Intra_16x16, 2, 1, 0},
{8, I_16x16_3_1_0, NA, Intra_16x16, 3, 1, 0},
{9, I_16x16_0_2_0, NA, Intra_16x16, 0, 2, 0},
{10, I_16x16_1_2_0, NA, Intra_16x16, 1, 2, 0},
{11, I_16x16_2_2_0, NA, Intra_16x16, 2, 2, 0},
{12, I_16x16_3_2_0, NA, Intra_16x16, 3, 2, 0},
{13, I_16x16_0_0_1, NA, Intra_16x16, 0, 0, 15},
{14, I_16x16_1_0_1, NA, Intra_16x16, 1, 0, 15},
{15, I_16x16_2_0_1, NA, Intra_16x16, 2, 0, 15},
{16, I_16x16_3_0_1, NA, Intra_16x16, 3, 0, 15},
{17, I_16x16_0_1_1, NA, Intra_16x16, 0, 1, 15},
{18, I_16x16_1_1_1, NA, Intra_16x16, 1, 1, 15},
{19, I_16x16_2_1_1, NA, Intra_16x16, 2, 1, 15},
{20, I_16x16_3_1_1, NA, Intra_16x16, 3, 1, 15},
{21, I_16x16_0_2_1, NA, Intra_16x16, 0, 2, 15},
{22, I_16x16_1_2_1, NA, Intra_16x16, 1, 2, 15},
{23, I_16x16_2_2_1, NA, Intra_16x16, 2, 2, 15},
{24, I_16x16_3_2_1, NA, Intra_16x16, 3, 2, 15},
{25, I_PCM, NA, NA, NA, NA, NA}};

int P_and_SP_macroblock_modes[32][7] = {
{0, P_L0_16x16, 1, Pred_L0, NA, 16, 16},
{1, P_L0_L0_16x8, 2, Pred_L0, Pred_L0, 16, 8},
{2, P_L0_L0_8x16, 2, Pred_L0, Pred_L0, 8, 16},
{3, P_8x8, 4, NA, NA, 8, 8},
{4, P_8x8ref0, 4, NA, NA, 8, 8},
{0, I_4x4, 0, Intra_4x4, NA, NA, NA},
{1, I_16x16_0_0_0, NA, Intra_16x16, 0, 0, 0},
{2, I_16x16_1_0_0, NA, Intra_16x16, 1, 0, 0},
{3, I_16x16_2_0_0, NA, Intra_16x16, 2, 0, 0},
{4, I_16x16_3_0_0, NA, Intra_16x16, 3, 0, 0},
{5, I_16x16_0_1_0, NA, Intra_16x16, 0, 1, 0},
{6, I_16x16_1_1_0, NA, Intra_16x16, 1, 1, 0},
{7, I_16x16_2_1_0, NA, Intra_16x16, 2, 1, 0},
{8, I_16x16_3_1_0, NA, Intra_16x16, 3, 1, 0},
{9, I_16x16_0_2_0, NA, Intra_16x16, 0, 2, 0},
{10, I_16x16_1_2_0, NA, Intra_16x16, 1, 2, 0},
{11, I_16x16_2_2_0, NA, Intra_16x16, 2, 2, 0},
{12, I_16x16_3_2_0, NA, Intra_16x16, 3, 2, 0},
{13, I_16x16_0_0_1, NA, Intra_16x16, 0, 0, 15},
{14, I_16x16_1_0_1, NA, Intra_16x16, 1, 0, 15},
{15, I_16x16_2_0_1, NA, Intra_16x16, 2, 0, 15},
{16, I_16x16_3_0_1, NA, Intra_16x16, 3, 0, 15},
{17, I_16x16_0_1_1, NA, Intra_16x16, 0, 1, 15},
{18, I_16x16_1_1_1, NA, Intra_16x16, 1, 1, 15},
{19, I_16x16_2_1_1, NA, Intra_16x16, 2, 1, 15},
{20, I_16x16_3_1_1, NA, Intra_16x16, 3, 1, 15},
{21, I_16x16_0_2_1, NA, Intra_16x16, 0, 2, 15},
{22, I_16x16_1_2_1, NA, Intra_16x16, 1, 2, 15},
{23, I_16x16_2_2_1, NA, Intra_16x16, 2, 2, 15},
{24, I_16x16_3_2_1, NA, Intra_16x16, 3, 2, 15},
{25, I_PCM, NA, NA, NA, NA, NA},
{NA, P_Skip, 1, Pred_L0, NA, 16, 16}};

int B_macroblock_modes[50][7] = {
{0, B_Direct_16x16, NA, Direct, NA, 8, 8},
{1, B_L0_16x16, 1, Pred_L0, NA, 16, 16},
{2, B_L1_16x16, 1, Pred_L1, NA, 16, 16},
{3, B_Bi_16x16, 1, BiPred, NA, 16, 16},
{4, B_L0_L0_16x8, 2, Pred_L0, Pred_L0, 16, 8},
{5, B_L0_L0_8x16, 2, Pred_L0, Pred_L0, 8, 16},
{6, B_L1_L1_16x8, 2, Pred_L1, Pred_L1, 16, 8},
{7, B_L1_L1_8x16, 2, Pred_L1, Pred_L1, 8, 16},
{8, B_L0_L1_16x8, 2, Pred_L0, Pred_L1, 16, 8},
{9, B_L0_L1_8x16, 2, Pred_L0, Pred_L1, 8, 16},
{10, B_L1_L0_16x8, 2, Pred_L1, Pred_L0, 16, 8},
{11, B_L1_L0_8x16, 2, Pred_L1, Pred_L0, 8, 16},
{12, B_L0_Bi_16x8, 2, Pred_L0, BiPred, 16, 8},
{13, B_L0_Bi_8x16, 2, Pred_L0, BiPred, 8, 16},
{14, B_L1_Bi_16x8, 2, Pred_L1, BiPred, 16, 8},
{15, B_L1_Bi_8x16, 2, Pred_L1, BiPred, 8, 16},
{16, B_Bi_L0_16x8, 2, BiPred, Pred_L0, 16, 8},
{17, B_Bi_L0_8x16, 2, BiPred, Pred_L0, 8, 16},
{18, B_Bi_L1_16x8, 2, BiPred, Pred_L1, 16, 8},
{19, B_Bi_L1_8x16, 2, BiPred, Pred_L1, 8, 16},
{20, B_Bi_Bi_16x8, 2, BiPred, BiPred, 16, 8},
{21, B_Bi_Bi_8x16, 2, BiPred, BiPred, 8, 16},
{22, B_8x8, 4, NA, NA, 8, 8},
{0, I_4x4, 0, Intra_4x4, NA, NA, NA},
{1, I_16x16_0_0_0, NA, Intra_16x16, 0, 0, 0},
{2, I_16x16_1_0_0, NA, Intra_16x16, 1, 0, 0},
{3, I_16x16_2_0_0, NA, Intra_16x16, 2, 0, 0},
{4, I_16x16_3_0_0, NA, Intra_16x16, 3, 0, 0},
{5, I_16x16_0_1_0, NA, Intra_16x16, 0, 1, 0},
{6, I_16x16_1_1_0, NA, Intra_16x16, 1, 1, 0},
{7, I_16x16_2_1_0, NA, Intra_16x16, 2, 1, 0},
{8, I_16x16_3_1_0, NA, Intra_16x16, 3, 1, 0},
{9, I_16x16_0_2_0, NA, Intra_16x16, 0, 2, 0},
{10, I_16x16_1_2_0, NA, Intra_16x16, 1, 2, 0},
{11, I_16x16_2_2_0, NA, Intra_16x16, 2, 2, 0},
{12, I_16x16_3_2_0, NA, Intra_16x16, 3, 2, 0},
{13, I_16x16_0_0_1, NA, Intra_16x16, 0, 0, 15},
{14, I_16x16_1_0_1, NA, Intra_16x16, 1, 0, 15},
{15, I_16x16_2_0_1, NA, Intra_16x16, 2, 0, 15},
{16, I_16x16_3_0_1, NA, Intra_16x16, 3, 0, 15},
{17, I_16x16_0_1_1, NA, Intra_16x16, 0, 1, 15},
{18, I_16x16_1_1_1, NA, Intra_16x16, 1, 1, 15},
{19, I_16x16_2_1_1, NA, Intra_16x16, 2, 1, 15},
{20, I_16x16_3_1_1, NA, Intra_16x16, 3, 1, 15},
{21, I_16x16_0_2_1, NA, Intra_16x16, 0, 2, 15},
{22, I_16x16_1_2_1, NA, Intra_16x16, 1, 2, 15},
{23, I_16x16_2_2_1, NA, Intra_16x16, 2, 2, 15},
{24, I_16x16_3_2_1, NA, Intra_16x16, 3, 2, 15},
{25, I_PCM, NA, NA, NA, NA, NA},
{NA, B_Skip, NA, Direct, NA, 8, 8}};

int I_macroblockmodes(int line, int column) {
if (line < 27 && column < 7)
    return I_macroblock_modes[line][column];
else {
    if (line >= 27) {
        return -10000; //line is bigger than the array size
    } else {
        return -20000; //column is bigger than the array size
    }
}}
//Inter prediction slices - Macroblock types
//Defined strictly by norm, page 121.

//(Table 7-13 Macroblock type values 0 to 4 for P and SP slices)
/*
First column:   mb_type
Second column:  Name of mb_type
Third column:   NumMbPart( mb_type )
Fourth column:  MbPartPredMode( mb_type, 0 )
Fifth column:   MbPartPredMode( mb_type, 1 )
Sixth column:   MbPartWidth( mb_type )
Seventh column: MbPartHeight( mb_type )
*/
int P_and_SP_macroblockmodes(int line, int column){
 if (line < 32 && column < 7)
    return P_and_SP_macroblock_modes[line][column];
else {
    if (line >= 32) {
        return -10000; //line is bigger than the array size
    } else {
        return -20000; //column is bigger than the array size
    }
}}



//p94. Table 7-14 – Macroblock type values 0 to 22 for B slices
/*
First column:   mb_type
Second column:  Name of mb_type
Third column:   NumMbPart( mb_type )
Fourth column:  MbPartPredMode( mb_type, 0 )
Fifth column:   MbPartPredMode( mb_type, 1 )
Sixth column:   MbPartWidth( mb_type )
Seventh column: MbPartHeight( mb_type )
 */
int B_macroblockmodes(int line, int column){
 if (line < 50 && column < 7)
    return B_macroblock_modes[line][column];
else {
    if (line >= 50) {
        return -10000; //line is bigger than the array size
    } else {
        return -20000; //column is bigger than the array size
    }
}}

最后,我的h.264文件(.cpp和.h)。在.h中,我包含了macroblocktable.h和macroblocktype.h,在h.264.cpp中只包含了h.264.h。
我想在h.264.cpp中使用来自macroblocktable.cpp的函数。
提前感谢。
2个回答

11

你必须仅将声明放在.h文件中,而不是定义,否则可能会出现链接器错误。这些

int I_Macroblock_Modes[27][7];
int P_and_SP_macroblock_modes[32][7];
int B_Macroblock_Modes[50][7];

这些是定义,尽管看起来不太直观。将其更改为

extern int I_Macroblock_Modes[27][7];
extern int P_and_SP_macroblock_modes[32][7];
extern int B_Macroblock_Modes[50][7];

你应该没问题了


谢谢你的回答,但我不想在外部访问数组,而是要将它们封装在macroblocktable.cpp中,并从那个.cpp文件调用函数。我从.h文件中删除了声明,'multiple definition'错误停止了;但是当我尝试从h.265.cpp中调用这些函数时,仍然出现'h.264parser.cpp:(.text+0x104): undefined reference to `B_macroblockmodes(int, int)''的错误。=( - Pedro Dusso
@Pmdusso:你不需要从任何地方删除任何声明。按照以下步骤操作:1)在.h文件中,将extern关键字添加到数组声明中。保留cpp文件中的数组定义不变。2)在cpp文件中添加B_macroblockmodes(int, int)函数的定义(你忘记编写函数体了,因此出现错误)。 - Armen Tsirunyan
如果你的意思是在macroblocktable.cpp中编写B_macroblockmodes(int, int),那我已经写了!你可以在问题中看到...而且我用extern在macroblocktable.h中写回数组的定义,谢谢! - Pedro Dusso
@Pmdusso:嗯...不,你还没有。那个函数的主体在哪里? - Armen Tsirunyan
啊...抱歉...我只是为了节省空间而写了int I_macroblockmodes(int line, int column)...但是所有三个都在那里,我会编辑它。就这样,它们现在都在那里了。 - Pedro Dusso

6

这不仅仅是“定义”问题。您正在其中声明变量,因此每次包含它时,您都会重新声明全局变量。

如果您想要从其他.c/.cpp文件中访问,则应在macroblocktables.h头文件中放置:

//resides in macroblocktables.cpp
extern int I_Macroblock_Modes[27][7];
extern int P_and_SP_macroblock_modes[32][7];
extern int B_Macroblock_Modes[50][7];

// the rest of it

因为这些int数组是在macroblocktables.cpp中定义的,是吗?

在C/C++中,每次你做像这样的事情:

int myarray[4];
char mychar;

更普遍地说,
type var_name;

您告诉编译器:请为我在堆栈上分配一个名为X且类型为T的变量。当您#include header.h时,在最基本的解释中,您正在告诉预处理器:打开header.h,复制所有内容并粘贴到此处。然后将预处理后的源文件提供给编译器,并且每次看到type var_name时,它都会执行我上面解释的操作。抱歉如果这些信息过多,但我只是想写下来:P

抱歉没有表达清楚,但只包含定义的文件是macroblocktypes.h。我知道macroblocktable.h中声明了变量。 - Pedro Dusso
@hexa 我应该将这些函数也声明为extern吗?将数组声明为extern解决了多重定义问题,但我现在不知道如何处理这些函数! - Pedro Dusso
函数原型是正确的 :) http://zh.wikipedia.org/wiki/函数原型 - Vinicius Kamakura
是的,但仍然从h.264.cpp中出现“未定义的引用”错误... =/ - Pedro Dusso
你在h264.cpp文件中包含了.h文件吗?并且为数组添加了extern关键字吗? - Vinicius Kamakura

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接