在形如 <X>:\Documents\Arduino\libraries
的目录下通常能找到当前 Arduino IDE 安装的所有库。
Arduino 第三方库的目录结构
按如下结构组织的文件才能被 Arduino IDE 识别为已安装的库。
└───PubSubClient # 以该库为例
│ CHANGES.txt # 更新日志
│ keywords.txt # 提供语法高亮功能
│ library.json # 库的一些相关信息
│ library.properties # 库的一些相关信息
│ LICENSE.txt # 协议
│ README.md
│
├───examples # 代码示例
│ ├───mqtt_auth
│ │ mqtt_auth.ino
│ │
│ ├───mqtt_basic
│ │ mqtt_basic.ino
│ │
│ ├───mqtt_esp8266
│ │ mqtt_esp8266.ino
│ │
│ ├───mqtt_large_message
│ │ mqtt_large_message.ino
│ │
│ ├───mqtt_publish_in_callback
│ │ mqtt_publish_in_callback.ino
│ │
│ ├───mqtt_reconnect_nonblocking
│ │ mqtt_reconnect_nonblocking.ino
│ │
│ └───mqtt_stream
│ mqtt_stream.ino
│
├───src # 源码
│ PubSubClient.cpp
│ PubSubClient.h
│
└───tests
│ Makefile
│ README.md
│ testsuite.py
│
├───src
│ │ connect_spec.cpp
│ │ keepalive_spec.cpp
│ │ publish_spec.cpp
│ │ receive_spec.cpp
│ │ subscribe_spec.cpp
│ │
│ └───lib
│ Arduino.h
│ BDDTest.cpp
│ BDDTest.h
│ Buffer.cpp
│ Buffer.h
│ Client.h
│ IPAddress.cpp
│ IPAddress.h
│ Print.h
│ ShimClient.cpp
│ ShimClient.h
│ Stream.cpp
│ Stream.h
│ trace.h
│
└───testcases
mqtt_basic.py
mqtt_publish_in_callback.py
settings.py
__init__.py
library.json / library.properties
都是一些库信息,例如作者、版本、描述,源码仓库之类的…
{
"name": "PubSubClient",
"keywords": "ethernet, mqtt, m2m, iot",
"description": "A client library for MQTT messaging. MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.",
"repository": {
"type": "git",
"url": "https://github.com/knolleary/pubsubclient.git"
},
"version": "2.8",
"exclude": "tests",
"examples": "examples/*/*.ino",
"frameworks": "arduino",
"platforms": [
"atmelavr",
"espressif8266",
"espressif32"
]
}
name=PubSubClient
version=2.8
author=Nick O'Leary <nick.oleary@gmail.com>
maintainer=Nick O'Leary <nick.oleary@gmail.com>
sentence=A client library for MQTT messaging.
paragraph=MQTT is a lightweight messaging protocol ideal for small devices. This library allows you to send and receive MQTT messages. It supports the latest MQTT 3.1.1 protocol and can be configured to use the older MQTT 3.1 if needed. It supports all Arduino Ethernet Client compatible hardware, including the Intel Galileo/Edison, ESP8266 and TI CC3000.
category=Communication
url=http://pubsubclient.knolleary.net
architectures=*
src 目录
这是最重要的部分,直接关系到 Arduino 是否能找得到头文件和源文件,从 Arduino 的工程构建逻辑来看,.h/.cpp 文件应当放在同一层目录下。
下面以 nnom 库为例,这是调整前的 src 目录:
├───src
│ ├───core
│ │ nnom_layers.c
│ │ nnom_utils.c
│ │ nnom_tensor.c
│ │ nnom.c
│ │
│ ├───backends
│ │ nnom_local_q15.c
│ │ nnom_local.c
│ │
│ └───layers
│ nnom_lambda.c
│ ...
│ nnom_upsample.c
│
├───port
│ nnom_port.h
│
└───inc
│ nnom_local.h
│ nnom_utils.h
│ nnom_tensor.h
│ nnom_layers.h
│ nnom.h
│
└───layers
nnom_conv2d_trans.h
...
nnom_dense.h
不能简单的将 inc 里的头文件抽到 src 中,还需观察包含头文件时的路径关系,建议在 IDE 上一边编译一边按照编译报错来调整,下面是调整后的:
├───nnom
│ │ keywords.txt
│ │ library.json
│ │ library.properties
│ │ LICENSE.txt
│ │
│ └───src
│ │ nnom.c
│ │ nnom.h
│ │ ...
│ │ nnom_utils.c
│ │ nnom_utils.h
│ │
│ ├───backends
│ │ nnom_local.c
│ │ nnom_local_q15.c
│ │
│ └───layers
│ nnom_activation.c
│ nnom_activation.h
│ ...
│ nnom_zero_padding.c
│ nnom_zero_padding.h