插件窝 干货文章 在Linux上使用Geany进行C/C++编程的推荐配置

在Linux上使用Geany进行C/C++编程的推荐配置

配置 插件 Preferences 编译 712    来源:    2025-04-16

Geany配置指南:Linux下高效C/C++开发

基础安装

  1. 安装Geany

    sudo apt update
    sudo apt install geany
    
  2. 安装编译工具链

    sudo apt install build-essential gcc g++ make cmake gdb
    

推荐配置

1. 主题与界面优化

  • 主题:Preferences → Interface → 选择"Notepad++"或"Solarized Dark"
  • 字体:推荐使用"Monospace 10"或"DejaVu Sans Mono 10"
  • 显示行号:View → Show Line Numbers
  • 显示右侧标记栏:View → Show Markers Margin

2. 编辑器设置

  • 缩进
    • Preferences → Editor → Indentation
    • Tab宽度:4
    • 勾选"Auto-indent"和"Tab indent in leading whitespace"
  • 代码折叠:Preferences → Editor → Features → 勾选"Enable code folding"
  • 自动完成:Preferences → Editor → Completion → 勾选"Auto-complete symbols"

3. C/C++特定配置

  • 语法高亮
    • 打开C/C++文件 → Document → Set Filetype → 选择"C"或"C++"
  • 代码模板

    • 创建模板文件~/.config/geany/templates/files/file.cfile.cpp
    • 示例模板:
    /**
     * @file    ${filename}
     * @author  ${username}
     * @date    ${date}
     * @brief   ${cursor}
     */
    
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        return 0;
    }
    

4. 构建系统配置

  • 设置构建命令
    • Build → Set Build Commands
    • C编译: Compile: gcc -Wall -c "%f" -o "%e.o" Build: gcc -Wall "%f" -o "%e"
    • C++编译: Compile: g++ -Wall -std=c++11 -c "%f" -o "%e.o" Build: g++ -Wall -std=c++11 "%f" -o "%e"

5. 插件推荐

  1. 安装插件bash sudo apt install geany-plugins
  2. 推荐插件
    • Debugger:集成GDB调试器
    • GeanyVC:版本控制集成
    • Tree Browser:文件树浏览器
    • Split Window:分屏编辑

6. 调试配置

  1. 启用Debugger插件:Tools → Plugin Manager → 勾选Debugger
  2. 配置GDB
    • 在Debugger插件设置中指定gdb路径:/usr/bin/gdb
    • 设置调试命令:gdb -tui "%e"

高级技巧

  1. 自定义快捷键

    • Preferences → Keybindings
    • 推荐设置:
      • 构建:F9
      • 运行:F5
      • 调试:F8
  2. 项目管理

    • 使用Project → New创建项目
    • 添加多个文件到项目中统一管理
  3. 代码导航

    • 使用Ctrl+鼠标点击跳转到定义
    • 使用Ctrl+Shift+O打开符号浏览器
  4. 终端集成

    • 启用"Embedded Terminal"插件
    • 配置为编译后自动运行程序

常见问题解决

  1. 中文乱码

    • 修改编码:Document → Set Encoding → UTF-8
    • 或GBK(针对中文Windows文件)
  2. 头文件路径问题

    • 在构建命令中添加-I/path/to/headers
  3. 调试不工作

    • 确保编译时添加-g选项
    • 检查Debugger插件配置

通过以上配置,您可以在Linux上获得一个高效、舒适的C/C++开发环境。Geany轻量级但功能强大,特别适合中小型项目的开发。