XunSuan's Blog


  • Home

  • About

  • Archives

用 travis-ci 来测试你的项目

Posted on 2017-03-20 |

这里是 Travis CI 的官网:here

先注册一个帐号。当然,可以用 GitHub 登录。

Read more »

让你的 C/C++ 程序运行在后台

Posted on 2017-03-13 |

注意:这只是 Linux 平台下的操作。

nohup

Linux 下有个叫 nohup,它可以把输出强行重定向到文本中。
配合 & 可以达到运行在后台。

1
$ nohup ./test &

将输出到 nohup.out 文件中。

fork() & exit()

那么问题来了,如何在 C/C++(依赖 POSIX)里实现捏?
Linux 下有个叫做 fork() 的函数,它可以新建一个子进程(注意:不是子线程)。
我们可以利用它来让程序运行在后台。

上代码!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid = fork();
if (!pid) // pid==0
{
// TODO: child process
}
else if (pid > 0)
{
// TODO: parent process
}
else
{
// TODO: fork failed
return 1;
}
return 0;
}

在 child process 中填写乃的代码就阔以辣。

gdb 基本用法

Posted on 2017-03-12 |

What is gdb?

gdb 是 GNU Debugger 的缩写,用来调试程序,支持的语言有[1]:

  • Ada
  • C
  • C++
  • Objective-C
  • Free Pascal
  • Fortran
  • Java

小试牛刀

写一个简单的小程序,然后用 gdb 调试一下。

1
2
3
4
5
6
7
8
9
#include <stdio.h>
int main(void)
{
int i;
scanf("%d, &i);
printf("i = %d\n", i);
return 0;
}

编译:

1
$ gcc main.c -std=C99

为了方便 gdb 调试,窝们加一个 -g 参数。

1
$ gcc main.c -g -std=C99

不出问题的话应该会有一个 a.out 文件。
窝们用 gdb 调试它:

1
$ gdb a.out

不出问题的话应该可以看到一个这样的界面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...(no debugging symbols found)...done.
(gdb)

(gdb) 就是 gdb 的提示符,类似 Unix 的 $ 和 #

1
(gdb) break 9

在 9 行中下一个断点,把程序跑起来:

1
(gdb) run

不出问题的话程序应该断在了第9行,然后输入 233,回车。
现在的话是断在第9(10)行了。窝们可以去看一下它的变量之类的操作:

1
(gdb) print i

返回结果:

1
2
3
(gdb) print i
$1 = 233
(gdb)

还可以修改 i 的值:

1
(gdb) set i = 233333

想要退出 gdb 的话可以用 quit 命令。

Next 和 Step 指令

next 指令会步过,但是不会跟踪函数(步入),而 step 会步入函数。

参考

1 GDB Documentation - Supported Languages.

Disqus

Posted on 2017-03-05 |

这个方法只限于 NeXT 主题,其他的窝也不太清楚。

Read more »

i3wm 终端透明

Posted on 2017-03-05 |

Konsole 自带的透明效果依赖于 xcompmgr
所以需要额外配置一下呢。

Read more »

完全匿名使用 GitHub

Posted on 2017-03-02 |

这里可以参考编程随想的文章
窝这里基本讲一下他漏掉的内容。

Read more »

hellohexo

Posted on 2017-03-01 |

Hello wolrd, first!

Hello World

Posted on 2017-03-01 |

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

Xun Suan

Xun Suan

8 posts
5 tags
© 2017 Xun Suan
Powered by Hexo
Theme - NexT.Muse