Teensy 4.1 with make file

crose72

New member
Hi,

I am trying to establish a workflow that does not rely on the Arduino IDE, and I'm using windows. My project is setup as such
toplevel.PNGtoplevel.PNG
wnere teensy4 comes from "ArduinoData\packages\teensy\hardware\avr\1.57.2\cores\teensy4". Now I have successfully been able to execute the makefile inside the "teensy4" folder which makes a blink LED program, and then I was able to flash that to my teensy 4.1 and low and behold the onboard LED blinks. Now I'm trying to set up my project in a way that makes sense to me (shown in the picture above) with a "src" folder for my source files and an "inc" folder for all of the headers specific to my project. I've copied the "teensy4" folder into my project structure so I could have the core files needed. Here is my the makefile in the picture of my project above
Code:
# Teensyduino Core Library
# http://www.pjrc.com/teensy/
# Copyright (c) 2019 PJRC.COM, LLC.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# 1. The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# 2. If the Software is incorporated into a build system that allows
# selection among a list of target devices, then similar target
# devices manufactured by PJRC.COM must be included in the list of
# target devices and selectable in the same manner.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Use these lines for Teensy 4.0
#MCU = IMXRT1062
#MCU_LD = imxrt1062.ld
#MCU_DEF = ARDUINO_TEENSY40

# Use these lines for Teensy 4.1
MCU = IMXRT1062
MCU_LD = imxrt1062_t41.ld
MCU_DEF = ARDUINO_TEENSY41

# The name of your project (used to name the compiled .hex file)
TARGET = main

# configurable options
OPTIONS = -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -DUSING_MAKEFILE
#
# USB Type configuration:
#   -DUSB_SERIAL
#   -DUSB_DUAL_SERIAL
#   -DUSB_TRIPLE_SERIAL
#   -DUSB_KEYBOARDONLY
#   -DUSB_TOUCHSCREEN
#   -DUSB_HID_TOUCHSCREEN
#   -DUSB_HID
#   -DUSB_SERIAL_HID
#   -DUSB_MIDI
#   -DUSB_MIDI4
#   -DUSB_MIDI16
#   -DUSB_MIDI_SERIAL
#   -DUSB_MIDI4_SERIAL
#   -DUSB_MIDI16_SERIAL
#   -DUSB_AUDIO
#   -DUSB_MIDI_AUDIO_SERIAL
#   -DUSB_MIDI16_AUDIO_SERIAL
#   -DUSB_MTPDISK
#   -DUSB_RAWHID
#   -DUSB_FLIGHTSIM
#   -DUSB_FLIGHTSIM_JOYSTICK

# options needed by many Arduino libraries to configure for Teensy model
OPTIONS += -D__$(MCU)__ -DARDUINO=10813 -DTEENSYDUINO=154 -D$(MCU_DEF)

# for Cortex M7 with single & double precision FPU
CPUOPTIONS = -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -mthumb

# use this for a smaller, no-float printf
#SPECS = --specs=nano.specs

# Other Makefiles and project templates for Teensy
#
# https://forum.pjrc.com/threads/57251?p=213332&viewfull=1#post213332
# https://github.com/apmorton/teensy-template
# https://github.com/xxxajk/Arduino_Makefile_master
# https://github.com/JonHylands/uCee


#************************************************************************
# Location of Teensyduino utilities, Toolchain, and Arduino Libraries.
# To use this makefile without Arduino, copy the resources from these
# locations and edit the pathnames.  The rest of Arduino is not needed.
#************************************************************************

# core teensy4 files
CORE_DIR := /teensy4

# core teensy4 includes
CORE_AVR := $(CORE_DIR)/avr
CORE_DEBUG := $(CORE_DIR)/debug
CORE_UTIL := $(CORE_DIR)/util

# project libraries
INC_DIR := /inc

# project source code
SRC_DIR := /src

# path to arduino tools
ARDUINOPATH ?= ../../../ArduinoData/packages

# path location for Teensy Loader, teensy_post_compile and teensy_reboot (on Linux)
TOOLSPATH = $(abspath $(ARDUINOPATH)/teensy/tools/teensy-tools/1.57.2)

# path location for Arduino libraries (currently not used)
LIBRARYPATH = $(abspath $(ARDUINOPATH)/hardware/avr/1.57.2/libraries)

# path location for the arm-none-eabi compiler
COMPILERPATH = $(abspath $(ARDUINOPATH)/teensy/tools/teensy-compile/5.4.1/arm/bin)

#************************************************************************
# Settings below this point usually do not need to be edited
#************************************************************************

INCLUDES = -I$(CORE_AVR) -I$(CORE_DEBUG) -I$(CORE_UTIL) -I$(CORE_DIR) -I$(INC_DIR)

# CPPFLAGS = compiler options for C and C++
CPPFLAGS = -Wall -g -O2 $(CPUOPTIONS) -MMD $(OPTIONS) $(INCLUDES) -ffunction-sections -fdata-sections

