![17492ebd030f0cc13471d6a84f9d827a.gif](https://i-blog.csdnimg.cn/blog_migrate/1b3a8f8faf5da17eb7b54e67e2632d2c.gif)
点击上方蓝字关注我哦~
01
前言
在VScode中如何像其它IDE一样快速创建如下图的项目文件树。
![423017dc24268ffc3aef527bc5ad8145.png](https://i-blog.csdnimg.cn/blog_migrate/43c06a254766393edd606f29fe334fd0.png)
就自己写了个脚本,用于自动创建项目。
02
脚本介绍
# 输入一个文件名 $projectnameread projectname# 源文件目录Src="./Src"# 头文件目录Inc="./Inc"# 创建项目树mkdir -p $Src $Inc ./Output/bin# 创建makefile文件touch makefile# 创建 main.cpp文件touch $Src/main.c# 文件名小写转换#declare -l lfilename=$projectnamelfilename="${projectname,,}"# 源文件c_file=$Src"/"${lfilename}".c"# 头文件h_file=$Inc"/"${lfilename}".h"# 创建一对项目文件touch $c_file $h_file# 读取本机时间time4=$(date "+%Y.%m.%d")# 向main.c中写入内容# 注释echo "/***************************************************************************" >> $Src/main.cecho "* Copyright (c) 2020~2021 XXXX" >> $Src/main.cecho "* All rights reserved" >> $Src/main.cecho "* " >> $Src/main.cecho "* 文件名称:main.c" >> $Src/main.cecho "* " >> $Src/main.cecho "* 摘 要:主函数入口" >> $Src/main.cecho "* " >> $Src/main.cecho "* 创 建 者:xxxx" >> $Src/main.cecho "* " >> $Src/main.cecho "* 创建日期:"$time4 >> $Src/main.cecho "* " >> $Src/main.cecho "* 修改记录" >> $Src/main.cecho "* 日期 修改者 版本 修改内容" >> $Src/main.cecho "* --- ---- --- -------" >> $Src/main.cecho "****************************************************************************/" >> $Src/main.c# mainecho "#include " >> $Src/main.cecho "#include \"$lfilename.h\"" >> $Src/main.cecho "" >> $Src/main.cecho "" >> $Src/main.cecho "" >> $Src/main.cecho "int main()" >> $Src/main.cecho "{" >> $Src/main.cecho " printf(\"hello world\n\");" >> $Src/main.cecho " test();" >> $Src/main.cecho " return 0;" >> $Src/main.cecho "}" >> $Src/main.c# 向$lfilename.c中写入内容# 注释echo "/***************************************************************************" >> $Src/$lfilename.cecho "* Copyright (c) 2020~2021 XXXX" >> $Src/$lfilename.cecho "* All rights reserved" >> $Src/$lfilename.cecho "* " >> $Src/$lfilename.cecho "* 文件名称:$lfilename.c" >> $Src/$lfilename.cecho "* " >> $Src/$lfilename.cecho "* 摘 要:测试C文件" >> $Src/$lfilename.cecho "* " >> $Src/$lfilename.cecho "* 创 建 者:xxxx" >> $Src/$lfilename.cecho "* " >> $Src/$lfilename.cecho "* 创建日期:"$time4 >> $Src/$lfilename.cecho "* " >> $Src/$lfilename.cecho "* 修改记录" >> $Src/$lfilename.cecho "* 日期 修改者 版本 修改内容" >> $Src/$lfilename.cecho "* --- ---- --- -------" >> $Src/$lfilename.cecho "****************************************************************************/" >> $Src/$lfilename.cecho "#include \"$lfilename.h\"" >> $Src/$lfilename.cecho "" >> $Src/$lfilename.cecho "" >> $Src/$lfilename.cecho "" >> $Src/$lfilename.cecho "int test()" >> $Src/$lfilename.cecho "{" >> $Src/$lfilename.cecho " printf(\"hello Test\n\");" >> $Src/$lfilename.cecho " return 0;" >> $Src/$lfilename.cecho "}" >> $Src/$lfilename.c# 向$lfilename.h中写入内容# 注释echo "/***************************************************************************" >> $Inc/$lfilename.hecho "* Copyright (c) 2020~2021 XXXX" >> $Inc/$lfilename.hecho "* All rights reserved" >> $Inc/$lfilename.hecho "* " >> $Inc/$lfilename.hecho "* 文件名称:$lfilename.h" >> $Inc/$lfilename.hecho "* " >> $Inc/$lfilename.hecho "* 摘 要:测试头文件" >> $Inc/$lfilename.hecho "* " >> $Inc/$lfilename.hecho "* 创 建 者:xxxx" >> $Inc/$lfilename.hecho "* " >> $Inc/$lfilename.hecho "* 创建日期:"$time4 >> $Inc/$lfilename.hecho "* " >> $Inc/$lfilename.hecho "* 修改记录" >> $Inc/$lfilename.hecho "* 日期 修改者 版本 修改内容" >> $Inc/$lfilename.hecho "* --- ---- --- -------" >> $Inc/$lfilename.hecho "****************************************************************************/" >> $Inc/$lfilename.hecho "/*防止重复引用 */" >> $Inc/$lfilename.hecho "#ifndef "$projectname"_H" >> $Inc/$lfilename.hecho "#define "$projectname"_H" >> $Inc/$lfilename.hecho "" >> $Inc/$lfilename.hecho "#include" >> $Inc/$lfilename.hecho "" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "//全局常量定义" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "//全局类型定义" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "//全局变量,可以被外部程序直接访问" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "//公开的过程/函数" >> $Inc/$lfilename.hecho "//---------------------------------------------------------------------" >> $Inc/$lfilename.hecho "" >> $Inc/$lfilename.hecho "int test();" >> $Inc/$lfilename.hecho "#endif" >> $Inc/$lfilename.h# 导入makefile# cat "./makefile" >> ./makefileecho "# C 项目 makefile文件" >> ./makefileecho "" >> ./makefileecho "# 头文件存放目录" >> ./makefileecho "INC_DIR=./Inc" >> ./makefileecho "" >> ./makefileecho "# 可执行文件存放目录" >> ./makefileecho "BIN_DIR=./Output/bin" >> ./makefileecho "OUT_DIR=.\Output" >> ./makefileecho "" >> ./makefileecho "# 源文件存放目录" >> ./makefileecho "SRC_DIR=./Src" >> ./makefileecho "" >> ./makefileecho "# 其它中间文件存放目录" >> ./makefileecho "OBJ_DIR=./Output" >> ./makefileecho "MainExt =.exe" >> ./makefileecho "SourceExt =.c" >> ./makefileecho "TargetExt =.o" >> ./makefileecho "" >> ./makefileecho "# 源文件列表" >> ./makefileecho "SRC := \${wildcard \${SRC_DIR}/*.c}" >> ./makefileecho "" >> ./makefileecho "# obj文件列表" >> ./makefileecho "OBJ := \${patsubst %.c, \$(OBJ_DIR)/%.o, \${notdir \${SRC}}}" >> ./makefileecho "" >> ./makefileecho "# 定义编译命令变量" >> ./makefileecho "CC := gcc" >> ./makefileecho "CFLAGS := -g -Wall -I\$(INC_DIR)" >> ./makefileecho "" >> ./makefileecho "# 定义可执行文件变量" >> ./makefileecho "TARGET := \$(BIN_DIR)/main\$(MainExt)" >> ./makefileecho "" >> ./makefileecho "# 生成可执行文件" >> ./makefileecho "\$(TARGET): \$(OBJ)" >> ./makefileecho " \$(CC) \$(CFLAGS) -o \$@ \$^" >> ./makefileecho "" >> ./makefileecho "#生成链接文件" >> ./makefileecho "\$(OBJ_DIR)/%.o: \$(SRC_DIR)/%.c" >> ./makefileecho " \$(CC) \$(CFLAGS) -c -o \$@ \$ >> ./makefileecho "" >> ./makefileecho "#clean规则" >> ./makefileecho ".PHONY: clean" >> ./makefileecho "clean:" >> ./makefileecho " del \$(OUT_DIR)\bin\*.exe" >> ./makefileecho " del \$(OUT_DIR)\*.o" >> ./makefile
脚本先从外部读取一个文件名,然后生成测试C文件和头文件,并生成main.c 和Makefile文件。windows下运行shell脚本需要安装git bash。
03
运行测试
运行脚本会弹出git bash对话框输入项目名TESTHELLO。回车等待创建完成
![d7cc8c8d240cf7d966909f0b658be484.png](https://i-blog.csdnimg.cn/blog_migrate/b41fb8a56484771c598e25759785a3b1.png)
make编译下
![9dc4285484a1079fa57b62da610fbbe6.png](https://i-blog.csdnimg.cn/blog_migrate/6b18b6cacbe4921ea38972268e121ef7.png)
运行可执行文件,输出符合预期。
![35ecddd9a57a0cdbdebc5695fd8f5398.png](https://i-blog.csdnimg.cn/blog_migrate/7f1325126ee295138a8aaa3f168b3c58.png)
看下编译后的目录文件树:
![7f8eef80a40698b93495f379c4524288.png](https://i-blog.csdnimg.cn/blog_migrate/b695d2c4b6965654c9854666de6f888d.png)
公众号回复“脚本” 获取本文资料。
/ The End /
推荐阅读
win10下使用VS Code编译、运行 和调试C
Makefile 入门
使用git管理嵌入式软件版本
本文由【嵌入式案例Show】原创出品,未经许可,请勿转载
![46da978fa5b754c39f25baf1b2eb4302.gif](https://i-blog.csdnimg.cn/blog_migrate/a8bf907298ced698491e13b23efcdfd9.gif)
扫码关注我们
看更多嵌入式案例
![1b555d81d7fec74a934fb3db32ea6c31.png](https://i-blog.csdnimg.cn/blog_migrate/1978a0841aece828b63faddb27ca7b09.jpeg)