通用Makefile模式将每个源编译为相应的目标文件

时间:2015-04-16 08:35:41

标签: c gcc makefile rule

您好我正在尝试使用通用makefile来构建我的项目。

我得到的错误是我的源代码中存在重复的函数。我检查了nm,是的,他们在那里。我知道我可以用每个明确命名的源文件编写一个normale makefile,并逐行编译。但是我的项目的性质要求,其他人能够在没有新平台的情况下整合新平台,并将整个事件移植到新机器上。 (这意味着从长远来看,即使是直接的库路径也必须变得相对) 任何帮助表示赞赏。

其他信息: 主机系统是Windows 7,我使用MinGW 32(nuwens发行版)进行编译 - 作为交叉编译器和库提供程序,我使用ARM-Launchpad中的gnu-arm-none-eabi-gcc。

#PATHS

GEN_SOURCE_PATH=    ../../source/generate_osek
HW_SOURCE_PATH=     $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/
ASM_PATH=           $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/$(PLATFORM)_asm
HEADER_PATH=        $(GEN_SOURCE_PATH)/header


#HEADERS
ORG_HEADERS =   $(HEADER_PATH)/
HW_HEADERS  =   $(HEADER_PATH)/$(PLATFORM)_head/
ALL_HEADERS =   $(ORG_HEADERS) $(HW_HEADERS)

INCLUDES =      -I/c/osek/workspace/htosek/source/generate_osek/header/
INCLUDES +=     -I/c/osek/workspace/htosek/source/generate_osek/header/$(PLATFORM)_head/
INCLUDES +=     -I/c/osek/workspace/htosek/source/$(PLATFORM)_hw/
INCLUDES +=     -I/c/osek/workspace/htosek/source/
INCLUDES +=     -I/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/include/
#Library
LIBS = -L/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/  -lgcc


#LISTSOURCES
ASM_INCLUDE= -I$(ASM_PATH)/   


