文章目录

  • Ubuntu系统使用快速入门实践系列文章
  • Ubuntu系统使用快速入门实践系列文章总链接
  • Ubuntu系统使用快速入门实践系列文章专栏
  • 前言
  • Ubuntu系统使用快速入门实践(七)——软件安装与使用(4)
  • PyCharm安装
  • 社区版安装(免费)
  • 设置中文
  • Clion安装
  • CLion 配置
  • 汉化
  • 配置CLion的一些设置
  • 使用配置
  • 批量重命名函数和参数
  • 设置文件模板
  • 设置实时模板
  • 生成操作
  • 键位映射
  • 代码配色方案
  • 重新格式化代码
  • Debug调试
  • 表达式计算器
  • 性能分析



前言

Ubuntu是一个以桌面应用为主的Linux发行版操作系统,也是大多数人第一个接触到的Linux系统,尤其是从事理工科研究工作的人,这个系列的文章主要讲述如何使用Ubuntu系统,完成日常的学习、科研以及工作


Ubuntu系统使用快速入门实践(七)——软件安装与使用(4)

PyCharm安装

官网:https://www.jetbrains.com/zh-cn/pycharm/

社区版安装(免费)

下载社区版PyCharm,然后安装安装包

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android

下载结束后,提取文件

打开终端,输入 cd bin命令 ,进入刚解压文件夹下的 bin 文件夹,执行 ./pycharm.sh命令,开始安装pycharm,出现下面的界面后按continue,等待安装完成

ubuntu系统安装android Ubuntu系统安装软件_linux_02

ubuntu系统安装android Ubuntu系统安装软件_linux_03

在左下角点击设置选项,创建桌面快捷方式

设置中文

选择File->Setting->Plugins->搜索中文,然后安装即可

ubuntu系统安装android Ubuntu系统安装软件_linux_04

安装完成后重启

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_05

Clion安装

官网:https://www.jetbrains.com/zh-cn/clion/download/#section=linux

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_06

可以下载安装包安装,也可以用命令安装

sudo snap install clion --classic

ubuntu系统安装android Ubuntu系统安装软件_系统使用_07

安装完成后,禁止自动更新

sudo snap refresh --hold clion

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_08

如果需要更新,可以解禁

sudo snap refresh --unhold clion

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_09

进入bin文件夹,执行clion.sh

cd bin
./clion.sh

ubuntu系统安装android Ubuntu系统安装软件_系统使用_10

和pycharm用相同的激活码即可

ubuntu系统安装android Ubuntu系统安装软件_linux_11

但是用安装包的CLion必须从文件夹中打开,并不能像Pycharm一样可以在程序列表中找到

ubuntu系统安装android Ubuntu系统安装软件_系统使用_12

CLion 配置
汉化

安装完成后,先汉化

点击Plugins插件——>输入中文,搜索到Chinese(simplified),点击install安装

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_13

或者点击File,然后选择setting,然后选择Plugin,再进行安装

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_14

汉化完成后如下所示

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_15

配置CLion的一些设置

CLion是一款开发环境IDE,它不具有编译环境,所以需要安装C/C++的编译环境。

点击自定义下面的所有设置

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_16

或者点击文件->设置

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_17

点击构建、执行、部署中的工具链,可以看到编译器的信息

ubuntu系统安装android Ubuntu系统安装软件_系统使用_18

先找到gcc、g++和cmake的路径

ubuntu系统安装android Ubuntu系统安装软件_linux_19

再将其填入相应的位置

ubuntu系统安装android Ubuntu系统安装软件_linux_20

点击右上角的 Add Configuration ,添加配置。

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_21

然后点击加号,选择CMake调试

ubuntu系统安装android Ubuntu系统安装软件_linux_22

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_23

使用配置

打开项目后,点击文件->设置->构建、执行、部署->CMake,来配置CMake的参数

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_24

点击环境,由于之前已经 source 过 setup.bash 了,所以这里显示了环境变量

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_25

如果没有source的话,使用命令打印环境变量

printenv

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_26

CMAKE_PREFIX_PATH 这一项复制

