-1

可能的重复:
在多个 cpps 中包含相同的标头导致重复的多个定义错误

编译第三方 src 时遇到以下错误:

.libs/lib_udf_la-udf.o:(.rodata+0x240): multiple definition of `SHIFT_TABLE'
.libs/lib_udf_la-hll.o:(.rodata+0x0): first defined here

该项目是使用自动工具设置的;我的Makefile.ag参考如下:

SOURCES = hll.c udf.c udf.h 
  • hll.c 引用 hll.h
  • udf.c 引用 hll.h
  • hll.h 有一些const这样的:
  • hll.h#ifndef HLL_H ... #endif可以避免双重定义

    int const SHIFT_TABLE[1024] = {...}

我不明白为什么我要打多个定义,我猜它与链接步骤有关,但自从我涉足 C 以来已经有很长时间了。

这是 cc/link 输出供参考:

make[1]: Entering directory `/home/mping/workspace/monetdb/MonetDB-11.13.5/sql/backends/monet5/UDF'
/bin/bash ../../../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../../..  -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk  -DLIBUDF  -g -O2   -c -o lib_udf_la-hll.lo `test -f 'hll.c' || echo './'`hll.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I../../../.. -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk -DLIBUDF -g -O2 -c hll.c  -fPIC -DPIC -o .libs/lib_udf_la-hll.o
/bin/bash ../../../../libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I../../../..  -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk  -DLIBUDF  -g -O2   -c -o lib_udf_la-udf.lo `test -f 'udf.c' || echo './'`udf.c
libtool: compile:  gcc -DHAVE_CONFIG_H -I. -I../../../.. -I. -I.. -I./.. -I../../../include -I./../../../include -I../../../common -I./../../../common -I../../../storage -I./../../../storage -I../../../server -I./../../../server -I../../../../monetdb5/modules/atoms -I./../../../../monetdb5/modules/atoms -I../../../../monetdb5/modules/kernel -I./../../../../monetdb5/modules/kernel -I../../../../monetdb5/mal -I./../../../../monetdb5/mal -I../../../../monetdb5/modules/mal -I./../../../../monetdb5/modules/mal -I../../../../monetdb5/optimizer -I./../../../../monetdb5/optimizer -I../../../../clients/mapilib -I./../../../../clients/mapilib -I../../../../common/options -I./../../../../common/options -I../../../../common/stream -I./../../../../common/stream -I../../../../gdk -I./../../../../gdk -DLIBUDF -g -O2 -c udf.c  -fPIC -DPIC -o .libs/lib_udf_la-udf.o
/bin/bash ../../../../libtool --tag=CC   --mode=link gcc -DLIBUDF  -g -O2  -module -avoid-version  -o lib_udf.la -rpath /usr/local/lib/monetdb5 lib_udf_la-hll.lo lib_udf_la-udf.lo  ../../../../monetdb5/tools/libmonetdb5.la ../../../../gdk/libbat.la 
libtool: link: gcc -shared  -fPIC -DPIC  .libs/lib_udf_la-hll.o .libs/lib_udf_la-udf.o   -Wl,-rpath -Wl,/home/mping/workspace/monetdb/MonetDB-11.13.5/monetdb5/tools/.libs -Wl,-rpath -Wl,/home/mping/workspace/monetdb/MonetDB-11.13.5/gdk/.libs ../../../../monetdb5/tools/.libs/libmonetdb5.so ../../../../gdk/.libs/libbat.so  -O2   -pthread -Wl,-soname -Wl,lib_udf.so -o .libs/lib_udf.so
4

1 回答 1

1

int const SHIFT_TABLE[1024] = {...}在头文件中定义数组时。然后你c在同一个项目的2个文件中引用头文件这就像在2个文件中定义了两次数组一样c。这就是你的问题的原因。

即使您使用#ifndef它也不会避免您的预处理将定义包含在第二个C文件中

预处理器 #ifndef

标准标题可以以任何顺序包含;每个都可以在给定范围内多次包含,与仅包含一次没有任何不同的效果

您可以检查在预处理代码中,您会发现该数组被定义了两次,这是错误的。你可以生成你的预处理代码gcc -E

#ifndef仅当您在不同的头文件中而不是在同一个头文件中检查常量宏时才有效

为避免该问题,您可以在其中一个c文件中定义数组。extern并且您在头文件中定义数组

在其中一个 C 文件中

int const SHIFT_TABLE[1024] = {...};

在头文件中:

extern int const SHIFT_TABLE[1024];
于 2012-12-26T10:44:39.137 回答