# compiler options for C++ only
CXXFLAGS = -std=gnu++14 -felide-constructors -fno-exceptions -fpermissive -fno-rtti -Wno-error=narrowing

# compiler options for C only
CFLAGS =

# linker options
LDFLAGS = -Os -Wl,--gc-sections,--relax $(SPECS) $(CPUOPTIONS) -T$(MCU_LD)

# additional libraries to link
LIBS = -larm_cortexM7lfsp_math -lm -lstdc++


# names for the compiler programs
CC = $(COMPILERPATH)/arm-none-eabi-gcc
CXX = $(COMPILERPATH)/arm-none-eabi-g++
OBJCOPY = $(COMPILERPATH)/arm-none-eabi-objcopy
SIZE = $(COMPILERPATH)/arm-none-eabi-size

# automatically create lists of the sources and objects
# TODO: this does not handle Arduino libraries yet...
CORE_C_FILES := $(wildcard $(CORE_DIR)/*.c)
CORE_CPP_FILES := $(wildcard $(CORE_DIR)/*.cpp)
C_FILES := $(wildcard $(SRC_DIR)/*.c)
CPP_FILES := $(wildcard $(SRC_DIR)/*.cpp)
OBJS := $(CORE_C_FILES:.c=.o) $(CORE_CPP_FILES:.cpp=.o) $(CORE_LIBS_AVR:.h=.o) $(C_FILES:.c=.o) $(CPP_FILES:.cpp=.o)


# the actual makefile rules (all .o files built by GNU make's default implicit rules)

all: $(TARGET).hex

$(TARGET).elf: $(OBJS) $(MCU_LD)
	$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)

%.hex: %.elf
	$(SIZE) $<
	$(OBJCOPY) -O ihex -R .eeprom $< $@
ifneq (,$(wildcard $(TOOLSPATH)))
	$(TOOLSPATH)/teensy_post_compile -file=$(basename $@) -path=$(shell pwd) -tools=$(TOOLSPATH)
	-$(TOOLSPATH)/teensy_reboot
endif

# compiler generated dependency info
-include $(OBJS:.o=.d)

# use the command "make clean SHELL=cmd" in windows to execute the clean script 
clean:
	-del -fR *.o *.d $(TARGET).elf $(TARGET).hex
and the error I get is
Code:
arm-none-eabi-gcc -Os -Wl,--gc-sections,--relax  -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -mthumb -Timxrt1062_t41.ld -o main.elf      -larm_cortexM7lfsp_math -lm -lstdc++
c:/users/camer/documents/arduinodata/packages/teensy/tools/teensy-compile/5.4.1/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ld.exe: address 0xc0 of main.elf section `.bss' is not within region `DTCM'
c:/users/camer/documents/arduinodata/packages/teensy/tools/teensy-compile/5.4.1/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ld.exe: address 0xc0 of main.elf section `.bss' is not within region `DTCM'
c:/users/camer/documents/arduinodata/packages/teensy/tools/teensy-compile/5.4.1/arm/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ld.exe: warning: cannot find entry symbol ImageVectorTable; not setting start address
collect2.exe: error: ld returned 1 exit status
make: *** [main.elf] Error 1
My makefile is based off of what is in the "teensy4" folder. But I've changed it to specify folder locations And my "main.cpp" file is also the one with the blink LED code (just to start).

I would love to receive help in resolving this error and setting up this makefile so that I can just add arduino libraries and start writing my code. If anything in my post is unclear please feel free to let me know.

P.S. I plan on posting this to gitgub so that other people with similar background and experience can have an easy time doing projects with teensy 4.1 without using the arduino IDE.
 
Looks like this from your makefile (color for each part)

Code:
$(TARGET).elf: $(OBJS) $(MCU_LD)
	[COLOR="#FFD700"]$(CC)[/COLOR] [COLOR="#800080"]$(LDFLAGS)[/COLOR] -o [COLOR="#006400"]$@[/COLOR] [COLOR="#FF0000"]$(OBJS)[/COLOR] [COLOR="#006400"][COLOR="#0000CD"]$(LIBS)[/COLOR][/COLOR]

resulted in this command line executed (color for each resulting part of the command)

Code:
[COLOR="#FFD700"]arm-none-eabi-gcc[/COLOR] [COLOR="#800080"]-Os -Wl,--gc-sections,--relax  -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -mthumb -Timxrt1062_t41.ld[/COLOR] -o [COLOR="#006400"]main.elf[/COLOR]      [COLOR="#0000CD"]-larm_cortexM7lfsp_math -lm -lstdc++[/COLOR]

Sure looks like the problem has something to do with the defining of $(OBJS)
 
Sure looks like the problem has something to do with the defining of $(OBJS)

Okay so I did notice something that probably shouldn't have been in my definition of OBJS, so I changed it to
Code:
OBJS := $(CORE_C_FILES:.c=.o) $(CORE_CPP_FILES:.cpp=.o) $(C_FILES:.c=.o) $(CPP_FILES:.cpp=.o)
I removed
Code:
$(CORE_LIBS_AVR:.h=.o)
I still get the same error though.
 
Back
Top