Installation - 508364/NetLeaf GitHub Wiki
本页面提供 NetLeaf 在各平台上的详细安装说明。
- 操作系统: Windows 10 1809+, Windows 11, Windows Server 2019+
- 编译器: Visual Studio 2022 (Community/Professional/Enterprise)
- 构建工具: CMake 3.15 或更高版本
- 架构支持: x86 (Win32), x64, ARM64
下载并安装 Visual Studio 2022,确保安装以下组件:
- 使用 C++ 的桌面开发 (Desktop development with C++)
- CMake tools for Visual Studio
下载并安装 CMake,或使用 Visual Studio 内置的 CMake。
git clone https://github.com/solo-agent/NetLeaf.git
cd NetLeaf方式一:使用批处理脚本 (推荐)
# 构建所有架构 (x86, x64, ARM64)
.\build_all_windows.bat
# 或构建单个架构
.\build.bat x64 # x64
.\build.bat x86 # x86 (Win32)
.\build.bat arm64 # ARM64方式二:手动 CMake
# x64
mkdir build_x64
cd build_x64
cmake .. -G "Visual Studio 17 2022" -A x64
cmake --build . --config Release- 操作系统: Ubuntu 20.04+, Debian 11+, CentOS 8+, 或其他现代 Linux 发行版
- 编译器: GCC 8+ 或 Clang 10+
- 构建工具: CMake 3.15+, Make
- 架构支持: x64, ARM64 (计划中)
Ubuntu/Debian:
sudo apt update
sudo apt install -y build-essential cmake gitCentOS/RHEL:
sudo yum install -y gcc gcc-c++ make cmake gitgit clone https://github.com/solo-agent/NetLeaf.git
cd NetLeaf方式一:使用 Shell 脚本 (推荐)
chmod +x build_all_linux.sh
./build_all_linux.sh方式二:手动 CMake
mkdir build_linux
cd build_linux
cmake ..
make -j$(nproc)使用 WSL (Windows Subsystem for Linux):
.\build_wsl.sh- 操作系统: macOS 10.15+ (Catalina or later)
- 编译器: Xcode 12+ 或 osxcross (交叉编译)
- 构建工具: CMake 3.15+
- 架构支持: x64, ARM64 (Apple Silicon)
在 Linux/WSL 环境下使用 osxcross 工具链进行交叉编译:
# 确保 osxcross 已安装
# 参考: https://github.com/tpoechtrager/osxcross
# 构建所有 macOS 架构
chmod +x build_macos.sh
./build_macos.sh# 安装 Xcode 命令行工具
xcode-select --install
# 安装 Homebrew (如果未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 CMake
brew install cmake
# 构建项目
mkdir build_macos
cd build_macos
cmake ..
make -j$(sysctl -n hw.ncpu)- IPC 模块: 在 macOS 上不可用
- LinkAgg 模块: 在 macOS 上不可用(依赖 IPC)
- Vue 模块: 完全支持
- 其他模块: 完全支持
-
mbedTLS: 已内置于
third-party/mbedtls/目录 - 编译器: 支持 C99 标准的编译器
- 可选依赖: OpenSSL (仅在使用外部 TLS 库时需要)
方式一:使用 CMake 选项
# 构建包含 TLS 的版本
mkdir build_with_tls
cd build_with_tls
cmake .. -DBUILD_TLS=ON -DBUILD_MQTT=ON
cmake --build . --config Release方式二:修改 CMakeLists.txt
# 在项目根目录的 CMakeLists.txt 中添加
option(BUILD_TLS "Build TLS/SSL extension" ON)
option(BUILD_MQTT "Build MQTT client module" ON)编译完成后,将生成以下文件:
| 文件 | 说明 |
|---|---|
netleaf_tls.dll (Windows) |
TLS/SSL 加密扩展动态库 |
libnetleaf_tls.so (Linux) |
TLS/SSL 加密扩展共享库 |
libnetleaf_tls.dylib (macOS) |
TLS/SSL 加密扩展动态库 |
netleaf_mqtt.dll (Windows) |
MQTT 客户端模块(含 TLS) |
libnetleaf_mqtt.so (Linux) |
MQTT 客户端模块(含 TLS) |
libnetleaf_mqtt.dylib (macOS) |
MQTT 客户端模块(含 TLS) |
# 仅构建 MQTT 核心功能
mkdir build_mqtt_no_tls
cd build_mqtt_no_tls
cmake .. -DBUILD_MQTT=ON -DBUILD_TLS=OFF
cmake --build . --config Release# 构建 MQTT + TLS 完整版本
mkdir build_mqtt_full
cd build_mqtt_full
cmake .. -DBUILD_MQTT=ON -DBUILD_TLS=ON
cmake --build . --config Release| 选项 | 默认值 | 说明 |
|---|---|---|
BUILD_MQTT |
ON | 构建 MQTT 客户端模块 |
BUILD_TLS |
OFF | 构建 TLS/SSL 加密扩展(MQTT 会依赖此选项) |
BUILD_TESTING |
ON | 构建测试程序 |
BUILD_EXAMPLES |
ON | 构建示例程序 |
将 NetLeaf 作为子模块或复制到您的项目中,然后在您的 CMakeLists.txt 中添加:
# 添加 NetLeaf 子目录
add_subdirectory(path/to/NetLeaf)
# 链接到您的目标(使用动态库)
target_link_libraries(your_target PRIVATE netleaf_core)构建 NetLeaf 后,将以下文件添加到您的项目中:
| 平台 | 包含目录 | 动态库文件 |
|---|---|---|
| Windows x64 | include/ |
build_x64/bin/Release/netleaf.dll |
| Windows x86 | include/ |
build_x86/bin/Release/netleaf.dll |
| Windows ARM64 | include/ |
build_arm64/bin/Release/netleaf.dll |
| Linux x64 | include/ |
build_linux/bin/libnetleaf.so |
| macOS x64 | include/ |
build_macos_x64/bin/libnetleaf.dylib |
| macOS ARM64 | include/ |
build_macos_arm64/bin/libnetleaf.dylib |
构建完成后,运行简单服务器验证:
# Windows
cd build_x64
.\bin\Release\simple_server.exe
# Linux
cd build_wsl
./bin/simple_server
# macOS
cd build_macos_x64
./bin/simple_server访问 http://localhost:8080 确认服务器正常运行。
创建一个简单的测试文件 test.c:
#include "netleaf.h"
#include <stdio.h>
int main() {
nl_web_server_t* server = nl_web_create(8080);
if (!server) {
printf("Failed to create server\n");
return 1;
}
nl_web_add_html(server, "/", "<h1>Hello NetLeaf!</h1>");
printf("Server running on http://localhost:8080\n");
nl_web_start(server);
// 事件循环
while (1) {
nl_modules_tick();
}
nl_web_destroy(server);
nl_modules_shutdown();
return 0;
}编译并运行:
Windows (MSVC):
cl test.c /I include /link build_x64/lib/Release/netleaf.lib
.\test.exeLinux (GCC):
gcc test.c -I include -L build_wsl/lib -lnetleaf -o test
./testA: 确保 CMake 已安装并添加到系统 PATH 环境变量中。
A: 确保安装了完整的 build-essential 或开发工具包。
A: 将 CMake 构建配置改为 Debug:
cmake --build . --config Debug