teensydude
New member
Because I'm a stubborn jerk, I do not want to use Teensyduino. I want something more low-level for total control over Teensy 4.0, but I love my C++ STL. So, I discovered this nifty project someone kindly put on GitHub with a bare minimum Blinky that compiles straight C code for Teensy 4.0:
https://github.com/blazer82/baremetal-blinky.teensy
So far so good. I was able to alter the Makefile to use g++ to compile cpp files. I can write and compile code with classes, templates and all the fun stuff from C++. Hooray!
But, I cannot for the life of me get any STL code like std::vector to link after compiling. I am pulling my hair out, searching Google like a madman, and tried all of the suggestions (of course I am using -lstdc++ to the linker flags). No dice. I get various linker errors, when I include stdc++ (underfed reference to new, or delete, or exit).
See code attached below:
View attachment baremetal-blinky.teensy.zip
I'm starting to think that the STL might actually not be supported on this thing. Is that true? I couldn't find a clear answer, but I have read a few articles that suggest that STL support with GNU Toolchain for Embedded ARM *does* work.
Has anyone gotten this to work? Would be great if I could find a bare minimum Makefile. Here is what I'm using now:
https://github.com/blazer82/baremetal-blinky.teensy
So far so good. I was able to alter the Makefile to use g++ to compile cpp files. I can write and compile code with classes, templates and all the fun stuff from C++. Hooray!
But, I cannot for the life of me get any STL code like std::vector to link after compiling. I am pulling my hair out, searching Google like a madman, and tried all of the suggestions (of course I am using -lstdc++ to the linker flags). No dice. I get various linker errors, when I include stdc++ (underfed reference to new, or delete, or exit).
Code:
new_allocator.h:114: undefined reference to `operator new(unsigned int)'
See code attached below:
View attachment baremetal-blinky.teensy.zip
I'm starting to think that the STL might actually not be supported on this thing. Is that true? I couldn't find a clear answer, but I have read a few articles that suggest that STL support with GNU Toolchain for Embedded ARM *does* work.
Has anyone gotten this to work? Would be great if I could find a bare minimum Makefile. Here is what I'm using now:
Code:
CC = arm-none-eabi-gcc
CXX = arm-none-eabi-g++
LD = arm-none-eabi-ld
OBJCOPY = arm-none-eabi-objcopy
OBJDUMP = arm-none-eabi-objdump
SIZE = arm-none-eabi-size
LOADER = teensy_loader_cli
OUTFILE = firmware
BUILD_DIR = ./build
SRC_DIRS ?= ./src ./teensy ./include
SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.cpp -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)
FLAGS_CPU := -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16
FLAGS_OPT := -O3
FLAGS_COM := -g -Wall -ffunction-sections -fdata-sections -MMD -nostdlib -nostartfiles
FLAGS_LSP :=
FLAGS_CPP := -std=c++17 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing
FLAGS_C :=
FLAGS_S := -x assembler-with-cpp
FLAGS_LD := -Wl,--gc-sections,--print-memory-usage,--relax,-Tteensy/imxrt1062.ld -specs=nosys.specs
INCLUDE_DIRS := $(ARM_NONE_EABI_INCLUDE) $(shell find $(SRC_DIRS) -type d)
FLAGS_INCLUDE := $(addprefix -I,$(INCLUDE_DIRS))
LIB_DIRS := $(ARM_NONE_EABI_LIB)
FLAGS_LIB_DIRS = $(addprefix -L,$(LIB_DIRS))
LIBS := m stdc++_nano gcc g_nano c_nano
FLAGS_LIBS := $(addprefix -l,$(LIBS))
DEFINES := -D__IMXRT1062__ -DTEENSYDUINO=147 -DARDUINO=10807
DEFINES += -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH
CPP_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_COM) $(DEFINES) $(FLAGS_CPP) $(FLAGS_INCLUDE)
C_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_COM) $(DEFINES) $(FLAGS_C) $(FLAGS_INCLUDE)
S_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_COM) $(DEFINES) $(FLAGS_S) $(FLAGS_INCLUDE)
LD_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_LSP) $(FLAGS_LD) $(FLAGS_LIB_DIRS) $(FLAGS_LIBS)
AR_FLAGS := rcs
$(BUILD_DIR)/$(OUTFILE).hex: $(BUILD_DIR)/$(OUTFILE).elf
@$(OBJCOPY) -O ihex -R .eeprom build/$(OUTFILE).elf build/$(OUTFILE).hex
@$(OBJDUMP) -d -x build/$(OUTFILE).elf > build/$(OUTFILE).dis
@$(OBJDUMP) -d -S -C build/$(OUTFILE).elf > build/$(OUTFILE).lst
@$(SIZE) build/$(OUTFILE).elf
$(BUILD_DIR)/$(OUTFILE).elf: $(OBJS)
@$(CXX) $(CPP_FLAGS) -Xlinker -Map=build/$(OUTFILE).map -Tteensy/imxrt1062.ld -o $@ $^
@echo Linking...
$(BUILD_DIR)/%.s.o: %.s
@$(MKDIR_P) $(dir $@)
@$(AS) $(S_FLAGS) -c $< -o $@
@echo Assembling $<...
$(BUILD_DIR)/%.c.o: %.c
@$(MKDIR_P) $(dir $@)
@$(CC) $(C_FLAGS) -c $< -o $@
@echo Compiling $<...
$(BUILD_DIR)/%.cpp.o: %.cpp
@$(MKDIR_P) $(dir $@)
@$(CXX) ${CPP_FLAGS} -c $< -o $@
@echo Compiling $<...
.PHONY: flash
flash: $(BUILD_DIR)/$(OUTFILE).hex
$(LOADER) --mcu=TEENSY40 -w -v $<
.PHONY: clean
clean:
@$(RM) -r $(BUILD_DIR)
MKDIR_P ?= mkdir -p