利用Cmake及Vscode开发嵌入式linux应用

本文介绍了使用Cmake构建Linux工程的方法,包括在cmake.txt文件中指定编译器、项目名称等信息,自动生成makefile。还提及添加tasks.json和launch.json文件,最后在开发板通过NFS拷贝执行文件,启动GDB server,可便捷开发Linux应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Cmake构建linux工程
列出本人在使用的cmake文件,用于构建工程,在cmake.txt文件目录执行cmake命令自动生成makefile
#指定C编译器
SET(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
#指定C++编译器
SET(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
#SET(CMAKE_C_COMPILER gcc)
#SET(CMAKE_CXX_COMPILER g++)
#项目名称
PROJECT(main)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
#工程类型是调试版还是发布版
SET(CMAKE_BUILD_TYPE “Debug”)
#编译器选项
SET(CMAKE_CXX_FLAGS_DEBUG “$ENV{CXXFLAGS} -O0 -Wall -lpthread -g -ggdb -ldl -lm”)
#源文件目录
ADD_SUBDIRECTORY( src )
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
find_package (Threads)

ADD_EXECUTABLE(main ${DIR_SRCS} )
target_link_libraries (main KaTeX parse error: Expected 'EOF', got '#' at position 60: …IES( main src) #̲mqtt目录及连接库路径 IN…{file}",
// “-o”,
// “fileDirname/{fileDirname}/fileDirname/{fileBasenameNoExtension}”
// “-lpthread”,
],
“options”: {
“cwd”: “KaTeX parse error: Expected 'EOF', got '}' at position 23: …aceFolder}" }̲, "problemMa…gcc”
],
“group”: {
“kind”: “build”,
“isDefault”: true
}
}
]
}

添加launch.json文件
{
“version”: “0.2.0”,
“configurations”: [
{
“name”: “test”,
“type”: “cppdbg”,
“request”: “launch”,
“program”: “workspaceFolder/build/main","args":[],"stopAtEntry":false,"cwd":"{workspaceFolder}/build/main", "args": [], "stopAtEntry": false, "cwd": "workspaceFolder/build/main","args":[],"stopAtEntry":false,"cwd":"{workspaceFolder}”,
“environment”: [],
“externalConsole”: true,
“MIMode”: “gdb”,
“setupCommands”: [
{
“description”: “Enable pretty-printing for gdb”,
“text”: “-enable-pretty-printing”,
“ignoreFailures”: true
}
],
//“preLaunchTask”: “C/C++: gcc build active file”,
//本地使用的GDB调试器
“miDebuggerPath”: “arm-linux-gnueabihf-gdb”,
//GDB server的地址与端口
“miDebuggerServerAddress”: “192.168.1.108:6666”
}
]
}

在开发板通过NFS拷贝编译好的执行文件,并启动GDB server,就可以像Visaul studio一样简单好用的开发linux应用程序

