-1

Possible Duplicate:
Repeated Multiple Definition Errors from including same header in multiple cpps

I'm hitting the following error when compiling a third-party 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

The project is set up with autotools; my Makefile.ag references the following:

SOURCES = hll.c udf.c udf.h 
  • hll.c references hll.h
  • udf.c references hll.h
  • hll.h has some const like this:
  • hll.h has the #ifndef HLL_H ... #endif thing to avoid double defs

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

I don't understand why I'm hitting multiple definitions, I'm guessing it has to do with the link step, but it is a long time since I dabbled with C.

Here's the cc/link output for reference:

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
Community
  • 1
  • 1
Miguel Ping
  • 18,082
  • 23
  • 88
  • 136

1 Answers1

1

When you define array int const SHIFT_TABLE[1024] = {...} in the header file. and then you make reference to the header file in 2 c file in the same project It's like of defining the array twice in the 2 c files. and that's the cause of your problem.

Even If you use #ifndef that's will not avoid your preprocess to include the definition in the second C file

From Preprocessor #ifndef:

Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once

You can check that in the preprocess code you will find that the array is defined twice and that's wrong. you can generate your preprocess code with gcc -E

the #ifndef works only when you check the constant macro in different header files and not in the same header file

To avoid that problem you can define your array in one of the c file. And you define the array as extern in the header file

In one of the C files:

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

In the header file:

extern int const SHIFT_TABLE[1024];
Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • The compilation step does work, but when running the program it gives an error. Unfortunately, I cannot get any more details about the error, since it's a third party app. – Miguel Ping Dec 26 '12 at 11:08
  • Try to provide a small code which reproduce your problem in order to help you – MOHAMED Dec 26 '12 at 11:28