2012-12-26 32 views
-2

可能重複:
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的東西,以避免雙重defs

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

我不明白爲什麼我打多個定義,我猜它與鏈接步驟做的,但它是一個漫長的時間,因爲我與C

涉足

這裏的cc /鏈接輸出供參考:

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 

回答

0

當您在頭文件中定義數組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]; 
+0

編譯步驟做的工作,但是在運行程序時會出現錯誤。不幸的是,由於它是第三方應用程序,我無法獲得關於錯誤的更多細節。 –

+0

嘗試提供一個能夠重現問題的小代碼,以幫助您 – MOHAMED