<think>我们参考了多个引用资料,特别是引用[3]和引用[4]中关于在Linux下配置VSCode进行嵌入式开发的步骤。以下将分步骤详细说明如何在Linux系统中配置Visual Studio Code进行嵌入式开发。 ### 1. 安装Visual Studio Code 根据引用[3],首先需要安装Visual Studio Code。 - 访问[VS Code官网](https://code.visualstudio.com/)下载适用于Linux的安装包(.deb或.rpm,或压缩包)。 - 或者使用包管理器安装(以Ubuntu为例): ```bash sudo apt update sudo apt install code ``` ### 2. 安装必要扩展 在VS Code中安装以下扩展(引用[3]和引用[4]): - **C/C++**:由Microsoft提供,用于C/C++语言支持。 - **Cortex-Debug**:用于ARM Cortex-M微控制器的调试支持。 - 其他可能需要的扩展(如Python支持,因为pyOCD需要Python环境)。 安装步骤: 1. 打开VS Code。 2. 点击左侧活动栏的扩展图标(或按`Ctrl+Shift+X`)。 3. 搜索扩展名称,点击安装。 ### 3. 安装工具链 根据引用[3],需要安装GNU ARM嵌入式工具链和调试工具(如pyOCD): - **GNU ARM嵌入式工具链**:用于编译ARM架构的嵌入式代码。 - 下载地址:https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm - 选择适合Linux的版本,解压并添加到环境变量中。例如: ```bash # 假设解压到/opt/gcc-arm-none-eabi echo 'export PATH=$PATH:/opt/gcc-arm-none-eabi/bin' >> ~/.bashrc source ~/.bashrc ``` - **pyOCD**:一个开源的ARM Cortex-M调试器(引用[3])。 - 使用pip安装: ```bash pip install pyocd ``` ### 4. 配置项目 1. 打开项目文件夹(`File` > `Open Folder`)。 2. 创建`.vscode`文件夹,并在其中创建以下配置文件(引用[3][4]): - **c_cpp_properties.json**:配置C/C++编译路径和宏定义。 - **tasks.json**:定义构建任务。 - **launch.json**:配置调试设置。 #### 示例配置 **c_cpp_properties.json**(根据你的芯片和项目调整): ```json { "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/opt/gcc-arm-none-eabi/arm-none-eabi/include/**" // 根据实际路径调整 ], "defines": [], "compilerPath": "/opt/gcc-arm-none-eabi/bin/arm-none-eabi-gcc", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 } ``` **tasks.json**(定义构建任务): ```json { "version": "2.0.0", "tasks": [ { "label": "Build", "type": "shell", "command": "make", // 或者使用你的构建脚本,如cmake或直接调用gcc "args": [], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [] } ] } ``` **launch.json**(调试配置,以pyOCD为例): ```json { "version": "0.2.0", "configurations": [ { "name": "Cortex Debug", "cwd": "${workspaceRoot}", "executable": "${workspaceRoot}/build/project.elf", // 替换为你的elf文件路径 "request": "launch", "type": "cortex-debug", "servertype": "pyocd", "device": "nrf52840", // 根据你的目标芯片修改 "runToEntryPoint": "main" } ] } ``` ### 5. 连接调试器 将开发板通过USB连接到Linux电脑,并确保系统识别了调试器(如CMSIS-DAP、J-Link等)。通常需要安装相关驱动(引用[3]): - 对于CMSIS-DAP,Linux通常自带驱动,使用`lsusb`命令检查设备是否被识别。 ### 6. 编译与调试 - 编译:按`Ctrl+Shift+B`执行构建任务(在tasks.json中定义)。 - 调试:切换到调试视图(左侧活动栏的虫子图标),选择配置并点击运行。 ### 注意事项 - 不同开发板和芯片需要调整配置(如launch.json中的`device`字段)。 - 如果使用其他调试器(如J-Link),需要安装对应的软件(如J-Link软件包)并修改`servertype`为`jlink`(引用[4])。 通过以上步骤,即可在Linux系统中使用Visual Studio Code进行嵌入式开发。 §§相关问题§§ 1. 如何解决在LinuxVSCode调试嵌入式程序时出现的设备连接问题? 2. 除了pyOCD,还有哪些常用的嵌入式调试工具可以在VSCode中使用? 3. 如何在VSCode中配置多目标平台(如不同ARM芯片)的构建任务?</think>### 在Linux系统中配置Visual Studio Code进行嵌入式开发 以下是分步配置指南,结合引用资料[3][4][5]的最佳实践: --- #### 1. 安装基础软件 ```bash # 安装VS Code sudo apt update && sudo apt install code # 安装Git(用于版本控制) sudo apt install git # 安装编译工具链(以ARM为例) sudo apt install gcc-arm-none-eabi binutils-arm-none-eabi ``` --- #### 2. 安装必要扩展 在VS Code扩展商店安装: 1. **C/C++** (Microsoft官方扩展) ![C/C++扩展截图](https://example.com/cpp-extension.png)[^3] 2. **Cortex-Debug** (ARM调试支持) 3. **CMake Tools** (可选,用于CMake项目) 4. **Python** (支持pyOCD等工具)[^3] > 💡 提示:扩展安装后需重新加载窗口生效 --- #### 3. 配置工具链路径 创建配置文件 `~/.vscode/c_cpp_properties.json`: ```json { "configurations": [ { "name": "Linux", "includePath": [ "/usr/arm-none-eabi/include/**", "${workspaceFolder}/**" ], "defines": [], "compilerPath": "/usr/bin/arm-none-eabi-gcc", "cStandard": "c11", "cppStandard": "c++17" } ], "version": 4 } ``` > 注:路径需根据实际安装位置调整[^4] --- #### 4. 配置调试环境 1. **安装调试工具**: ```bash pip install pyOCD # 开源调试器[^3] sudo apt install openocd # 备选方案 ``` 2. 创建 `launch.json`: ```json { "version": "0.2.0", "configurations": [ { "name": "Cortex Debug", "cwd": "${workspaceRoot}", "executable": "${workspaceFolder}/build/firmware.elf", "request": "launch", "type": "cortex-debug", "servertype": "pyocd", "device": "STM32F407VG" // 修改为你的芯片型号 } ] } ``` --- #### 5. 配置构建任务 创建 `tasks.json`: ```json { "version": "2.0.0", "tasks": [ { "label": "Build Firmware", "type": "shell", "command": "make -j4", "group": "build", "problemMatcher": ["$gcc"] } ] } ``` > 按 `Ctrl+Shift+B` 触发构建[^4] --- #### 6. 硬件连接验证 ```bash # 查看设备是否识别 pyocd list # 输出示例 [ INFO ] Available debug probes: [ INFO ] 1: 0483:374b (STLink V3) [ST-LINK] ``` --- #### 7. 推荐工作流 1. 使用 **CMake** 管理项目结构 2. 启用 **clangd** 扩展获得智能提示 3. 安装 **Serial Monitor** 扩展查看串口输出 4. 使用 **GitLens** 进行版本控制 > 注意:不同开发板需调整芯片型号和链接脚本[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纵向深耕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值