Skip to content

Commit d7493c5

Browse files
author
iot-DQHoang
committed
first commit
0 parents  commit d7493c5

File tree

8 files changed

+242
-0
lines changed

8 files changed

+242
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
build/
2+
bin/
3+
lib/
4+
5+
*.exe
6+
*.out
7+
*.app
8+
9+
*.o
10+
*.obj
11+
*.a
12+
*.so
13+
*.so.*
14+
*.dylib
15+
*.dll
16+
17+
CMakeCache.txt
18+
CMakeFiles/
19+
cmake_install.cmake
20+
Makefile
21+
CTestTestfile.cmake
22+
compile_commands.json
23+
24+
.vscode/*
25+
!.vscode/settings.json
26+
!.vscode/tasks.json
27+
!.vscode/launch.json
28+
29+
*.swp
30+
*~
31+
*.bak
32+
*.log

.vscode/launch.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "GDB Remote Debug",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "${config:embed.hostBuildPath}",
9+
"args": [],
10+
"stopAtEntry": true,
11+
"cwd": "${workspaceFolder}",
12+
"environment": [],
13+
"externalConsole": false,
14+
"MIMode": "gdb",
15+
"miDebuggerPath": "/usr/bin/gdb-multiarch",
16+
"miDebuggerServerAddress": "${config:embed.targetIP}:${config:embed.debugPort}",
17+
"preLaunchTask": "Deploy and Start GDBServer",
18+
"setupCommands": [
19+
{
20+
"description": "Enable pretty-printing for gdb",
21+
"text": "-enable-pretty-printing",
22+
"ignoreFailures": true
23+
}
24+
]
25+
}
26+
]
27+
}

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"embed.targetIP": "10.152.86.71",
3+
"embed.targetUser": "root",
4+
"embed.targetPath": "/usr/local/my_app",
5+
"embed.debugPort": "2345",
6+
"embed.hostBuildPath": "${workspaceFolder}/build/my_app"
7+
}

.vscode/tasks.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "CMake Build",
6+
"type": "shell",
7+
"command": "cmake --build ${workspaceFolder}/build",
8+
"group": {
9+
"kind": "build",
10+
"isDefault": true
11+
},
12+
"problemMatcher": "$gcc"
13+
},
14+
{
15+
"label": "Deploy and Start GDBServer",
16+
"type": "shell",
17+
"dependsOn": "CMake Build", // Tự động chạy task Build trước task này
18+
"command": "scp ${config:embed.hostBuildPath} ${config:embed.targetUser}@${config:embed.targetIP}:${config:embed.targetPath} && ssh -t ${config:embed.targetUser}@${config:embed.targetIP} 'killall -q gdbserver; gdbserver :${config:embed.debugPort} ${config:embed.targetPath}'",
19+
"isBackground": true, // Quan trọng: Cho phép VS Code chạy tiếp debugger mà không đợi lệnh này kết thúc
20+
"problemMatcher": {
21+
"pattern": [
22+
{
23+
"regexp": ".",
24+
"file": 1,
25+
"location": 2,
26+
"message": 3
27+
}
28+
],
29+
"background": {
30+
"activeOnStart": true,
31+
"beginsPattern": ".",
32+
"endsPattern": "Listening on port" // VS Code sẽ đợi dòng này xuất hiện rồi mới Attach Debugger
33+
}
34+
}
35+
}
36+
]
37+
}

CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
# 2. Project Name and Details
4+
project(my_app
5+
VERSION 1.0
6+
DESCRIPTION "A standard C++ project"
7+
LANGUAGES C CXX
8+
)
9+
10+
if(NOT CMAKE_BUILD_TYPE)
11+
set(CMAKE_BUILD_TYPE Release)
12+
endif()
13+
14+
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
15+
16+
# 3. C++ Standard Settings
17+
set(CMAKE_CXX_STANDARD 17) # Use C++17
18+
set(CMAKE_CXX_STANDARD_REQUIRED ON) # Error if C++17 is not available
19+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Generates compile_commands.json (useful for VSCode/Clangd)
20+
21+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
22+
message(STATUS "Configuring for DEBUG: Optimizations disabled.")
23+
add_compile_options(-g -O0)
24+
endif()
25+
26+
file(GLOB SOURCES "src/*.cpp" "src/*.c")
27+
28+
add_executable(my_app ${SOURCES})

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Embedded Linux Remote Template
2+
3+
**A professional, automated framework for cross-compiling and remote debugging on Embedded Linux devices using VS Code, CMake, and GDB.**
4+
5+
## 📖 Overview
6+
7+
This repository provides a robust **"One-Click Debugging"** workflow for Embedded Linux development. It solves the inefficiency of manual file transfers and `printf` debugging by automating the entire pipeline:
8+
9+
1. **Cross-Compilation** (Host side).
10+
2. **Deployment** (SCP to Target).
11+
3. **Process Management** (Safe port handling on Target).
12+
4. **GDB Attachment** (Live debugging via VS Code).
13+
14+
This template is **board-agnostic** and can be adapted for any platform (Raspberry Pi, BeagleBone, i.MX, STM32MP1, Nuvoton, etc.) running `gdbserver`.
15+
16+
## 📂 Project Structure
17+
18+
```text
19+
.
20+
├── CMakeLists.txt # Main build configuration (Platform independent)
21+
├── build/ # Out-of-source build artifacts
22+
├── src/
23+
│ └── main.cpp # Application source code
24+
├── toolchain/
25+
│ └── generic.cmake # Toolchain definition (Compiler paths & flags)
26+
└── .vscode/ # VS Code Automation & Configuration
27+
├── settings.json # Project-wide variables (IPs, Paths, Users)
28+
├── tasks.json # Automation pipeline (Build -> Deploy -> Run)
29+
└── launch.json # GDB Client configuration
30+
```
31+
32+
## 🛠 Prerequisites
33+
34+
Host Machine (Developer PC)
35+
36+
- OS: Linux (Ubuntu/Debian) or Windows (WSL2).
37+
- Software:
38+
- VS Code + C/C++ Extension + CMake Tools.
39+
- gdb-multiarch (Universal GDB Client).
40+
- ssh, scp.
41+
- Target Device (Embedded Board)
42+
- Linux Kernel.
43+
- gdbserver installed. (or download from https://github.com/stayliv3/gdb-static-cross/tree/master/prebuilt)
44+
- SSH Access enabled.
45+
46+
## 🚀 Setup & Usage Guide
47+
48+
### Step 1: Clone and Configure
49+
50+
Clone this repository and update the environment variables in .vscode/settings.json to match your hardware:
51+
52+
```json
53+
// .vscode/settings.json
54+
{
55+
// --- Target Board Configuration ---
56+
"embed.targetIP": "192.168.1.100",
57+
"embed.targetUser": "root",
58+
"embed.targetPath": "/usr/local/my_app",
59+
"embed.debugPort": "2345",
60+
61+
// --- Host Configuration ---
62+
"embed.hostBuildPath": "${workspaceFolder}/build/my_app"
63+
}
64+
```
65+
66+
### Step 2: SSH Key (Passwordless Access)
67+
68+
To allow automation, the Host must access the Target without a password prompt.
69+
70+
```bash
71+
# On Host Terminal
72+
ssh-copy-id root@192.168.1.100
73+
```
74+
75+
### Step 3: Toolchain Setup (toolchain/generic.cmake)
76+
77+
Create this file to define your cross-compiler. Do not hardcode paths in CMakeLists.txt.
78+
79+
### Step 4: Run one-time for setup
80+
81+
```bash
82+
# Change your toolchain path
83+
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=./toolchain/generic.cmake -DCMAKE_BUILD_TYPE=Debug
84+
```
85+
86+
### Step 5: Start Debugging
87+
88+
1. Open src/main.cpp
89+
2. Set a Breakpoint (F9).
90+
3. Press F5.

src/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
3+
int main(int argc, char *argv[])
4+
{
5+
int a = 2;
6+
int b = 3;
7+
int c = a + b;
8+
std::cout << "c: " << c << std::endl;
9+
return 0;
10+
}

toolchain/generic.cmake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(CMAKE_SYSTEM_NAME Linux)
2+
set(CMAKE_SYSTEM_PROCESSOR arm) # or aarch64, riscv, etc.
3+
4+
# Replace these with your actual toolchain binaries
5+
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
6+
set(CMAKE_CXX_COMPILER arm-linux-gnueabihf-g++)
7+
8+
# Build settings
9+
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
10+
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
11+
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

0 commit comments

Comments
 (0)