由于code::blocks与Visual C++的相似性,很容易上手,本博文主要摘记Ubuntu下使用code::blocks编码的技巧,其他IDE暂时不讨论。
安装code::blocks
sudo apt-get install build-essential
sudo apt install codeblocks
sudo apt-get install codeblocks-contrib(不安装此包,可能会没有incrementalsearch等众多功能)
sudo apt install libboost-all-dev(安装boost包,此步骤非必须)
安装gmp
sudo apt-get install libgmp10 libgmp-dev
sudo apt-get install libgmpxx4ldbl-dbgsym
sudo apt-get install libgmp10-dbgsym
信息:
gmp package : Ubuntu
gmp package in Ubuntu
libgmp-dev: Multiprecision arithmetic library developers tools
libgmp10: Multiprecision arithmetic library
libgmp10-dbgsym: debug symbols for package libgmp10
libgmp10-doc: Multiprecision arithmetic library example code
libgmp3-dev: Multiprecision arithmetic library developers tools
libgmpxx4ldbl: Multiprecision arithmetic library (C++ bindings)
libgmpxx4ldbl-dbgsym: debug symbols for package libgmpxx4ldbl
code::blocks中使用他人的源代码
将代码复制到工程目录下,为了避免修改他人代码中include的头文件的路径,可在project→build option→search directories增加工程的绝对路径
code::blocks中使用gmp及其他静态库
将project→build option→linker settings增加libgmpxx和libgmp(静态库的名称),可能debug和release下都需要增加
名称 | 位置 | 库名 |
gmp | libgmpxx、libgmp | |
boost::program_options | /usr/lib/x86_64-linux-gnu | libboost_program_options |
zlib | /usr/lib/x86_64-linux-gnu | libz |
安装cmake
准备工作:官网下载cmake-3.6.3.tar.gz(https://cmake.org/download/)
1.解压文件tar -xvf cmake-3.6.3.tar.gz,并修改文件权限chmod -R 777 cmake-3.6.3
2.检测gcc和g++是否安装,如果没有则需安装gcc-g++:sudo apt-get install build-essential(或者直接执行这两条命令sudo apt-get install gcc,sudo apt-get install g++)
3.进入cmake-3.6.3 进入命令 cd cmake-3.6.3
4.执行sudo ./bootstrap
5.执行sudo make
6.执行 sudo make install
7.执行 cmake –version,返回cmake版本信息,则说明安装成功
code::blocks中使用C++11
project→build option→compiler settings→compiler flags勾选have g++ follow C++ 11 ISO C++ language standard
获取系统状态
使用sysinfo函数,使用方法举例如下:
#include <sys/sysinfo.h>
double Free_Memo_Ratio()
{
struct sysinfo info;
sysinfo(&info);
return 1.0 * info.freeram / info.totalram;
}
使用valgrind查内存泄漏
内存溢出会导致一些很奇怪的错误(如vector申请不了空间,释放内存错误等)
命令示例1:
valgrind --leak-check=full bin/Debug/program
其中–leak-check=full 指的是完全检查内存泄漏,注意用Debug而非Release能更好地显示错误位置。
命令示例2:
valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes bin/Debug/program
命令示例3:
valgrind --tool=memcheck --leak-check=full --show-reachable=yes --trace-children=yes --log-file="debug.txt" ./program
其中–show-reachable=yes是显示所有内存情况,–trace-children=yes是跟入子进程,--log-file重定向输出。
使用GDB进行调试
- 开始:gdb 程序名,从文件读取命令加“-x command-file”,不输出版本信息加“-q”
- 模式:切换显示上下文的TUI模式,使用Ctrl+X+A
- 带参数调试:run 参数
- 设置断点:break 断点信息,临时断点使用tbreak,条件断点使用“break break-args if (condition)”,删除断点“delete 断点号”,忽略断点“ignore 断点号 次数”
- 调试:continue一直执行到断点,next执行一步不进入子函数,step执行一步进行子函数,finish(缩写fin)跳出当前函数,until i可以一次执行到第i行
- 查看信息:print,查看一维动态数组“print *数组名@长度”,打印选项设置参考Debugging with GDB - Print Settings,特别地,打印对象时可使用set print static-members [on | off]设置是否打印成员变量。
- 监视变量:watch 变量名
- 重启:checkpoint标记得到编号,restart 编号
使用perf/gprof测试模块性能
安装perf:sudo apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`
使用gcc优化:优化选项参考:Optimize Options (Using the GNU Compiler Collection (GCC))
使用perf:
- 测试:使用sudo perf record -a -g procedure options测试,使用sudo perf report显示(perf report -n --stdio输出文本信息)
- 如何在release模式下使用perf:编译时加上“-g”选项
使用gprof:
- 编译:增加-pg选项,可在code::blocks中project→build option→compiler settings→compiler flags直接勾选
- 测试:运行编译得到的可执行文件(假定程序名为prog),会在当前目录中生成gmon.out,运行gprof prog gmon.out > report.txt即可
使用time测试程序性能
使用/usr/bin/time --verbose -o output.txt program parameters,其中output.txt为输出时间内存等信息的文件名,运行时间为文件中User time与System time之和,可使用如下命令自动计算。
usertime=$(sed -n "/User time (seconds): .*/p" output.txt | sed "s/User time (seconds): / /")
systemtime=$(sed -n "/System time (seconds): .*/p" output.txt | sed "s/System time (seconds): / /")
echo "$usertime+$systemtime" | bc
二进制文件发布
1. 静态编译,然后使用file或ldd验证是否为静态编译
2. 使用strip进行压缩,然后使用nm验证是否有符号
【待续】