VSCode快速創(chuàng)建多目錄多文件C項(xiàng)目
掃描二維碼
隨時(shí)隨地手機(jī)看文章
點(diǎn)擊上方藍(lán)字關(guān)注我哦~
01
前言
在VScode中如何像其它IDE一樣快速創(chuàng)建如下圖的項(xiàng)目文件樹。
就自己寫了個(gè)腳本,用于自動(dòng)創(chuàng)建項(xiàng)目。
02
腳本介紹
# 輸入一個(gè)文件名 $projectnameread projectname# 源文件目錄Src="./Src"# 頭文件目錄Inc="./Inc"# 創(chuàng)建項(xiàng)目樹mkdir -p $Src $Inc ./Output/bin# 創(chuàng)建makefile文件touch makefile# 創(chuàng)建 main.cpp文件touch $Src/main.c# 文件名小寫轉(zhuǎn)換#declare -l lfilename=$projectnamelfilename="${projectname,,}"# 源文件c_file=$Src"/"${lfilename}".c"# 頭文件h_file=$Inc"/"${lfilename}".h"# 創(chuàng)建一對項(xiàng)目文件touch $c_file $h_file# 讀取本機(jī)時(shí)間time4=$(date "+%Y.%m.%d")# 向main.c中寫入內(nèi)容# 注釋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 "* 摘 要:主函數(shù)入口" $Src/main.cecho "* " $Src/main.cecho "* 創(chuàng) 建 者:xxxx" $Src/main.cecho "* " $Src/main.cecho "* 創(chuàng)建日期:"$time4 $Src/main.cecho "* " $Src/main.cecho "* 修改記錄" $Src/main.cecho "* 日期 修改者 版本 修改內(nèi)容" $Src/main.cecho "* --- ---- --- -------" $Src/main.cecho "****************************************************************************/" $Src/main.c# mainecho "#include <stdio.h> " $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中寫入內(nèi)容# 注釋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 "* 創(chuàng) 建 者:xxxx" $Src/$lfilename.cecho "* " $Src/$lfilename.cecho "* 創(chuàng)建日期:"$time4 $Src/$lfilename.cecho "* " $Src/$lfilename.cecho "* 修改記錄" $Src/$lfilename.cecho "* 日期 修改者 版本 修改內(nèi)容" $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中寫入內(nèi)容# 注釋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 "* 創(chuàng) 建 者:xxxx" $Inc/$lfilename.hecho "* " $Inc/$lfilename.hecho "* 創(chuàng)建日期:"$time4 $Inc/$lfilename.hecho "* " $Inc/$lfilename.hecho "* 修改記錄" $Inc/$lfilename.hecho "* 日期 修改者 版本 修改內(nèi)容" $Inc/$lfilename.hecho "* --- ---- --- -------" $Inc/$lfilename.hecho "****************************************************************************/" $Inc/$lfilename.hecho "/*防止重復(fù)引用 */" $Inc/$lfilename.hecho "#ifndef "$projectname"_H" $Inc/$lfilename.hecho "#define "$projectname"_H" $Inc/$lfilename.hecho "" $Inc/$lfilename.hecho "#include<stdio.h>" $Inc/$lfilename.hecho "" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "http://全局常量定義" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "http://全局類型定義" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "http://全局變量,可以被外部程序直接訪問" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "http://公開的過程/函數(shù)" $Inc/$lfilename.hecho "http://---------------------------------------------------------------------" $Inc/$lfilename.hecho "" $Inc/$lfilename.hecho "int test();" $Inc/$lfilename.hecho "#endif" $Inc/$lfilename.h# 導(dǎo)入makefile# cat "./makefile" >> ./makefileecho "# C 項(xiàng)目 makefile文件" ./makefileecho "" ./makefileecho "# 頭文件存放目錄" ./makefileecho "INC_DIR=./Inc" ./makefileecho "" ./makefileecho "# 可執(zhí)行文件存放目錄" ./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 "# 定義可執(zhí)行文件變量" ./makefileecho "TARGET := \$(BIN_DIR)/main\$(MainExt)" ./makefileecho "" ./makefileecho "# 生成可執(zhí)行文件" ./makefileecho "\$(TARGET): \$(OBJ)" ./makefileecho " \$(CC) \$(CFLAGS) -o \$@ \$^" ./makefileecho "" ./makefileecho "#生成鏈接文件" ./makefileecho "\$(OBJ_DIR)/%.o: \$(SRC_DIR)/%.c" ./makefileecho " \$(CC) \$(CFLAGS) -c -o \$@ \$<" ./makefileecho "" ./makefileecho "#clean規(guī)則" ./makefileecho ".PHONY: clean" ./makefileecho "clean:" ./makefileecho " del \$(OUT_DIR)\bin\*.exe" ./makefileecho " del \$(OUT_DIR)\*.o" ./makefile
腳本先從外部讀取一個(gè)文件名,然后生成測試C文件和頭文件,并生成main.c 和Makefile文件。windows下運(yùn)行shell腳本需要安裝git bash。
03
運(yùn)行測試
運(yùn)行腳本會(huì)彈出git bash對話框輸入項(xiàng)目名TESTHELLO?;剀嚨却齽?chuàng)建完成
make編譯下
運(yùn)行可執(zhí)行文件,輸出符合預(yù)期。
看下編譯后的目錄文件樹:
公眾號回復(fù)“腳本” 獲取本文資料。
/ The End /
推薦閱讀
win10下使用VS Code編譯、運(yùn)行 和調(diào)試C
本文由【嵌入式案例Show】原創(chuàng)出品,未經(jīng)許可,請勿轉(zhuǎn)載
掃碼關(guān)注我們
看更多嵌入式案例
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場,如有問題,請聯(lián)系我們,謝謝!





