美文网首页
Some basic tips with c++ in linu

Some basic tips with c++ in linu

作者: ustclcl | 来源:发表于2018-09-14 22:22 被阅读0次

using vim to edit a cpp source file

vim bubblesort.cpp

The code is like this:

# include<iostream>
using namespace std;

void BubbleSort(int A[], int n)
{
    int temp;
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(A[j]>A[j+1])
            {
                temp = A[j];
                A[j] = A[j+1];
                A[j+1] = temp;
            }
        }
    }
}

void print(int A[],int n)
{
    int i;
    for(i = 0;i < n;i++)
        cout << A[i] << ' ';
    cout << endl;
}
            
int main()
{
    int A[] = {5,34,67,32,43,7,75,34,4,55,63,99,22,123,143};
    int len = sizeof(A)/sizeof(int);
    
    print(A,len);
    BubbleSort(A,len);
    print(A,len);
}

Then we return to terminal, using

g++ -g -std=c++11 bubblesort.cpp -o bubble

In this command, -g used to generate gdb debug information which we will discuss later. -o to name the executed file.
To execute the code, use

.\bubblesort

Here I suppose you are at the dir. The outcome is

[xx@localhost gdb_example]$ ./bubblesort
5 34 67 32 43 7 75 34 4 55 63 99 22 123 143 
4 5 7 22 32 34 34 43 55 63 67 75 99 123 143 
[xx@localhost gdb_example]$ 

GDB is a debug tool under linux. If you want debug with gdb, you have to add -g in the gcc command:

$ g++ -g -std=c++11 xxx.cpp -o xxx

Start debug with the command:

1 $ gdb xxx

xxx is execute file.

2 $ gdb
dgb> file xxx

Useful command in gdb:

r for run
s for step
n for next (not go in to functions)
p [var] for print variable
b [lineNO][function] for set breakpoint at line or function
c for countinue
q for quit

More commands:

info breakpoints for show all breakpoints
delete for delete breakpoints
help [command]
set var

example:

[xx@localhost gdb_example]$ gdb bubblesort
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/xx/Documents/c++work/gdb_example/bubblesort...done.
(gdb) l
23  {
24      int i;
25      for(i = 0;i < n;i++)
26          cout << A[i] << ' ';
27      cout << endl;
28  }
29              
30  int main()
31  {
32      int A[] = {5,34,67,32,43,7,75,34,4,55,63,99,22,123,143};
(gdb) b main
Breakpoint 1 at 0x40095e: file bubblesort.cpp, line 32.
(gdb) r
Starting program: /home/xx/Documents/c++work/gdb_example/bubblesort 

Breakpoint 1, main () at bubblesort.cpp:32
32      int A[] = {5,34,67,32,43,7,75,34,4,55,63,99,22,123,143};
Missing separate debuginfos, use: debuginfo-install glibc-2.17-222.el7.x86_64 libgcc-4.8.5-28.el7_5.1.x86_64 libstdc++-4.8.5-28.el7_5.1.x86_64
(gdb) s
33      int len = sizeof(A)/sizeof(int);
(gdb) n
35      print(A,len);
(gdb) p len
$1 = 15
(gdb) 

相关文章

  • Some basic tips with c++ in linu

    using vim to edit a cpp source file The code is like this...

  • 使用Hexo搭建博客框架

    To record some basic operations about Hexo and test some ...

  • 商务英语 Level4 Unit2 part2

    Interview Tips Here are some tips for how to conduct a pr...

  • Chatbots Will Change Some Custom

    Some Basic Understanding: The Conversational interface is...

  • some tips

    公司小哥准备要跟着我这组学习ruby,然后写了些小建议: 1. 锻炼身体,推荐keep或跑步 2. 至少每周三次,...

  • some tips

    1.土豆削皮后有大量的淀粉在表面(貌似刀切比用筛子处理的会好一些),在炒之前最好是用清水清洗一下。但是不用清洗太彻...

  • Some Tips

    1、访问私有属性 2、访问 bundle 中的 json 文件 3、隐藏导航栏返回标题 4、weak self 5...

  • Some Tips

    1、在项目编译过程中gradle.properties配置的值会被编译解析,其作为配置文件使用是很有必要的当在gr...

  • Some tips

    1. CPU 32位机型 armv6, armv7, armv7sCPU 64位机型 arm64模拟器32位处理...

  • some tips

    开始学着使用onenote,发现可以把会议的内容很好的记录在里面。对一些幻灯内容进行拍照,可以打字把思考的部分写在...

网友评论

      本文标题:Some basic tips with c++ in linu

      本文链接:https://www.haomeiwen.com/subject/cntigftx.html