编译/链接时有多个定义

时间:2012-12-26 10:39:35

标签: c

  

可能重复:
  Repeated Multiple Definition Errors from including same header in multiple 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

该项目设置了autotools;我的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

1 个答案:

答案 0 :(得分:1)

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

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

来自Preprocessor #ifndef

  

标准标题可以包含在任何顺序中;每个都可以包括在内   在给定范围内不止一次,没有效果   只包括一次

您可以检查预处理代码中是否会发现数组已定义两次且错误。您可以使用gcc -E

生成预处理代码

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

要避免该问题,您可以在c文件之一中定义数组。并在头文件

中将数组定义为extern

在其中一个C档案中

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

在标题文件中:

extern int const SHIFT_TABLE[1024];