Skip to content

Commit cc69023

Browse files
committed
[examples] Simple example for <vector>
1 parent 1ae9f0c commit cc69023

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

examples/vector/Makefile

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
NAME=vector-test
2+
MCU=atmega328p
3+
F_CPU=16000000ul
4+
5+
PROGRAMMER=arduino
6+
AVRDUDE_FLAGS= -P/dev/ttyUSB0 -b57600
7+
8+
BUILD_DIR=./build
9+
LIB_DIR=../..
10+
COMMON_DIR=../common
11+
12+
INCLUDES=-I$(COMMON_DIR) -I$(LIB_DIR)/include
13+
14+
SOURCES=$(wildcard *.cpp $(LIB_DIR)/src/*.cc $(COMMON_DIR)/*.cpp)
15+
VPATH=.:$(LIB_DIR)/src:$(COMMON_DIR)
16+
OBJECTS=$(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:%=%.o)))
17+
18+
CXXFLAGS=-std=c++17 -O2 -Wall -Wextra -pedantic -fno-exceptions -fno-rtti -fno-unwind-tables -fno-threadsafe-statics -Wshadow -Wcast-qual -Wpointer-arith -Wundef -DF_CPU=$(F_CPU)
19+
LDFLAGS=
20+
21+
TARGET=$(BUILD_DIR)/$(NAME)
22+
23+
all: hex size
24+
25+
hex: $(TARGET).hex
26+
27+
$(TARGET).hex: $(TARGET).elf
28+
avr-objcopy -O ihex -j .data -j .text $(TARGET).elf $(TARGET).hex
29+
30+
$(TARGET).elf: $(OBJECTS)
31+
avr-g++ $(LDFLAGS) -mmcu=$(MCU) $(OBJECTS) -o $(TARGET).elf
32+
33+
$(BUILD_DIR)/%.cpp.o: %.cpp
34+
@mkdir -p $(BUILD_DIR)
35+
avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@
36+
37+
$(BUILD_DIR)/%.cc.o: %.cc
38+
@mkdir -p $(BUILD_DIR)
39+
avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@
40+
41+
size: $(TARGET).elf
42+
avr-size --mcu=$(MCU) -C $(TARGET).elf
43+
44+
program: $(TARGET).hex
45+
avrdude -p$(MCU) $(AVRDUDE_FLAGS) -c$(PROGRAMMER) -Uflash:w:$(TARGET).hex:a
46+
47+
clean:
48+
rm -rf $(BUILD_DIR)/*.o
49+
rm -rf $(BUILD_DIR)/*.elf
50+
rm -rf $(BUILD_DIR)/*.hex

examples/vector/main.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <vector>
2+
#include <cstdio>
3+
#include <algorithm>
4+
5+
int main()
6+
{
7+
// output on Uart0, see common/uart.cpp
8+
puts("AVR libstdc++ vector test\n");
9+
10+
std::vector<uint8_t> test{10, 1, 2, 42, 3};
11+
test.push_back(4);
12+
test.erase(test.begin());
13+
14+
if(auto it = std::find(test.begin(), test.end(), 42); it != test.end()) {
15+
test.erase(it);
16+
}
17+
18+
for(auto i : test) {
19+
printf("%i ", i);
20+
}
21+
puts("\n");
22+
}

0 commit comments

Comments
 (0)