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è)文件名 $projectname
read 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=$projectname
lfilename="${projectname,,}"
# 源文件
c_file=$Src"/"${lfilename}".c"
# 頭文件
h_file=$Inc"/"${lfilename}".h"
# 創(chuàng)建一對(duì)項(xiàng)目文件
touch $c_file $h_file
# 讀取本機(jī)時(shí)間
time4=$(date "+%Y.%m.%d")
# 向main.c中寫入內(nèi)容
# 注釋
echo "/***************************************************************************" $Src/main.c
echo "* Copyright (c) 2020~2021 XXXX" $Src/main.c
echo "* All rights reserved" $Src/main.c
echo "* " $Src/main.c
echo "* 文件名稱:main.c" $Src/main.c
echo "* " $Src/main.c
echo "* 摘 要:主函數(shù)入口" $Src/main.c
echo "* " $Src/main.c
echo "* 創(chuàng) 建 者:xxxx" $Src/main.c
echo "* " $Src/main.c
echo "* 創(chuàng)建日期:"$time4 $Src/main.c
echo "* " $Src/main.c
echo "* 修改記錄" $Src/main.c
echo "* 日期 修改者 版本 修改內(nèi)容" $Src/main.c
echo "* --- ---- --- -------" $Src/main.c
echo "****************************************************************************/" $Src/main.c
# main
echo "#include <stdio.h> " $Src/main.c
echo "#include \"$lfilename.h\"" $Src/main.c
echo "" $Src/main.c
echo "" $Src/main.c
echo "" $Src/main.c
echo "int main()" $Src/main.c
echo "{" $Src/main.c
echo " printf(\"hello world\n\");" $Src/main.c
echo " test();" $Src/main.c
echo " return 0;" $Src/main.c
echo "}" $Src/main.c
# 向$lfilename.c中寫入內(nèi)容
# 注釋
echo "/***************************************************************************" $Src/$lfilename.c
echo "* Copyright (c) 2020~2021 XXXX" $Src/$lfilename.c
echo "* All rights reserved" $Src/$lfilename.c
echo "* " $Src/$lfilename.c
echo "* 文件名稱:$lfilename.c" $Src/$lfilename.c
echo "* " $Src/$lfilename.c
echo "* 摘 要:測(cè)試C文件" $Src/$lfilename.c
echo "* " $Src/$lfilename.c
echo "* 創(chuàng) 建 者:xxxx" $Src/$lfilename.c
echo "* " $Src/$lfilename.c
echo "* 創(chuàng)建日期:"$time4 $Src/$lfilename.c
echo "* " $Src/$lfilename.c
echo "* 修改記錄" $Src/$lfilename.c
echo "* 日期 修改者 版本 修改內(nèi)容" $Src/$lfilename.c
echo "* --- ---- --- -------" $Src/$lfilename.c
echo "****************************************************************************/" $Src/$lfilename.c
echo "#include \"$lfilename.h\"" $Src/$lfilename.c
echo "" $Src/$lfilename.c
echo "" $Src/$lfilename.c
echo "" $Src/$lfilename.c
echo "int test()" $Src/$lfilename.c
echo "{" $Src/$lfilename.c
echo " printf(\"hello Test\n\");" $Src/$lfilename.c
echo " return 0;" $Src/$lfilename.c
echo "}" $Src/$lfilename.c
# 向$lfilename.h中寫入內(nèi)容
# 注釋
echo "/***************************************************************************" $Inc/$lfilename.h
echo "* Copyright (c) 2020~2021 XXXX" $Inc/$lfilename.h
echo "* All rights reserved" $Inc/$lfilename.h
echo "* " $Inc/$lfilename.h
echo "* 文件名稱:$lfilename.h" $Inc/$lfilename.h
echo "* " $Inc/$lfilename.h
echo "* 摘 要:測(cè)試頭文件" $Inc/$lfilename.h
echo "* " $Inc/$lfilename.h
echo "* 創(chuàng) 建 者:xxxx" $Inc/$lfilename.h
echo "* " $Inc/$lfilename.h
echo "* 創(chuàng)建日期:"$time4 $Inc/$lfilename.h
echo "* " $Inc/$lfilename.h
echo "* 修改記錄" $Inc/$lfilename.h
echo "* 日期 修改者 版本 修改內(nèi)容" $Inc/$lfilename.h
echo "* --- ---- --- -------" $Inc/$lfilename.h
echo "****************************************************************************/" $Inc/$lfilename.h
echo "/*防止重復(fù)引用 */" $Inc/$lfilename.h
echo "#ifndef "$projectname"_H" $Inc/$lfilename.h
echo "#define "$projectname"_H" $Inc/$lfilename.h
echo "" $Inc/$lfilename.h
echo "#include<stdio.h>" $Inc/$lfilename.h
echo "" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "http://全局常量定義" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "http://全局類型定義" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "http://全局變量,可以被外部程序直接訪問" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "http://公開的過程/函數(shù)" $Inc/$lfilename.h
echo "http://---------------------------------------------------------------------" $Inc/$lfilename.h
echo "" $Inc/$lfilename.h
echo "int test();" $Inc/$lfilename.h
echo "#endif" $Inc/$lfilename.h
# 導(dǎo)入makefile
# cat "./makefile" >> ./makefile
echo "# C 項(xiàng)目 makefile文件" ./makefile
echo "" ./makefile
echo "# 頭文件存放目錄" ./makefile
echo "INC_DIR=./Inc" ./makefile
echo "" ./makefile
echo "# 可執(zhí)行文件存放目錄" ./makefile
echo "BIN_DIR=./Output/bin" ./makefile
echo "OUT_DIR=.\Output" ./makefile
echo "" ./makefile
echo "# 源文件存放目錄" ./makefile
echo "SRC_DIR=./Src" ./makefile
echo "" ./makefile
echo "# 其它中間文件存放目錄" ./makefile
echo "OBJ_DIR=./Output" ./makefile
echo "MainExt =.exe" ./makefile
echo "SourceExt =.c" ./makefile
echo "TargetExt =.o" ./makefile
echo "" ./makefile
echo "# 源文件列表" ./makefile
echo "SRC := \${wildcard \${SRC_DIR}/*.c}" ./makefile
echo "" ./makefile
echo "# obj文件列表" ./makefile
echo "OBJ := \${patsubst %.c, \$(OBJ_DIR)/%.o, \${notdir \${SRC}}}" ./makefile
echo "" ./makefile
echo "# 定義編譯命令變量" ./makefile
echo "CC := gcc" ./makefile
echo "CFLAGS := -g -Wall -I\$(INC_DIR)" ./makefile
echo "" ./makefile
echo "# 定義可執(zhí)行文件變量" ./makefile
echo "TARGET := \$(BIN_DIR)/main\$(MainExt)" ./makefile
echo "" ./makefile
echo "# 生成可執(zhí)行文件" ./makefile
echo "\$(TARGET): \$(OBJ)" ./makefile
echo " \$(CC) \$(CFLAGS) -o \$@ \$^" ./makefile
echo "" ./makefile
echo "#生成鏈接文件" ./makefile
echo "\$(OBJ_DIR)/%.o: \$(SRC_DIR)/%.c" ./makefile
echo " \$(CC) \$(CFLAGS) -c -o \$@ \$<" ./makefile
echo "" ./makefile
echo "#clean規(guī)則" ./makefile
echo ".PHONY: clean" ./makefile
echo "clean:" ./makefile
echo " del \$(OUT_DIR)\bin\*.exe" ./makefile
echo " del \$(OUT_DIR)\*.o" ./makefile
腳本先從外部讀取一個(gè)文件名,然后生成測(cè)試C文件和頭文件,并生成main.c 和Makefile文件。windows下運(yùn)行shell腳本需要安裝git bash。
03
運(yùn)行測(cè)試
運(yùn)行腳本會(huì)彈出git bash對(duì)話框輸入項(xiàng)目名TESTHELLO?;剀嚨却齽?chuàng)建完成
make編譯下
運(yùn)行可執(zhí)行文件,輸出符合預(yù)期。
看下編譯后的目錄文件樹:
公眾號(hào)回復(fù)“腳本” 獲取本文資料。
/ The End /
推薦閱讀
win10下使用VS Code編譯、運(yùn)行 和調(diào)試C
本文由【嵌入式案例Show】原創(chuàng)出品,未經(jīng)許可,請(qǐng)勿轉(zhuǎn)載
掃碼關(guān)注我們
看更多嵌入式案例
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問題,請(qǐng)聯(lián)系我們,謝謝!