2016-01-27 97 views
0

我有一個項目,其中包含2個位置的源代碼。 當項目鏈接,它抱怨說,一個功能是雙重定義(這不是):帶有2個源目錄的makefile包含保護失敗

cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src src/Meter.c -o build/x86_64-linux-gnu/obj/Meter.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src src/PaymentHandler.c -o build/x86_64-linux-gnu/obj/PaymentHandler.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src src/PaymentHandlerDaemon.c -o build/x86_64-linux-gnu/obj/PaymentHandlerDaemon.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src src/PrintSocketReceiver.c -o build/x86_64-linux-gnu/obj/PrintSocketReceiver.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src src/SerialPrinter.c -o build/x86_64-linux-gnu/obj/SerialPrinter.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src ../../Bla/trunk/src/CheckSum.c -o build/x86_64-linux-gnu/obj/Bla/CheckSum.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src ../../Bla/trunk/src/CheckSum.c -o build/x86_64-linux-gnu/obj/Bla/Config.o 
cc build/x86_64-linux-gnu/obj/Meter.o build/x86_64-linux-gnu/obj/PaymentHandler.o build/x86_64-linux-gnu/obj/PaymentHandlerDaemon.o build/x86_64-linux-gnu/obj/PrintSocketReceiver.o build/x86_64-linux-gnu/obj/SerialPrinter.o build/x86_64-linux-gnu/obj/Bla/CheckSum.o build/x86_64-linux-gnu/obj/Bla/Config.o -o build/x86_64-linux-gnu/obj/../PaymentHandlerDaemon -lrt -lpthread -lconfig -lm -L../../libSOS/trunk/build/x86_64-linux-gnu -lSOS -L../lib/ABCUtilLib/build/x86_64-linux-gnu -lABCUtil -L../lib/ABCUtilSO/build/x86_64-linux-gnu -lABCMain 
build/x86_64-linux-gnu/obj/Bla/Config.o: In function `CheckSum_Buffer': 
/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/../../Bla/trunk/src/CheckSum.c:34: multiple definition of `CheckSum_Buffer' 
build/x86_64-linux-gnu/obj/Bla/CheckSum.o:/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/../../Bla/trunk/src/CheckSum.c:34: first defined here 
build/x86_64-linux-gnu/obj/Bla/Config.o: In function `CheckSum_Stream': 
/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/../../Bla/trunk/src/CheckSum.c:54: multiple definition of `CheckSum_Stream' 
build/x86_64-linux-gnu/obj/Bla/CheckSum.o:/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/../../Bla/trunk/src/CheckSum.c:54: first defined here 
build/x86_64-linux-gnu/obj/Bla/Config.o: In function `pppfcs16': 
/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/../../Bla/trunk/src/CheckSum.c:125: multiple definition of `pppfcs16' 
build/x86_64-linux-gnu/obj/Bla/CheckSum.o:/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/../../Bla/trunk/src/CheckSum.c:125: first defined here 
build/x86_64-linux-gnu/obj/Meter.o: In function `Meter_MessagePumpProc': 
/home/builder/projects/Applications/PaymentHandlerDaemon/trunk/src/Meter.c:365: undefined reference to `Config_GetHardwareInfo' 
collect2: error: ld returned 1 exit status 
make: *** [build/x86_64-linux-gnu/obj/../PaymentHandlerDaemon] Error 1 

我認爲這是因爲Makefile中有2個不同的食譜來編譯每個目錄和他們不共享預編譯定義。

有沒有更好的方法來製作2個源目錄的項目?我應該只是將不同目錄中的少量源添加到迷你庫中,然後添加它?

的Makefile:

CC ?= gcc 
ARCH = $(shell $(CC) -dumpmachine) 

# compile and link flags 
CC_FLAGS ?= -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ 

CC_INCLUDE = -I../../libSOS/trunk/include 
CC_INCLUDE += -I. -I../../include 
CC_INCLUDE += -I../lib/ABCUtilSO/src 
CC_INCLUDE += -I../../Bla/trunk/src 

# Libraries 
LIBS := -lrt -lpthread -lconfig -lm 
LIBS += -L../../libSOS/trunk/build/$(ARCH) -lSOS 
LIBS += -L../lib/ABCUtilLib/build/$(ARCH) -lABCUtil 
LIBS += -L../lib/ABCUtilSO/build/$(ARCH) -lABCMain 


OUT1_DIR = build/$(ARCH)/obj 
OUT2_DIR = build/$(ARCH)/obj/Bla 

# result 
EXEC = ${OUT1_DIR}/../PaymentHandlerDaemon 

