linux C/C++编程基本操作

来源:互联网 发布:软件开发培训班 编辑:程序博客网 时间:2024/06/08 05:52
linux C/C++编程基本操作
1、gcc编译器
编译命令
1)gcc hello.c -o hello
2)./hello

2、g++编译器
同上,针对c++

3、gdb调试器
编译时要加上-g选项,生成的目标文件才能用gdb进行调试
1)gcc -g hello.c -o hello
2)gdb hello
gdb基本命令
backtrace(或bt)    查看各级函数调用及参数
finish                   执行到当前函数返回,然后停下来等待命令
frame(或f)帧编号 选择栈帧
info(或i) locals     查看当前栈帧局部变量的值
list(或l)               列出源代码
list 行号              列出从第几行开始的源代码
list 函数名           列出某个函数的源代码
next(或n)            执行下一条语句
print(或p)            打印表达式的值,通过表达式可以修改变量的值或者调用函数
quit                     退出gdb环境
set var                修改变量的值
start                   开始执行程序,停在main函数第一行语句前面等待命令
step(或s)            执行下一条语句,如果有函数调用则进入到函数中
回车                   重复上一条命令                   

4、makefile
make命令会自动读取当前目录下的makefile文件
规则格式:
目标:条件
          命令(必须以tab开头)
1)欲更新目标,必须先更新所有条件
2)所有条件只要有一个被更新,目标也必须被更新
例子:
1)test.h
#ifndef _TEST_H
#define _TEST_H
int add(int a, int b);
int sub(int a, int b);
#endif
2)sub.c
#include"test.h"
int sub(int a, int b)
{
    return a-b;
}
3)add.c
#include"test.h"
#include<stdio.h>
int add(int a, int b)
{
    return a + b;
}
int main()
{
    printf("2+3=%d\n",add(2,3));
    printf("2-3=%d\n",sub(2,3));
    return 1;
}
4)makefile
test:add.o sub.o
        gcc add.o sub.o -o test
add.o:add.c test.h
        gcc -c add.c
sub.o:sub.c test.h
        gcc -c sub.c
clean:
        rm -rf test
        rm -rf *.o
步骤:
1)执行make命令,会生成全部目标文件
2)./test运行得到结果
3)执行make clean命令,会执行最后clean部分的命令
原创粉丝点击