CMAKE_PREFIX_PATH=/opt/ros/noetic

然后添加进去

但是这样一个一个复制比较麻烦,选中所有的环境变量,然后点击Clion中的粘贴,会自动分隔开,再点加号即可添加

ubuntu系统安装android Ubuntu系统安装软件_系统使用_27

这几个按钮分别表示:定位当前打开的文件位置(常用),展开所有文件,收起所有文件

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_28

点击工具链配置开发选项,我这里新建了一个配置命名为myconfig

ubuntu系统安装android Ubuntu系统安装软件_系统使用_29

查看cmake在哪里,然后设置其路径

ubuntu系统安装android Ubuntu系统安装软件_linux_30

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_31

可以看到选择的cmake版本和默认的版本不一样,如果需要的话,下面的编译器也可以自己选择

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_32

配置好之后,选择右上角的编辑配置,然后选择最外层的CMakeList.txt文件,就可以编译 catkin_ws 文件夹内的项目了

编译好的文件在 catkin_ws/devel/lib 中的各个文件夹中中,

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_33

例如VINS编译后,都存在 catkin_ws/devel/lib/vins

ubuntu系统安装android Ubuntu系统安装软件_系统使用_34

批量重命名函数和参数

如果想要将一个函数重命名,且连同引用这个函数的地方全部一起重命名,可以点击函数或者某个变量,按shift + F6,即可全部重命名

ubuntu系统安装android Ubuntu系统安装软件_linux_35

如果还想修改函数的返回值和变量,则按 ctrl + F6 来进行设置

ubuntu系统安装android Ubuntu系统安装软件_linux_36

设置文件模板

点击设置->文件和代码模板,可以在这里设置新建某种后缀的文件时的模板

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_37

C File Header.h 的设置可以被其他模板引用,我的这个文件的模板代码如下

#if ($HEADER_COMMENTS)
//
// Created by $USER_NAME on ${DATE}.
#if ($ORGANIZATION_NAME && $ORGANIZATION_NAME != "")
// Copyright (c) $YEAR ${ORGANIZATION_NAME}#if (!$ORGANIZATION_NAME.endsWith(".")).#end All rights reserved.
#end
// author email: yangtsejin@gmail.com
#end

这时新建一个.cpp文件,就会如下所示

ubuntu系统安装android Ubuntu系统安装软件_linux_38

在新建cpp类文件时,其类型可以自定义,还可以选择将其添加到哪个CMakeLists.txt里面,比如这里可以添加到 vins_lib 中

ubuntu系统安装android Ubuntu系统安装软件_linux_39

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_40

设置 .c/cpp 的模板

设置 .c/cpp 文件的模板,这样在编程时可以省去很多麻烦

设置 C Source File 中的模板

#parse("C File Header.h") 
#if (${HEADER_FILENAME})
#[[#include]]# "${HEADER_FILENAME}"
#end

#[[#include]]# <iostream>

using namespace std;

int main(int argc,char** argv) {

    return 0;
}

#parse("C File Header.h") 表示继承 C File Header.h的设置

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_41

设置好之后新建.c/cpp 源文件如下所示

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_42

设置 CMakeLists.txt 的模板

ubuntu系统安装android Ubuntu系统安装软件_linux_43


比如我这里设置为

cmake_minimum_required(VERSION 2.8)
project(${name})

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wall -g")

