Linux_2

Linux上开发

vim编辑器

插入模式 :编辑模式

命令模式: 允许通过命令 进行文本编辑控制

底行模式:vim和shell 进行交互 (执行shell命令

命令模式

-i 进入插入模式

-a 进入插入模式同时光标到下一个位置

-o 进入插入模式同时光标到下一行

n + yy 连续复制n行

n + p 连续粘贴n次

-u 撤销操作

n + d 删除d行

-$ 快速把光标定位到当前行结尾

-^ 快速把光标定位到当前行开头

-gg 定位到文本最开始

-G 定位到文本最结尾

n + G 光标定位到n行

n + (h, j, k, l 方向定位, 移动指定n个位置

w,b 按照单词进行光标前后移动
大小写切换

底行模式

退出vim : wq 保存退出

关键字搜索 / + 关键字

!+ 命令, 执行命令

Linux编译器 gcc, g++

gcc只能编译c语言, g++可以编译c和c++

例如 g++ test.cc -o you.exe -std=c++11

程序翻译过程

翻译过程

预处理 : g++ -E test.cc --o test.i

编译: g++ -S test.i -o test.s

汇编 g++ -c test.s -o test.o

链接 g++ test.o -o my.exe

linux 链接时, 有两种链接方式

动态链接 : 动态库, .so

节省资源, 不能丢失

静态链接: 静态库 .a

g++ -o mytest_static test.c -static

每次链接都需要把库拷贝一份, 一旦形成和库就无关了, 无运行依赖

makefile / make

用于快速编译

最简单的makefile

makefile 默认执行一对依赖关系

(目标文件)mytest:test.cc(依赖文件列表, 如果有多个文件用空格划分)
	g++ -o mytest test.cc -std=c++11 (依赖方法) // make 命令构建
.PHONY:clean // 修饰下面总是能被执行
clean:
	rm -f mytest // make clean 命令清理

makefile 会根据文件中的依赖关系, 进行自动推导, 帮助执行所有的依赖关系, 类似于递归。

makefile示例

bin=mycode
src=code.cc

$(bin):$(src)
	@g++ -o $@ $^
	@echo "compliesr $(src) to $(bin)..."
.PHONY:clean
clean:
	@rm -f $(bin)
	@echo "clean project"

Linux下编写一个进度条

理解回车和换行 '\n' -> 回车 + 换行

缓冲区,按行刷新 提高效率

process

#include "Processbar.h"

// download
// 模拟简单网络
void download() {
    double filesize = 100 * 1024 * 1024;
    double currect = 0.0; 
    double bandwidth = 1024 * 1024; // 带宽

    printf("download begin, filesize: %lf\n", filesize);
    while (currect <= filesize) {
        ProcBar(filesize, currect);
        currect += bandwidth;
        // sleep(1);
        usleep(100000);
    }
    printf("\n");
    printf("download done, filesize %lf\n", filesize);
}

int main() {
    // ForTest();
    // ProcBar(100, 49.5);
    download();
}
bin=processbar
src=Main.c Porcessbar.c

$(bin):$(src)
	@gcc -o $@ $^
	@echo "compliesr $(src) to $(bin)..."
.PHONY:clean
clean:
	@rm -f $(bin)
	@echo "clean project"

#include "Processbar.h"

// void ForTest() {
//     printf("this is fortest\n");
// }
#define length 101
#define Style '#'
const char* lable = "|/-\\";

// version1
// void ProcBar() {
//     char bar[length];
//     memset(bar, '\0', sizeof(bar));

//     int cnt = 0;
//     while (cnt <= 100) {
//         printf("[%-100s][%3d%%][%c]\r", bar, cnt, lable[cnt % 4]);
//         fflush(stdout);
//         bar[cnt++] = Style;
//         usleep(10000);
//     }
//     printf("\n");
// }

// version2
void ProcBar(double total, double currect) {
    char bar[length];
    memset(bar, '\0', sizeof(bar));

    int cnt = 0;
    int locp_count = currect * 100.0 / total;
    double rate = (currect * 100.0) / total;

    while (cnt <= locp_count) {
        bar[cnt++] = Style;
    }
    printf("[%-100s][%.1lf%%][%c]\r", bar, rate, lable[cnt % 4]);
    fflush(stdout);
    // printf("\n");

}
#pragma once

#include <stdio.h>
#include <string.h>
#include <unistd.h>

// extern void ForTest();

void ProcBar(double total, double currect);

gdb 调试器

debug(可以被调式) 和 release

debug时, 编译形成可执行程序会添加调试信息

g++/gcc 默认编译是release模式

debug 编译命令 + -g

断点是查找问题时用来快速锁定问题区域的

基础相关命令

gdb [可执行程序] #开始调试
quit # 退出gdb
list [行号] #(简写为 l) 显示文本
b [行数] / b [文件名:行号] #打断点
i b #查看断点
d [断点编号] #删除断点
disable\enable [断点编号] # 关闭/开启断点
next / n # 表示逐过程调试
step / s # 进入函数·内部
print [变量] #显示变量信息
display[变量] / undisplay[变量序号] #打开 / 关闭 常显示
continue # 直接跳到下一个断点
finish # 一键运行完一个函数
util # 跳转到某一行

posted @ 2025-07-21 16:51  _nilv  阅读(8)  评论(0)    收藏  举报