4.1 常用调试命令参数
## 以下命令后括号内为命令的简化使用,比如 run( r ),直接输入命令r 就代表命令run(gdb) help (h) # 查看命令帮助,具体命令查询在 gdb 中输入 help + 命令。(gdb) run(r) # 重新开始运行文件( run-text :加载文本文件, run-bin :加载二进制文件)$(gdb) start # 单步执行,运行程序,停在第一行执行语句(gdb) list(l) # 查看原代码( list-n, 从第 n 行开始查看代码。 list+ 函数名:查看具体函数)(gdb) set # 设置变量的值(gdb) next(n) # 单步调试(逐过程,函数直接执行)(gdb) step(s) # 单步调试(逐语句:跳入自定义函数内部执行)(gdb) backtrace(bt) # 查看函数的调用的栈帧和层级关系(gdb) frame(f) # 切换函数的栈帧(gdb) info(i) # 查看函数内部局部变量的数值(gdb) finish # 结束当前函数,返回到函数调用点(gdb) continue(c) # 继续运行(gdb) print(p) # 打印值及地址(gdb) quit(q) # 退出 gdb(gdb) break + num(b) # 在第 num 行设置断点(gdb) info breakpoints # 查看当前设置的所有断点(gdb) delete breakpoints num(d) # 删除第 num 个断点(gdb) display # 追踪查看具体变量值(gdb) undisplay # 取消追踪观察变量(gdb) watch # 被设置观察点的变量发生修改时,打印显示(gdb) i watch # 显示观察点(gdb) enable breakpoints # 启用断点(gdb) disable breakpoints # 禁用断(gdb) x # 查看内存 x/20xw 显示20个单元, 16 进制, 4 字节每单元(gdb) run argv[1] argv[2](gdb) set follow-fork-mode child.
4.2 【实战】命令行调试
#include <iostream>
using namespace std;
int main(int argc,char **argv)
{int N = 100;int sum = 0;int i = 1;// calculate sum from 1 to 100while (i <= N){sum = sum + i;i = i + 1;}cout << "sum = " << sum << endl;cout << "The program is over." << endl;return 0;
} 


5.1 界面介绍

![]()
5.2 插件安装
5.3 快捷键


第六讲:CMake
前言:
6.1 Cross-platform development


Same action - adding new bar.cpp file, will be done in one step now:

6.2 语法特性介绍
set(HELLO hello.cpp)
add_executable(hello main.cpp hello.cpp)
ADD_EXECUTABLE(hello main.cpp ${HELLO}) 6.3 重要指令和CMake常用变量
6.3.1 重要指令
# CMake最小版本要求为2.8.3
cmake_minimum_required(VERSION 2.8.3) # 指定工程名为HELLOWORLD
project(HELLOWORLD) # 定义SRC变量,其值为sayhello.cpp hello.cpp
set(SRC sayhello.cpp hello.cpp) nclude_directories - 向工程添加多个特定的头文件搜索路径 --->相当于指定g++编译器的-I参数
语法: include_directories([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) # 将/usr/include/myincludefolder 和 ./include 添加到头文件搜索路径
include_directories(/usr/include/myincludefolder ./include) # 将/usr/lib/mylibfolder 和 ./lib 添加到库文件搜索路径
link_directories(/usr/lib/mylibfolder ./lib) # 通过变量 SRC 生成 libhello.so 共享库
add_library(hello SHARED ${SRC}) add_compile_options - 添加编译参数
语法:add_compile_options(
# 添加编译参数 -Wall -std=c++11 -O2
add_compile_options(-Wall -std=c++11 -O2
) 语法:add_executable(exename source1 source2 ... sourceN) # 编译main.cpp生成可执行文件main
add_executable(main main.cpp) # 将hello动态库文件链接到可执行文件main
target_link_libraries(main hello) # 添加src子目录,src中需有一个CMakeLists.txt
add_subdirectory(src) 语法: aux_source_directory(dir VARIABLE) # 定义SRC变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC)
# 编译SRC变量所代表的源代码文件,生成main可执行文件
add_executable(main ${SRC}) 6.3.2 CMake常用变量
# 在CMAKE_CXX_FLAGS编译选项后追加-std=c++11
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") # 设定编译类型为debug,调试时需要选择debug
set(CMAKE_BUILD_TYPE Debug)
# 设定编译类型为release,发布时需要选择release
set(CMAKE_BUILD_TYPE Release) 1. 这三个变量指代的内容是一致的。
2. 如果是 in source build,指的就是工程顶层目录。
3. 如果是 out-of-source 编译,指的是工程编译发生的目录。
# 编译main.cpp生成可执行文件main
add_executable(main main.cpp)
# 将hello动态库文件链接到可执行文件main
target_link_libraries(main hello)
# 添加src子目录,src中需有一个CMakeLists.txt
add_subdirectory(src)
# 定义SRC变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC)
# 编译SRC变量所代表的源代码文件,生成main可执行文件
add_executable(main ${SRC})
# 在CMAKE_CXX_FLAGS编译选项后追加-std=c++11
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# 设定编译类型为debug,调试时需要选择debug
set(CMAKE_BUILD_TYPE Debug)
# 设定编译类型为release,发布时需要选择release
set(CMAKE_BUILD_TYPE Release) 4. PROJECT_BINARY_DIR 跟其他指令稍有区别,不过现在,你可以理解为他们是一致
的。 CMAKE_SOURCE_DIR
PROJECT_SOURCE_DIR
_SOURCE_DIR
1. 这三个变量指代的内容是一致的,不论采用何种编译方式,都是工程顶层目录。
2. 也就是在 in source build时,他跟 CMAKE_BINARY_DIR 等变量一致。
3. PROJECT_SOURCE_DIR 跟其他指令稍有区别,现在,你可以理解为他们是一致的。 6.4 CMake编译工程
6.4.1 编译流程
6.4.2 两种构建方式
## 内部构建
# 在当前目录下,编译本目录的CMakeLists.txt,生成Makefile和其他文件
cmake .
# 执行make命令,生成target
make ## 外部构建
# 1. 在当前目录下,创建build文件夹
mkdir build
# 2. 进入到build文件夹
cd build
# 3. 编译上级目录的CMakeLists.txt,生成Makefile和其他文件
cmake ..
# 4. 执行make命令,生成target
make 6.5 【实战】CMake代码实践
6.5.1 最小CMake工程(5.3.1 目录)
目录结构

helloworld.cpp
#include <algorithm>
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
using namespace std;int main(int argc, char *argv[]) {cout<<"hello world"<<endl;return 0; } CMakeLists.txt
# Set the minimum version of CMake that can be used
cmake_minimum_required(VERSION 3.0)
# Set the project name
project (HELLO)
# Add an executable
add_executable(hello_cmake main.cpp) #命令 mkdir build ->cd build -> cmake ..(build 文件下生成makefile 内容) ->make 6.5.2 多目录工程 - 直接编译(5.3.2)
目录

include 文件下
include/swap.h
#pragma once
#include <iostream>
using namespace std;class swap11 {
private:int _a;int _b;public:void run();void printInfo();swap11(int a, int b) {this->_a = a;this->_b = b;}
};
src/
#include "../include/swap.h"
// #include"swap.h"void swap11::run(){int temp;temp=_a;_a=_b;_b=temp;}void swap11::printInfo(){cout<<"_a:"<<_a<<endl;cout<<"_b:"<<_b<<endl;} CMakeLists.txt.
# Set the minimum version of CMake that can be used
cmake_minimum_required(VERSION 3.0)
#project name
project(SWAP)
#head file pat
include_directories( include )#source directory files to var
add_subdirectory( src DIR_SRCS )#add executable file
add_executable(swap_02 ${TEST_MATH})#add link library target_link_libraries(${FS_BUILD_BINARY_PREFIX}sqrt ${LIBRARIES}) 6.5.3 多目录工程 - 生成库编译
# Set the minimum version of CMake that can be used
cmake_minimum_required(VERSION 3.0)
#project name
project(SWAP_LIBRARY)
#add compile options
add_compile_options("-Wall -std=c++11")
#set CMAKE_BUILD_TYPE
set( CMAKE_BUILD_TYPE Debug )
# set output binary path
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
############################################################
# Create a library
############################################################
#Generate the static library from the library sources
add_library( swap_library STATIC src/Swap.cpp )
target_include_directories( swap_lib PUBLIC ${PROJECT_SOURCE_DIR}/include )
############################################################
# Create an executable
############################################################
# Add an executable with the above sources
add_executable( swap_01 main.cpp )
# link the new swap_01 target with the swap_lib target
target_link_libraries( swap_01 swap_liby )
#命令 mkdir build ->cd build -> cmake ..(build 文件下生成makefile 内容) ->make