Agentic Coding 实战:让 AI Agent 自动完成整个功能开发
Agentic Coding 实战:让 AI Agent 自动完成整个功能开发
2026年,AI 编程从「帮你补全一行代码」进化到「帮你完成整个功能」。Claude Code、Cursor Agent、GitHub Copilot Workspace——这些工具正在重新定义「写代码」这件事。本文从 C++ 开发者视角,手把手带你体验 Agentic Coding 的完整工作流。
一、什么是 Agentic Coding?
传统的 AI 编程辅助是「你问我答」模式:你写一行代码,AI 补全下一行。Agentic Coding 则是「你说需求,AI 自己干」模式:
传统模式: 开发者 → 写代码 → AI 补全 → 开发者 → 继续写
Agent模式:开发者 → 描述需求 → AI 自动规划 → AI 自动编码 → AI 自动测试 → 开发者 Review
核心区别在于:AI 不再是被动的补全工具,而是主动的任务执行者。它能:
- 理解需求 → 拆解任务
- 浏览代码库 → 理解上下文
- 自动编写多个文件
- 运行测试 → 修复 bug
- 提交代码 → 创建 PR
二、主流 Agentic Coding 工具对比
| 特性 | Claude Code | Cursor Agent | GitHub Copilot Workspace | Windsurf |
|---|---|---|---|---|
| 交互方式 | 终端 CLI | IDE 内嵌 | Web + IDE | IDE 内嵌 |
| 上下文窗口 | 200K tokens | 动态 | 动态 | 动态 |
| 多文件编辑 | ✅ 原生支持 | ✅ 支持 | ✅ 支持 | ✅ 支持 |
| 终端执行 | ✅ 可直接运行命令 | ⚠️ 有限 | ✅ 支持 | ⚠️ 有限 |
| Git 集成 | ✅ 可直接 commit/PR | ⚠️ 需手动 | ✅ 原生 GitHub 集成 | ⚠️ 需手动 |
| 自主性 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ |
| 适合场景 | 大型重构、跨文件任务 | 日常开发、快速迭代 | GitHub 项目协作 | 快速原型 |
三、实战:用 Claude Code 完成 C++ 功能开发
下面用一个真实场景演示 Agentic Coding 的完整流程。
3.1 场景描述
在一个 Qt/C++ 项目中,需要新增一个「配置热重载」功能:监控 config.json 文件变化,自动解析并应用新配置,支持回调通知。
3.2 Step 1:描述需求(自然语言)
# 在项目根目录启动 Claude Code
$ claude
> 我需要一个配置热重载功能。要求:
> 1. 监控 config.json 文件变化(使用 OS 原生文件监控)
> 2. 文件变化时自动解析 JSON 配置
> 3. 支持注册回调函数,配置变化时通知
> 4. 线程安全
> 5. 支持 Qt 项目的信号槽机制
> 6. 写完整的单元测试
3.3 Step 2:AI 自动规划
Claude Code 会先分析项目结构,然后给出实现计划:
我来分析一下项目结构,然后制定实现计划:
📁 项目分析完成:
- 使用 CMake 构建
- Qt 6.7 + C++20
- 已有 nlohmann/json 依赖
- 测试框架:GoogleTest
📋 实现计划:
1. 创建 ConfigWatcher 类(文件监控核心)
2. 实现 ConfigReloader 类(配置解析+回调管理)
3. 集成 Qt 信号槽(ConfigReloadedSignal)
4. 编写单元测试
5. 更新 CMakeLists.txt
需要我开始实现吗?
3.4 Step 3:AI 自动编码
确认后,AI 会自动创建文件、编写代码:
// config_watcher.h
#pragma once
#include <QObject>
#include <QFileSystemWatcher>
#include <QString>
#include <functional>
#include <mutex>
#include <nlohmann/json.hpp>
class ConfigWatcher : public QObject {
Q_OBJECT
public:
using ConfigCallback = std::function<void(const nlohmann::json&)>;
explicit ConfigWatcher(const QString& configPath, QObject* parent = nullptr);
~ConfigWatcher() override;
// 启动/停止监控
void start();
void stop();
// 注册配置变化回调
void onConfigChanged(ConfigCallback callback);
// 获取当前配置
nlohmann::json currentConfig() const;
// 手动触发一次配置加载
void reload();
signals:
void configReloaded(const nlohmann::json& newConfig);
private slots:
void onFileChanged(const QString& path);
private:
nlohmann::json parseConfig(const QString& path);
QFileSystemWatcher watcher_;
QString configPath_;
nlohmann::json currentConfig_;
std::vector<ConfigCallback> callbacks_;
mutable std::mutex mutex_;
};
// config_watcher.cpp
#include "config_watcher.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
ConfigWatcher::ConfigWatcher(const QString& configPath, QObject* parent)
: QObject(parent), configPath_(configPath) {
connect(&watcher_, &QFileSystemWatcher::fileChanged,
this, &ConfigWatcher::onFileChanged);
}
ConfigWatcher::~ConfigWatcher() {
stop();
}
void ConfigWatcher::start() {
// 初始加载
reload();
// 开始监控
watcher_.addPath(configPath_);
qDebug() << "ConfigWatcher: started monitoring" << configPath_;
}
void ConfigWatcher::stop() {
watcher_.removePath(configPath_);
}
void ConfigWatcher::onConfigChanged(ConfigCallback callback) {
std::lock_guard<std::mutex> lock(mutex_);
callbacks_.push_back(std::move(callback));
}
nlohmann::json ConfigWatcher::currentConfig() const {
std::lock_guard<std::mutex> lock(mutex_);
return currentConfig_;
}
void ConfigWatcher::reload() {
auto newConfig = parseConfig(configPath_);
{
std::lock_guard<std::mutex> lock(mutex_);
currentConfig_ = std::move(newConfig);
}
// 通知所有回调
for (const auto& cb : callbacks_) {
cb(currentConfig_);
}
emit configReloaded(currentConfig_);
}
void ConfigWatcher::onFileChanged(const QString& path) {
Q_UNUSED(path);
qDebug() << "ConfigWatcher: file changed, reloading...";
reload();
// 重新添加监控(某些 OS 在文件变化后会移除监控)
watcher_.addPath(configPath_);
}
nlohmann::json ConfigWatcher::parseConfig(const QString& path) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "ConfigWatcher: cannot open" << path;
return {};
}
QTextStream in(&file);
try {
return nlohmann::json::parse(in.readAll().toStdString());
} catch (const nlohmann::json::parse_error& e) {
qWarning() << "ConfigWatcher: parse error:" << e.what();
return {};
}
}
3.5 Step 4:AI 自动编写测试
// test_config_watcher.cpp
#include <gtest/gtest.h>
#include <QCoreApplication>
#include <QTemporaryFile>
#include <QDir>
#include <fstream>
#include "config_watcher.h"
class ConfigWatcherTest : public ::testing::Test {
protected:
void SetUp() override {
// 创建临时配置文件
tempDir_ = QDir::tempPath() + "/config_watcher_test";
QDir().mkpath(tempDir_);
configPath_ = tempDir_ + "/config.json";
// 写入初始配置
std::ofstream file(configPath_.toStdString());
file << R"({"key": "value1", "number": 42})";
file.close();
}
void TearDown() override {
QDir(tempDir_).removeRecursively();
}
QString tempDir_;
QString configPath_;
};
TEST_F(ConfigWatcherTest, InitialLoad) {
ConfigWatcher watcher(configPath_);
watcher.start();
auto config = watcher.currentConfig();
EXPECT_EQ(config["key"].get<std::string>(), "value1");
EXPECT_EQ(config["number"].get<int>(), 42);
}
TEST_F(ConfigWatcherTest, CallbackNotification) {
ConfigWatcher watcher(configPath_);
bool callbackCalled = false;
nlohmann::json receivedConfig;
watcher.onConfigChanged([&](const nlohmann::json& config) {
callbackCalled = true;
receivedConfig = config;
});
watcher.start();
EXPECT_TRUE(callbackCalled);
EXPECT_EQ(receivedConfig["key"].get<std::string>(), "value1");
}
TEST_F(ConfigWatcherTest, SignalNotification) {
ConfigWatcher watcher(configPath_);
bool signalReceived = false;
QObject::connect(&watcher, &ConfigWatcher::configReloaded,
[&](const nlohmann::json&) {
signalReceived = true;
});
watcher.start();
EXPECT_TRUE(signalReceived);
}
3.6 Step 5:AI 自动运行测试并修复
# AI 会自动执行
$ cmake -B build && cmake --build build
$ ./build/tests/test_config_watcher
# 如果测试失败,AI 会自动分析错误并修复
四、Agentic Coding 的最佳实践
4.1 需求描述的艺术
好的需求描述:
实现一个 ConfigWatcher 类,监控 JSON 配置文件变化。
要求:
- 使用 QFileSystemWatcher 监控文件
- 文件变化时自动解析 JSON
- 支持注册回调函数(std::function)
- 线程安全(std::mutex)
- 支持 Qt 信号槽
- 包含 GoogleTest 单元测试
差的需求描述:
帮我写个配置监控功能
4.2 上下文管理
Agentic Coding 的效果高度依赖上下文质量:
| 策略 | 效果 | 适用场景 |
|---|---|---|
| 给完整需求 + 技术约束 | ⭐⭐⭐⭐⭐ | 新功能开发 |
| 给代码片段 + 修改意图 | ⭐⭐⭐⭐ | Bug 修复、小改动 |
| 只给一句话需求 | ⭐⭐ | 简单任务 |
| 不给任何上下文 | ⭐ | 基本不可用 |
4.3 Review 是底线
Agentic Coding ≠ 不需要 Review。 AI 生成的代码必须经过人工审查:
- ✅ 逻辑正确性
- ✅ 边界条件处理
- ✅ 内存安全(特别是 C++)
- ✅ 线程安全
- ✅ 错误处理
- ✅ 代码风格一致性
- ✅ 安全漏洞
4.4 版本控制策略
# 使用 Agentic Coding 时的 Git 工作流
git checkout -b feat/config-watcher # 创建功能分支
# 让 AI 完成开发
git add -A
git commit -m "feat: add ConfigWatcher for hot-reload"
git push origin feat/config-watcher
# 创建 PR → Code Review → 合并
五、C++ 开发者的特殊考量
5.1 内存管理
AI 生成的 C++ 代码最常见的问题就是内存管理:
// ❌ AI 可能生成的代码(裸指针)
ConfigWatcher* watcher = new ConfigWatcher(path);
// 忘记 delete...
// ✅ 应该用的代码(智能指针)
auto watcher = std::make_unique<ConfigWatcher>(path);
审查重点:检查所有 new 是否有对应的 delete,优先使用 RAII 和智能指针。
5.2 现代 C++ 特性
AI 通常会使用较新的 C++ 特性,确保你的编译器支持:
// AI 可能使用 C++17/20 特性
auto config = nlohmann::json::parse(file | std::views::filter(...));
// 如果编译器不支持,需要手动降级
5.3 跨平台兼容
// AI 可能只写 Linux 版本
#include <sys/inotify.h>
// 需要补充 Windows 版本
#ifdef _WIN32
#include <windows.h>
#endif
六、效率对比:传统开发 vs Agentic Coding
| 指标 | 传统开发 | Agentic Coding | 提升 |
|---|---|---|---|
| 需求到原型 | 4-8 小时 | 30-60 分钟 | 4-8x |
| 编写样板代码 | 2-4 小时 | 5-10 分钟 | 12-24x |
| 写单元测试 | 1-2 小时 | 10-20 分钟 | 6-12x |
| Code Review | 不变 | 不变 | 0 |
| Debug 时间 | 不变或略减 | 可能增加 | - |
| 总体效率 | 基准 | 提升 3-5 倍 | 3-5x |
注意:AI 越擅长写代码,Code Review 越重要。效率提升主要在编码阶段,Review 时间会相应增加。
七、总结
Agentic Coding 不是「让 AI 替代程序员」,而是「让程序员专注于更有价值的事」:
- AI 负责:样板代码、重复逻辑、单元测试、文档生成
- 人类负责:架构设计、需求分析、Code Review、关键决策
- 共同负责:调试复杂问题、性能优化、安全审查
2026 年的 C++ 开发者,不会 Agentic Coding 就像 2010 年不会用 IDE 一样——不是不能写代码,而是效率差了一个量级。
💡 后续我会出一篇「Claude Code + C++ 项目实战:从零搭建一个完整的 CLI 工具」的详细教程,敬请关注。
参考文献:
- Anthropic: Introducing Claude Code - https://anthropic.com/claude-code
- Cursor: Agent Mode - https://cursor.sh/features/agent
- GitHub: Copilot Workspace - https://githubnext.com/projects/copilot-workspace
- Google: Gemini Code Assist - https://cloud.google.com/gemini/docs/codeassist
- C++ Core Guidelines: Resource Management - https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r-resource-management
更多推荐



所有评论(0)