在 Ubuntu 上配置 C++ 插件,通常取决于你指的是哪一类“插件”。下面我按最常见的几种情况给你说明,你可以对照使用。
这是最常见的情况。
sudo apt update
sudo apt install code
或者从官网下载:https://code.visualstudio.com/
打开 VS Code → 扩展(Ctrl + Shift + X) → 搜索并安装:
✅ C/C++(Microsoft 官方)
✅ C/C++ Extension Pack(推荐)
sudo apt update
sudo apt install build-essential gdb
检查:
gcc --version
g++ --version
mkdir cpp-test
cd cpp-test
code .
创建 main.cpp:
#include <iostream>
using namespace std;
int main() {
cout << "Hello Ubuntu C++" << endl;
return 0;
}
tasks.json(编译)按 Ctrl + Shift + P → 输入:
Tasks: Configure Task
选择:
Create tasks.json from template → Others
修改为:
{
"version": "2.0.0",
"tasks": [
{
"label": "build c++",
"type": "shell",
"command": "g++",
"args": [
"-g",
"main.cpp",
"-o",
"main"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
编译:Ctrl + Shift + B
launch.json(调试)按 F5 → 选择 C++ (GDB/LLDB)
修改:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
请告诉我:
我可以给你一个 最小可运行插件示例。
// plugin.cpp
extern "C" {
void plugin_run() {
std::cout << "Plugin running!" << std::endl;
}
}
编译:
g++ -fPIC -shared plugin.cpp -o libplugin.so
加载(dlopen):
dlopen("./libplugin.so", RTLD_NOW);
| 问题 | 解决方案 |
|---|---|
| VS Code 找不到 g++ | 安装 build-essential |
| 无法调试 | 检查 launch.json 的 program 路径 |
| 头文件红色报错 | 安装 sudo apt install clang 或重启 VS Code |
Ubuntu 配置 C++ 插件 = 装编译环境 + IDE 插件 + 配置文件
如果你能说明:
我可以给你 更精确的配置方案。