我正在尝试编译一些使用USART和std函数的代码,例如printf()用于stm32f4发现。首先,我使用g ++将所有代码文件编译成目标文件,但是当我使用链接器创建可执行文件时,链接器抛出错误,描述它无法找到startup_stm32f4xx.s汇编文件中实现的任何函数。
我可以使用Makefile模板表单将所有源代码编译成目标文件,然后将其链接到* .elf可执行文件吗?
另外,如何设置链接器以查看newlib_stubs.c,以便在源代码中使用printf()?
答案 0 :(得分:1)
这是您的proyect的通用Makefile(经过编译测试,但如果它将在MCU上运行则不进行测试)
此脚本可以处理C / C ++和S(gas asm)文件。所以不需要使用coocox的C-startup文件
# ELF target name
TARGET=testmode09.elf
# C source files
SRC=src/system_stm32f4xx.c
# C++ source files
CXXSRC=src/main.cpp
# ASM source files
ASRC=lib/startup_stm32f4xx.s
# include directories
INCDIRS=inc lib/inc lib/inc/core lib/inc/peripherals
# search path for .so and .a
LIBDIRS=lib
# libraries to link
LIBS=libstm32f4.a
# Linker script
LDSCRIPT=stm32_flash.ld
# Tool configuration
CROSS=arm-none-eabi-
CC=$(CROSS)gcc
CXX=$(CROSS)g++
AS=$(CROSS)gcc
LD=$(CROSS)gcc
OBJCOPY=$(CROSS)objcopy
# Architecture configuration
ARCH_FLAGS=-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
# Flags for gcc
CFLAGS+=-O0 -ggdb3
CFLAGS+=$(ARCH_FLAGS)
CFLAGS+=-flto
CFLAGS+=-ffunction-sections
CFLAGS+=-fdata-sections
CFLAGS+=$(foreach i, $(INCDIRS), -I$(i))
# Flags for g++
CXXFLAGS=$(CFLAGS)
CXXFLAGS+=-fno-rtti -fno-exceptions
CXXFLAGS+=-std=c++11
# Flags for gcc as linker
LDFLAGS=$(ARCH_FLAGS)
LDFLAGS+=-Wl,--gc-sections
LDFLAGS+=$(foreach i, $(LIBDIRS), -L$(i))
LDFLAGS+=-T $(LDSCRIPT)
# No man land! Enter at your risk
OBJS=$(SRC:.c=.o) $(ASRC:.s=.o) $(CXXSRC:.cpp=.o)
DEPS=$(OBJS:.o=.d)
# Default make target (first target)
all: $(TARGET) bins
# include dependency files (*.d)
-include $(DEPS)
# Rules to build c/cpp/s files
%.o: %.c
@echo " CC $<"
@$(CC) -MMD $(CFLAGS) -o $@ -c $<
%.o: %.cpp
@echo " CXX $<"
@$(CXX) -MMD $(CXXFLAGS) -o $@ -c $<
%.o: %.s
@echo " AS $<"
@$(AS) $(CFLAGS) -o $@ -c $<
# Linker rule
$(TARGET): libs $(OBJS)
@echo " LINK $@"
@$(LD) -o $@ $(LDFLAGS) $(OBJS)
# Phony rules (not file-asociated)
.PHONY: clean all libs clean_lib bins
libs:
@$(MAKE) -C lib
clean_lib:
@$(MAKE) -C lib clean
clean:
@echo " CLEAN"
@rm -fR $(OBJS) $(DEPS) $(TARGET) $(TARGET:.elf=hex) $(TARGET:.elf=.bin)
bins: $(TARGET)
@echo " BUILD HEX & BIN"
@$(OBJCOPY) -O ihex $(TARGET) $(TARGET:.elf=.hex)
@$(OBJCOPY) -O binary $(TARGET) $(TARGET:.elf=.bin)
make的核心参考资料在官方手册中:http://www.gnu.org/software/make/manual/make.html 但你需要一些黑客时间来理解它; - )
建议,将syscalls代码放入名为syscall.c / cpp的单独C / C ++文件中。或者类似的东西。