我创建了一个带有自定义构建系统(使用 Ruby)的小项目,无需安装 Arduino IDE 即可轻松完成。基本上,它使用模板 Makefile 和 ruby 脚本来使编译 Arduino 库变得非常容易。你可以在https://github.com/Earlz/make-wiring看到它
但是,我将在此处留下旧答案,以获取有关自行滚动的信息。虽然这很麻烦而且很烦人:
方向:
- 下载 Arduino IDE 源代码的副本
- 将内容复制
hardware/arduino/cores/arduino
到一个新目录,我将其称为 arduino_build
- 将
pins_arduino.h
文件从您的任何 Arduino 变体复制hardware/arduino/variants
(如果不确定,请检查 board.txt)到 arduino_build
- 将此生成文件添加到 arduino_build:
.
#BSD licensed, see http://lastyearswishes.com/static/Makefile for full license
HDRS = Arduino.h binary.h Client.h HardwareSerial.h IPAddress.h new.h pins_arduino.h Platform.h Printable.h Print.h \
Server.h Stream.h Udp.h USBAPI.h USBCore.h USBDesc.h WCharacter.h wiring_private.h WString.h
OBJS = WInterrupts.o wiring_analog.o wiring.o wiring_digital.o wiring_pulse.o wiring_shift.o CDC.o HardwareSerial.o \
HID.o IPAddress.o main.o new.o Print.o Stream.o Tone.o USBCore.o WMath.o WString.o
#may need to adjust -mmcu if you have an older atmega168
#may also need to adjust F_CPU if your clock isn't set to 16Mhz
CFLAGS = -I./ -std=gnu99 -DF_CPU=16000000UL -Os -mmcu=atmega328p
CPPFLAGS = -I./ -DF_CPU=16000000UL -Os -mmcu=atmega328p
CC=avr-gcc
CPP=avr-g++
AR=avr-ar
default: libarduino.a
libarduino.a: ${OBJS}
${AR} crs libarduino.a $(OBJS)
.c.o: ${HDRS}
${CC} ${CFLAGS} -c $*.c
.cpp.o: ${HDRS}
${CPP} ${CPPFLAGS} -c $*.cpp
clean:
rm -f ${OBJS} core a.out errs
install: libarduino.a
mkdir -p ${PREFIX}/lib
mkdir -p ${PREFIX}/include
cp *.h ${PREFIX}/include
cp *.a ${PREFIX}/lib
然后就跑
make
make install PREFIX=/usr/arduino (or whatever)
然后使用编译的库等你可以使用一个简单的makefile,如下所示:
default:
avr-g++ -L/usr/arduino/lib -I/usr/arduino/include -Wall -DF_CPU=16000000UL -Os -mmcu=atmega328p -o main.elf main.c -larduino
avr-objcopy -O ihex -R .eeprom main.elf out.hex
upload:
avrdude -c arduino -p m328p -b 57600 -P /dev/ttyUSB0 -U flash:w:out.hex
all: default upload
此外,如果您尝试编译其中的库,libraries/
如果您没有按正确的顺序执行操作,则会出现链接器错误。例如,我必须这样做才能使用 SoftwareSerial:
avr-g++ -L/usr/arduino/lib -I/usr/arduino/include -Wall -DF_CPU=16000000UL -Os -mmcu=atmega328p -o main.elf main.c -lSoftwareSerial -larduino
-larduino
必须是命令行上的最后一个库
无论如何,这是为我编译它的一种非常简单的方法。随着未来版本的 Ardunio 的出现,这个 makefile 应该是相当面向未来的,只需要对 OBJS 和 HDRS 进行一些修改。此外,这个 makefile 应该适用于 BSD make 和 GNU make
另请参阅我的博客上此答案的略微修改版本,其中包含已编译的库二进制文件(使用“标准”pins_arduino.h 编译)。
**编辑**
我发现将以下编译器优化标志添加到库构建 Makefile 和每个单独的项目 Makefile 大大减少了最终编译二进制文件的大小。这使得最终的二进制大小与 IDE 的大小相当。
-Wl,--gc-sections -ffunction-sections -fdata-sections
.
因此,对于库构建 makefile:
CFLAGS = -I./ -std=gnu99 -DF_CPU=16000000UL -Os -Wl,--gc-sections -ffunction-sections -fdata-sections -mmcu=atmega328p
CPPFLAGS = -I./ -DF_CPU=16000000UL -Os -Wl,--gc-sections -ffunction-sections -fdata-sections -mmcu=atmega328p
并且,对于每个项目生成文件:
avr-g++ -L/usr/arduino/lib -I/usr/arduino/include -Wall -DF_CPU=16000000UL -Os -Wl,--gc-sections -ffunction-sections -fdata-sections -mmcu=atmega328p -o main.elf main.c -larduino
.
参考:http ://arduino.cc/forum/index.php?topic=153186.0