悉远

 找回密码
 注册
搜索
查看: 1252|回复: 0

Linux下编译与调试.

[复制链接]
发表于 2012-4-27 21:01:08 | 显示全部楼层 |阅读模式
LINUX下编译与调试


在Linux中开发c或c++程序的步骤:
先用vim编辑c或c++程序  c保存为.c c++保存为.cc 或.cpp文件

按 Tab 键补齐命令,用光标键上下翻动历史命令. 用help up看帮助
2.2. gdb 应用举例
下面列出了将被调试的程序它显示一个简单的问候, 再用反序将它列出
main.cpp:
void MyPrint(const char *pszSrc);
void MyPrint2(const char *pszSrc);
int main ()
{
    char szSrc[]= "hello there";
   MyPrint(szSrc);
MyPrint2(szSrc);
}

func.cpp
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void MyPrint(const char *pszSrc)
  printf("The string is %s\n", pszSrc);
}
void MyPrint2(const char *pszSrc)
{
    char*pszRev;
    int i,iLen;
   iLen=strlen(pszSrc);
    pszRev=(char*)malloc(iLen+1);
   for(i=0;i<iLen;i++)
       pszRev[iLen-i]=pszSrc;
   pszRev[iLen]='\0';
   printf("The revert string is:%s\n",pszRev);
   free(pszRev);
}
用下面的命令编译它(注意加上-g的编译选项):
g++ -g *.cpp
(gdb) file a.out
(gdb) run
(gdb) l[ist] func.cpp:1  列出源代码: 技巧: 在gdb 提示符下按回车健将重复上一个命令.
(gdb) break 17
(gdb) run
(gdb) watch pszRev[iLen-i]
(gdb) n
(gdb) info b                                            [查看所有断点信息]
(gdb) d b
(gdb) b 17 if i>=iLen-1                             [只有在i>=iLen-1时才停止在17行]
(gdb) continue                                                [继续全速运行]
(gdb) p i
(gdb) p iLen

练习:
1.用gdb调试下面程序(程序想倒序输出str的内容)
#include <stdio.h>
int f1(char *str);
int f2(char *str);
int main()
{

   charstr[]="helloworld";

   f1(str);
   f2(str);
   return 0;
}
int f1(char *str)
{

  printf("f1 str is%s",str);

}
int f2(char *str)
{
  char *str2;
  int size, i;
  size=strlen(str);
  str2=(char*)malloc(size+1);
  for(int i=0; i<size; i++)
     str2[size - i] = str;
  str2[size+1]='\0';

  printf("f2 str is%s",str2);

}
步骤:
  gcc –g –o str.out str.c
  gdb str.out
  gdb>l
  gdb>b 30 for处设断点
  gdb>b 33  printf处设断点
  gdb>info b
  gdb>r
  gdb>n
  gdb>p str2[size-i] 多次运行看值对不
  gdb>c
  gbd>p str2[0]  看str2数组中的值

3. 大型项目管理对大型项目而言编写makefile难度较大,autotools系列工具只需用户输入简单的目标文件、依赖文件、文件目录等就可以轻松地生成makefile,并且autotools工具会自动收集系统配置信息的,可以方便地处理各种移植性的问题。
Linux上的软件开发一般都用autotools来制作makefile
autotools流程图

用automake管理项目
1建立hello目录

2 写hello.chello.h

3 autoscan生成configure.scan
4修改configure.scan并保存为configure.in
AC_INIT(hello,1.0)
AM_INIT_AUTOMAKE(hello,1.0)
AC_CONFIG_SRCDIR([hello.h])
AC_CONFIG_HEADER([config.h])
5 aclocal生成aclocal.m4
6 用autoconf生成configure
7 用autoheader生成config.in.h
8 编辑Makefile.am
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
hello_SOURCE=hello.c hello.h
9 用automake生成Makefile.in
10用configure生成Makefile
11 make
12 make install
13 make dist

02Linux下编译与调试.doc

275 KB, 下载次数: 2

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|QQ客服|联系我们|Archiver|手机版|小黑屋|悉远网络 ( 鄂ICP备09013446号 )

GMT+8, 2024-4-19 23:00 , Processed in 0.040434 second(s), 10 queries , Redis On.

Powered by Discuz! X3.5

Copyright © 2001-2023 Tencent Cloud.

快速回复 返回顶部 返回列表