IF(NOT CMAKE_BUILD_TYPE)
  SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE("Build type: " #[[${CMAKE_BUILD_TYPE}]]#)

set(HEAD_PATH #[[${PROJECT_SOURCE_DIR}]]#/include)
set(SRC_PATH #[[${PROJECT_SOURCE_DIR}]]#/src)
set(LIB_PATH #[[${PROJECT_SOURCE_DIR}]]#/lib)
set(EXEC_PATH #[[${PROJECT_SOURCE_DIR}]]#/bin)

# 定义库的名称
set(NAME_LIB ${name})

# 搜索指定目录下源文件,两种方法选一个即可
file(GLOB SRC_LIST #[[${SRC_PATH}]]#/*.cpp)
aux_source_directory(#[[${SRC_PATH}]]# SRC_LIST)

# 头文件路径
include_directories(#[[${HEAD_PATH}]]#)
# 库文件的输出路径
set(LIBRARY_OUTPUT_PATH #[[${LIB_PATH}]]#)
# 二进制可执行文件的输出路径
set(EXECUTABLE_OUTPUT_PATH ${EXEC_PATH})

# 生成共享库
add_library(NAME_LIB  SHARED  #[[${SRC_LIST}]]#)
# 生成静态库
# add_library(NAME_LIB  STATIC  #[[${SRC_LIST}]]#)

# 添加子目录
# add_subdirectory(subdir_name)

# 设置可执行文件名
set(BIN_NAME proj)
# 添加可执行文件
# add_executable(#[[${BIN_NAME}]]# #[[${SRC_LIST}]]#)
# target_link_libraries(#[[${BIN_NAME}]]# #[[${NAME_LIB}]]#)

生成的模板如下

ubuntu系统安装android Ubuntu系统安装软件_系统使用_44

设置实时模板

点击设置->实时模板,可以设置实时的模板,比如添加namespace的模板,

ubuntu系统安装android Ubuntu系统安装软件_linux_45

这时建立一个命名空间即如下所示

namespace $name$
{

}   //namespace $name$

ubuntu系统安装android Ubuntu系统安装软件_linux_46

还可以添加一个自动生成主函数的模板

int main(int argc,char** argv)
{
    
}

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_47

生成操作

Alt + Insert 或者点击鼠标右键选择生成,可以有很多快捷操作,如建立构造函数、析构函数、get和set方法

同时也可以用来生成类函数的实现、声明

ubuntu系统安装android Ubuntu系统安装软件_系统使用_48

键位映射

可以按自己的习惯设置键位映射

ubuntu系统安装android Ubuntu系统安装软件_系统使用_49

这里设置前后跳转的快捷键时,可能会和Ubuntu系统的快捷键冲突,比如 ctrl + alt + left 是向后跳,但是在Ubuntu中表示切换工作区

JetBrain 的 IDE 在追踪代码的时候,常用的快捷键是 Ctrl+Alt+LeftCtrl+Alt+Right,而这两组快捷键正好和 gnome3 切换 WorkSpace 的快捷键冲突,导致在 IDE 中以上快捷键不可用。当然,gnome3 默认桌面只能上下切换工作空间,所以默认情况下,我们使用上述两组快捷键,没有任何效果。不过如果我们使用 Ctrl+Alt+Up 和 Ctrl+Alt+Down 的时候是可以上下切换 WorkSpace 的。

查看快捷键

gsettings get org.gnome.desktop.wm.keybindings switch-to-workspace-left

ubuntu系统安装android Ubuntu系统安装软件_linux_50

屏蔽快捷键

# 屏蔽Ubuntu以下4组快捷键

gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-left "['']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-right "['']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-up "['']"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-down "['']"

如果要恢复,可以使用以下命令

gsettings reset org.gnome.desktop.wm.keybindings

设置鼠标滚动调整字体大小

设置鼠标滚动调整字体大小,设置完后,按住 crtl 键,滚动滚轮可以调整字体大小

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_51

代码配色方案

导入已有的配色方案

https://github.com/marrblue/VS-style-theme-for-clion

https://github.com/dinbtechit/vscode-theme

或者在插件中心中下载主题

ubuntu系统安装android Ubuntu系统安装软件_linux_52

样式如下,和VSCODE配色一样

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_53

重新格式化代码

ctrl + alt +L 或者选择 代码-> 重新格式化代码,即可将代码的风格统一

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_54

例如

ubuntu系统安装android Ubuntu系统安装软件_系统使用_55

重新格式化后

ubuntu系统安装android Ubuntu系统安装软件_系统使用_56

Debug调试

点击小甲虫开始调试

ubuntu系统安装android Ubuntu系统安装软件_系统使用_57

点击两个红色圆圈可以查看所有打上的断点

ubuntu系统安装android Ubuntu系统安装软件_系统使用_58


ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_59

程序的参数设为

/home/jin/catkin_ws/src/VINS-Fusion-ste/config/euroc/euroc_stereo_imu_config.yaml

先开始跑包输入数据

rosbag play /home/jin/catkin_ws/src/dataset/MH_01_easy.bag

这个时候开始Debug,就停在了断点处

ubuntu系统安装android Ubuntu系统安装软件_系统使用_60

如果要查看某个变量,点击右边的加号,输入变量名

ubuntu系统安装android Ubuntu系统安装软件_linux_61

表达式计算器

打开表达式计算器,可以求得某个表达式的返回值

ubuntu系统安装android Ubuntu系统安装软件_linux_62

比如查看 Image0 的返回结果

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_63

性能分析

详细的可见CLion官方文档:https://www.jetbrains.com/help/clion/2023.2/cpu-profiler.html

ubuntu系统安装android Ubuntu系统安装软件_linux_64

(1)先安装 Perf 工具

# 输入命令安装linux-tools-generic
sudo apt install linux-tools-$(uname -r) linux-tools-generic

# 或者按官网给出的命令
uname -r
4.15.0-36-generic
sudo apt-get install linux-tools-4.15.0-36-generic

ubuntu系统安装android Ubuntu系统安装软件_系统使用_65


ubuntu系统安装android Ubuntu系统安装软件_ubuntu_66

安装完成后使用命令查看perf版本

perf --version

ubuntu系统安装android Ubuntu系统安装软件_ubuntu_67

ubuntu系统安装android Ubuntu系统安装软件_linux_68

设置perf分析器的路径

ubuntu系统安装android Ubuntu系统安装软件_linux_69

(2)调整内核选项

  • perf_event_paranoid - 控制非 root 用户对性能事件数据的使用。
    将值设置为小于 2,以便分析器在没有 root 权限的情况下收集性能信息:
sudo sh -c 'echo 1 >/proc/sys/kernel/perf_event_paranoid'

您可以在内核文档中找到可能值的描述。通常,10足以让探查器运行并收集数据。但是,如果您得到空的分析结果(无分析器数据消息),则您的系统设置可能需要-1- 最不安全的选项,该选项允许所有用户使用所有性能事件。

  • kptr_restrict - 设置对暴露内核地址的限制。
    要正确解析内核符号,请将 kptr_restrict的值设置为 0 来禁用kptr_restrict提供的保护:
sudo sh -c 'echo 0 >/proc/sys/kernel/kptr_restrict'

默认情况下,这些更改仅影响您当前的操作系统会话。要在系统重新启动后保留设置,请运行:

sudo sh -c 'echo kernel.perf_event_paranoid=1 >> /etc/sysctl.d/99-perf.conf'
sudo sh -c 'echo kernel.kptr_restrict=0 >> /etc/sysctl.d/99-perf.conf'
sudo sh -c 'sysctl --system'

首次启动探查器时,CLion 会检查内核变量是否已设置并建议必要的更改:

ubuntu系统安装android Ubuntu系统安装软件_linux_70

(3)准备构建(不设置也能用)

  1. 分析器依赖调试信息来提供有意义的输出数据和导航,因此调试配置更适合用于分析。
  2. 编译器优化(例如内联)可能会影响分析结果。为了确保不会因内联而丢失任何帧,请在CMakeLists.txt-O0中将优化级别设置为:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")

此外,编译器可以使用帧指针寄存器作为通用寄存器来实现优化目的,这可能会导致堆栈跟踪损坏。在Linux 上,分析器实现不依赖于此,但在 macOS 上,我们建议-fno-omit-frame-pointer为gcc以及两者-fno-omit-frame-pointer-mno-omit-leaf-frame-pointerclang设置编译标志。

点击分析工具开始运行程序

ubuntu系统安装android Ubuntu系统安装软件_ubuntu系统安装android_71

然后点击停止并分析,这个时候就可以看到分析结果

ubuntu系统安装android Ubuntu系统安装软件_Ubuntu_72