温馨提示×

Ubuntu怎么配C++插件

小樊
51
2026-10-02 10:36:51
栏目: 编程语言

你说的“Ubuntu 配 C++ 插件”通常有几种常见含义,我分别说明,你可以对号入座:


一、VS Code 配置 C++ 开发环境(最常见)

如果你是在 Ubuntu 上使用 VS Code 写 C++,步骤如下:

1. 安装基础编译工具

sudo apt update
sudo apt install build-essential gdb

验证:

gcc --version
g++ --version

2. 安装 VS Code(如果还没装)

sudo apt install code

或从官网下载 .deb 安装。

3. 安装 C++ 插件

在 VS Code 扩展商店搜索并安装:

  • C/C++(Microsoft 官方)
  • 可选:C/C++ Themes
  • 可选:CMake Tools(如果用 CMake)

4. 配置 tasks.json(编译)

按 Ctrl+Shift+P → Tasks: Configure Task → g++ build

示例:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "type": "shell",
      "command": "g++",
      "args": ["-g", "main.cpp", "-o", "main"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

5. 配置 launch.json(调试)

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C++ Debug",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/main",
      "miDebuggerPath": "/usr/bin/gdb",
      "preLaunchTask": "build"
    }
  ]
}

二、把 C++ 写成“插件”(动态库 .so)

如果你是写 C++ 插件给主程序调用:

1. 写插件代码

// plugin.cpp
extern "C" {
    void hello() {
        printf("Hello from plugin!\n");
    }
}

2. 编译成动态库

g++ -shared -fPIC plugin.cpp -o libplugin.so

3. 主程序加载

#include <dlfcn.h>

void* handle = dlopen("./libplugin.so", RTLD_LAZY);
void (*hello)() = (void(*)())dlsym(handle, "hello");
hello();
dlclose(handle);

编译主程序:

g++ main.cpp -ldl -o main

三、给某个软件装 C++ 插件(如 Vim / CLion / Qt)

如果你指的是某个具体软件(比如 Vim、Qt Creator、CLion、Unreal、ROS),告诉我名字,我可以给你专门步骤。


你可以告诉我:

  • 用的是 VS Code / Vim / CLion / Qt?
  • 是写代码环境还是做动态插件?

我可以直接给你最短可跑方案。

0 踩