#SOURCEFILES
CFILES      =   $(foreach dir,$(GEN_SOURCE_PATH)/,$(notdir $(wildcard $(dir)/*.c)))
CFILES      +=  $(foreach dir,$(HW_SOURCE_PATH),$(notdir $(wildcard $(dir)/*.c)))
SFILES      =   $(foreach dir,$(ASM_PATH)/,$(notdir $(wildcard $(dir)/*.s)))
HFILES      =   $(foreach dir,$(ORG_HEADERS),$(notdir $(wildcard $(dir)/*.h)))
HFILES      +=  $(foreach dir,$(HW_HEADERS),$(notdir $(wildcard $(dir)/*.h)))

#VPATH
VPATH= $(GEN_SOURCE_PATH)
VPATH+= $(ASM_PATH)
VPATH+= $(ALL_HEADERS)

#DEPENDENCIES
DEP = $(subst .o,.d,$(SOBJS) $(COBJS))
#Converting

SOBJS  =    $(SFILES:.s=.o)
COBJS   =   $(CFILES:.c=.o)
OBJECTS =  $(COBJS) $(SOBJS) 
#WARNINGS= -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror -Wno-unused
LOCAL_CFLAG= -v -d -c    -ffreestanding
C_FLAGS += $(LOCAL_CFLAG)

#GENERATE_OSEK BINARY
os: bin
    @echo "Generating binary image file: $@"
    $(OBJCOPY_DEVICE) -O binary $(EXEC) $@

#LINKING OBJECTS
bin:$(OBJECTS)  
    @echo ""
    @echo "Linking $<..." 
    $(LD_DEVICE) -o $(EXEC)  $(OBJECTS)  $(LD_FLAGS) $(LIBS)
    @echo ""
    @echo "And done..."

#COMPILING ASSEMBLY
$(SOBJS):  $(SFILES)
    @echo ""
    @echo "Assembling $<..."
    $(ASM_DEVICE)  $(ASM_INCLUDE)  $(ASM_FLAGS) -g -v  -o $@   $^ 

#COMPILING C SOURCES
$(COBJS): $(CFILES) ${HFILES}
    @echo ""
    @echo Compiling $<...
    $(CC_DEVICE) $(C_FLAGS)  $(INCLUDES) $(LIBS)  -g  -o $@  $^

os_clean:
     RM *.o 
     RM $(EXEC) 

更新: 我在VPATH中添加了一些缺失的包含,现在至少它编译为开头所述的错误:

更新了Makefile:     #PATHS

GEN_SOURCE_PATH=    ../../source/generate_osek
HW_SOURCE_PATH=     $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/
ASM_PATH=           $(GEN_SOURCE_PATH)/$(PLATFORM)_hw/$(PLATFORM)_asm
HEADER_PATH=        $(GEN_SOURCE_PATH)/header


#HEADERS
ORG_HEADERS =   $(HEADER_PATH)/
HW_HEADERS  =   $(HEADER_PATH)/$(PLATFORM)_head/
ALL_HEADERS =   $(ORG_HEADERS) $(HW_HEADERS)

INCLUDES =      -I/c/osek/workspace/htosek/source/generate_osek/header/
INCLUDES +=     -I/c/osek/workspace/htosek/source/generate_osek/header/$(PLATFORM)_head/
INCLUDES +=     -I/c/osek/workspace/htosek/source/
INCLUDES +=     -I/c/osek/workspace/htosek/source/$(PLATFORM)_hw/
INCLUDES +=     -I/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/include/
#Library
LIBS = -L/c/gnuarm/lib/gcc/arm-none-eabi/4.7.4/  -lgcc


#LISTSOURCES
ASM_INCLUDE= -I$(ASM_PATH)/   


#SOURCEFILES
CFILES      =   $(foreach dir,$(GEN_SOURCE_PATH)/,$(notdir $(wildcard $(dir)/*.c)))
CFILES      +=  $(foreach dir,$(HW_SOURCE_PATH),$(notdir $(wildcard $(dir)/*.c)))
SFILES      =   $(foreach dir,$(ASM_PATH)/,$(notdir $(wildcard $(dir)/*.s)))
HFILES      =   $(foreach dir,$(ORG_HEADERS),$(notdir $(wildcard $(dir)/*.h)))
HFILES      +=  $(foreach dir,$(HW_HEADERS),$(notdir $(wildcard $(dir)/*.h)))

#VPATH
VPATH= $(GEN_SOURCE_PATH)/
VPATH += $(HW_SOURCE_PATH)
VPATH+= $(ASM_PATH)
VPATH+= $(ALL_HEADERS)

#DEPENDENCIES
DEP = $(subst .o,.d,$(SOBJS) $(COBJS))
#Converting

SOBJS  =    $(SFILES:.s=.o)
COBJS   =   $(CFILES:.c=.o)
OBJECTS =  $(COBJS) $(SOBJS) 
#WARNINGS= -pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Werror -Wno-unused
LOCAL_CFLAG= -v -d -c    -ffreestanding
C_FLAGS += $(LOCAL_CFLAG)

#GENERATE_OSEK BINARY
os: bin
    @echo "Generating binary image file: $@"
    $(OBJCOPY_DEVICE) -o binary $(EXEC) $@

#LINKING OBJECTS
bin:$(OBJECTS)  
    @echo ""
    @echo "Linking $<..." 
    $(LD_DEVICE) -o $(EXEC)  $(OBJECTS)  $(LD_FLAGS) $(LIBS) -o $@
    @echo ""
    @echo "And done..."

#COMPILING ASSEMBLY
$(SOBJS):  $(SFILES)
    @echo ""
    @echo "Assembling $<..."
    $(ASM_DEVICE)  $(ASM_INCLUDE)  $(ASM_FLAGS) -g -v  -o $@   $^ 

#COMPILING C SOURCES
$(COBJS): $(CFILES) ${HFILES}
    @echo ""
    @echo "Compiling: $< -> $@"
    $(CC_DEVICE) $(C_FLAGS)  $(INCLUDES) $(LIBS)  -g  -o $@ $^

os_clean:
     RM *.o 
     RM $(EXEC) 
     RM *.gch

更新了错误示例:

debug_hook.o: In function `resetAlarm':
c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:19: multiple definition of `resetAlarm'
alarm.o:c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:19: first defined here
debug_hook.o: In function `RaiseAlarm':
c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:25: multiple definition of `RaiseAlarm'
alarm.o:c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:25: first defined here
debug_hook.o: In function `TimeTillAlarm':
c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:54: multiple definition of `TimeTillAlarm'
alarm.o:c:\osek\workspace\htosek\project\test/../../source/generate_osek/alarm.c:54: first defined here
make: *** [bin] Error 1

TimeTillAlarm只存在一次。其他地方没有预先声明。在整个来源上为它做好准备。

链接脚本:

arm-none-eabi-ld -o test.elf alarm.o debug_hook.o fifo_array.o fifo_queue.o htosek_startup.o i_basic_op_abstraction.o i_driver_op_abstraction.o othsek.o task.o at91sam72s256_restarttask.o at91sam72s256_startup.o at91sam72s256_system。 o __irfunctions.o _activatetask.o _contextswitch.o _cstartup.o _ir_vector_table.o _restartTask.o -M -nostartfiles -nostdlib -L ​​/ c / gnuarm / lib / gcc / arm-none-eabi / 4.7.4 / -lgcc'< / p>

更新 nm表示所有汇编器o.files具有相同的符号

P@P-PC /c/osek/workspace/htosek/project/test
$ nm debug_hook.o
00000084 t $a
00000000 t $a
00000010 N $d
00000080 t $d
0000047c C HTOsek
00000098 T InitAlarm
0000000c T RaiseAlarm
00000000 T resetAlarm
00000084 T TimeTillAlarm

P@P-PC /c/osek/workspace/htosek/project/test
$ nm fifo_array.o
00000084 t $a
00000000 t $a
00000010 N $d
00000080 t $d
0000047c C HTOsek
00000098 T InitAlarm
0000000c T RaiseAlarm
00000000 T resetAlarm
00000084 T TimeTillAlarm

来自c_sources的o.files看起来形状相同,但至少有一组不同的符号。 结论:一个源文件被制作成两个组的名为o.files的不同文件。有人建议为什么?

再次感谢您的耐心和建议

0 个答案:

没有答案