在没有 Arduino IDE 的情况下为 Arduino 编程……但使用提供的库?

电器工程 Arduino 编译器 图书馆
2022-01-14 07:20:32

我最近使用我的 Arduino 开始了一个新项目,该项目已经有一段时间了。除了物理板上的灰尘,我的 avr-gcc 副本和 Arduino 库也是如此。我可以管理更新 avr-gcc,但我不记得我是如何编译 Arduino 库的。使用 Arduino 源,列出了源文件等,但没有用于实际构建库的 makefile(我可以看到)。我不关心构建 IDE,我只想要二进制格式的库和我需要的头文件。是否有任何文件或类似的东西来做这件事?

我不想使用 Arduino IDE,我更喜欢使用自己的文本编辑器和 makefile。

3个回答

我创建了一个带有自定义构建系统(使用 Ruby)的小项目,无需安装 Arduino IDE 即可轻松完成。基本上,它使用模板 Makefile 和 ruby​​ 脚本来使编译 Arduino 库变得非常容易。你可以在https://github.com/Earlz/make-wiring看到它

但是,我将在此处留下旧答案,以获取有关自行滚动的信息。虽然这很麻烦而且很烦人:

方向:

  1. 下载 Arduino IDE 源代码的副本
  2. 将内容复制hardware/arduino/cores/arduino到一个新目录,我将其称为 arduino_build
  3. pins_arduino.h文件从您的任何 Arduino 变体复制hardware/arduino/variants(如果不确定,请检查 board.txt)到 arduino_build
  4. 将此生成文件添加到 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

如果您愿意使用 Arduino IDE 一次(或每种设备类型一次),这是构建静态库以及获取库源的最简单方法。之后,您可以使用任何适合您的开发工具。

这篇Arduino 文章(为迁移到 Eclipse IDE 的用户编写)描述了通过使用 Arduino IDE 编译草图并从 Arduino 的临时目录中检索库来构建 Arduino 库。向下滚动大约 1/4 的页面到该部分

  1. 从 Arduino IDE 项目复制库

如果您只需要一个还可以控制您的微控制器编程器的构建系统,那么platformio是您的朋友。