Linux-Shell获取未编译的工程文件并分类

本文介绍了一个用于清理Linux项目中非必需文件的脚本mv_src。该脚本通过GCC编译选项来确定项目的依赖文件,并将非必要的源代码文件移除到指定目录。文章详细记录了从编译依赖分析到脚本运行的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2016年10月22日 星期六

T.s.road总结笔记

项目源码:https://github.com/Tsroad/Linux-Shell-C/


作者说明:

When running this programme, the author’sPC setting is:

Ubuntu14.04 LTS+CPU 3.6GHz + RAM 8.0GB.

(form Lab 2204; Check by Keung Charteris& T.s.road CZQ)

题目

使用脚本mv_src实现将不需要的代码(以.c,.h结尾)去除。以linux 代码为例,可正常编译后,执行脚本,移动所有不是编译所需的文件和目录到指定的文件目录下。

例如:

 ./mv_src  ./linux-src/  ./test_src/

 执行脚本后,删除linux-src任一源文件,均导至编译出错。

需要提供脚本和测试过程的输出log.


对问题进行简化如下:

1.      文件夹内只包含未编译工程文件(*.c *.h Makefile etc.);

2.      子目录只做了一层,当然只要有Makefile子目录都不是问题;

3.      工程文件较少,仅为实验所用。


思路整理如下:

1.      确认哪些文件是编译所需,解决方案如下:

Gcc    –M   object.c    >>     temp.txt#会列出文件的依赖关系只有*.h文件

Gcc    –H    -c      object.c    >>     temp.txt  #会列出文件的依赖关系调用关系–c参数不链接,有些c文件不含main函数可用-c解决错误信息

2.      找出依赖关系后,接下来就是串的处理了,一系列的文本操作

详情见mv_src.


运行命令如下
$ tar xzvf linux-src.tar.gz
$ cd linux-src/
$ patch -p1 < ../patchfile 
$ cd ../
$ ./mv_src  ./linux-src/  ./test_src/



Done !


处理结果


系统log:

subdir
make -C subdir
make[1]: Entering directory `/home/tsroad/tsroad/linux-src/subdir'
echo /home/tsroad/tsroad/linux-src/subdir"/"iolib.c >> ../../temp.txt
cc -c iolib.c -o iolib.o 
cc -H -c iolib.c 2>> ../../temp.txt
make[1]: Leaving directory `/home/tsroad/tsroad/linux-src/subdir'
echo /home/tsroad/tsroad/linux-src"/"iolib.c >> ../temp.txt
cc -H -c iolib.c 2>> ../temp.txt
cc -c iolib.c -o iolib.o
echo /home/tsroad/tsroad/linux-src"/"main.c >> ../temp.txt
cc -H -c main.c 2>> ../temp.txt
cc -c main.c -o main.o
echo /home/tsroad/tsroad/linux-src"/"web_server.c >> ../temp.txt
cc -H -c web_server.c 2>> ../temp.txt
cc -c web_server.c -o web_server.o
make -C subdir clean	
make[1]: Entering directory `/home/tsroad/tsroad/linux-src/subdir'
make[1]: Leaving directory `/home/tsroad/tsroad/linux-src/subdir'


参考文献:

Linux and Unix make command——http://www.computerhope.com/unix/umake.htm

How do I make a simple makefile for gcc on Linux?——http://stackoverflow.com/questions/1484817/how-do-i-make-a-simple-makefile-for-gcc-on-linux

Replace a string in shell script ——http://stackoverflow.com/questions/3306007/replace-a-string-in-shell-script-using-a-variable

cat option——https://en.wikipedia.org/wiki/Cat_(Unix)

Gcc compile argument  ——https://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_3.html

linux shell String Operation——http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html

Linux shell scripts——http://linuxcommand.org/wss0090.php

Gcc详解——http://blog.chinaunix.net/uid-21411227-id-1826747.html

Using 'sed' to find and replace ——http://unix.stackexchange.com/questions/159367/using-sed-to-find-and-replace



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值