#------------------------------------------------------------- 
# main sources (1) 
SRC1_DIR = src 
#all the sources in the srec directory will be included 
SOURCES1 = $(wildcard ${SRC1_DIR}/*.c) 
# deduce the object list from the source list 
_OBJECTS1 = $(patsubst %.c,%.o,$(SOURCES1)) 
# the replace the source directory wioth the output directory 
OBJECTS1 = $(patsubst ${SRC1_DIR}%,${OUT1_DIR}%,$(_OBJECTS1)) 

#------------------------------------------------------------- 
# extra sources (2) 
SRC2_DIR = ../../Bla/trunk/src 
#SRC2_LIST = Meter.c 
SRC2_LIST = CheckSum.c Config.c 
#all the sources in the srec directory will be included 
#SOURCES2 = $(pathsubs %,$(SRC2_DIR)/%,$(SRC2_LIST)) 
SOURCES2 = $(patsubst %.c,${SRC2_DIR}/%.c,$(SRC2_LIST)) 
# deduce the object list from the source list 
_OBJECTS2 = $(patsubst %.c,%.o,$(SOURCES2)) 
# the replace the source directory wioth the output directory 
OBJECTS2 = $(patsubst ${SRC2_DIR}%,${OUT2_DIR}%,$(_OBJECTS2)) 

#------------------------------------------------------------- 
# build receipes 

all: $(EXEC) 
# Main target 
$(EXEC): $(OBJECTS1) $(OBJECTS2) 
    $(CC) $(OBJECTS1) $(OBJECTS2) -o [email protected] $(LIBS) 


# To obtain object files 
${OUT1_DIR}%.o:${SRC1_DIR}%.c 
    @mkdir -p $(OUT1_DIR) 
    $(CC) -c $(CC_FLAGS) $(CC_INCLUDE) $< -o [email protected] 

# # To obtain object files 
${OUT2_DIR}%.o:${SOURCES2} 
    @mkdir -p $(OUT2_DIR) 
    $(CC) -c $(CC_FLAGS) $(CC_INCLUDE) $< -o [email protected] 


# To remove generated files 
clean: 
    rm -fr build/$(ARCH) 


# debug.. for example use as make -print-SRC_DIR 
print-% : ; @echo $* = $($*) 

回答

1

人們建立一個項目,從多個目錄中的所有的時間。每種情況都需要不同的方法。

你發佈的內容應該有效。由於Makefile,多重定義很可能是而不是。我會更密切地看待錯誤。 toolchain告訴你它在哪裏找到了兩個定義。

的Makefile:

CC ?= gcc 
# compile and link flags 
CC_FLAGS ?= -c -w -fPIC -g3 -Og 

CC_INCLUDE += -I../../AnotherProject/src 

# Libraries 
LIBS := -lrt -lpthread -lconfig -lm 

OUT1_DIR = build/obj 
OUT2_DIR = build/obj/extra2 

# result 
EXEC = build/MyProgram 

#------------------------------------------------------------- 
# main sources (1) (everything in src directory) 
SRC1_DIR = src 
SOURCES1 = $(wildcard ${SRC1_DIR}/*.c) 
# deduce the object list from the source list 
_OBJECTS1 = $(patsubst %.c,%.o,$(SOURCES1)) 
# the replace the source directory wioth the output directory 
OBJECTS1 = $(patsubst ${SRC1_DIR}%,${OUT1_DIR}%,$(_OBJECTS1)) 

#------------------------------------------------------------- 
# extra sources (just cherry pick what i need to reuse) 
SRC2_DIR = ../../AnotherProject/src 
SRC2_LIST = CheckSum.c Config.c 
#all the sources in the srec directory will be included 
SOURCES2 = $(patsubst %.c,${SRC2_DIR}/%.c,$(SRC2_LIST)) 
# deduce the object list from the source list 
_OBJECTS2 = $(patsubst %.c,%.o,$(SOURCES2)) 
# the replace the source directory wioth the output directory 
OBJECTS2 = $(patsubst ${SRC2_DIR}%,${OUT2_DIR}%,$(_OBJECTS2)) 

#------------------------------------------------------------- 
# build receipes 

all: debug $(EXEC) 

debug: FORCE 
    echo "1====" $(OBJECTS1) 
    echo "2====" $(OBJECTS2) 

FORCE: 

# Main target 
$(EXEC): $(OBJECTS1) $(OBJECTS2) 
    $(CC) $(OBJECTS1) $(OBJECTS2) -o [email protected] $(LIBS) 


# To obtain object files 
${OUT1_DIR}/%.o:${SRC1_DIR}/%.c 
    @mkdir -p $(OUT1_DIR) 
    $(CC) -c $(CC_FLAGS) $(CC_INCLUDE) $< -o [email protected] 

# To obtain the extra object files 
${OUT2_DIR}/%.o:${SRC2_DIR}/%.c 
    @mkdir -p $(OUT2_DIR) 
    $(CC) -c $(CC_FLAGS) $(CC_INCLUDE) $< -o [email protected] 

編輯

這是你的問題。

cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src ../../Bla/trunk/src/CheckSum.c -o build/x86_64-linux-gnu/obj/Bla/CheckSum.o 
cc -c -c -w -fPIC -g3 -Og -D__EMULATE_SOS__ -I../../libSOS/trunk/include -I. -I../../include -I../lib/ABCUtilSO/src -I../../Bla/trunk/src ../../Bla/trunk/src/CheckSum.c -o build/x86_64-linux-gnu/obj/Bla/Config.o 

你看到它從同一個源文件CheckSum.c建設既CheckSum.oConfig.o。因此在鏈接時有多個定義。這是你有錯誤模式規則的結果。

${OUT2_DIR}%.o:${SOURCES2} 
+0

謝謝ziffusion。我剛剛更新了錯誤的實際輸出的問題。 – clogwog

+0

你可以發佈整個構建輸出嗎?另外,Makefile是否正確?我看到了一些奇怪的東西。 1.'$ {OUT1_DIR}%。o:$ {SRC1_DIR}%。c $ {SOURCES2}'似乎很奇怪。 2.'$ {OUT2_DIR}%。o:$ {SOURCES2}'似乎沒有正確縮進。 – Ziffusion

+0

我縮小了'$ {OUT2_DIR}%o:$ {SOURCES2}'的行 - 否則它是一個奇怪的。它看起來好像每個目標文件都依賴於每個源文件,所以如果源文件發生更改,那麼該目錄中的所有目標文件都將被重新編譯。我懷疑這是不是想要的。 –