Linux下使用脚本创建工程目录,同时创建关联Makefile

本文分享了一个用于创建统一风格工程结构的Shell脚本,旨在帮助开发者在学习和工作中形成固定的编码风格,提高代码的可读性和维护性。脚本详细定义了Makefile的创建过程,包括顶层Makefile、模块Makefile及主Makefile的生成,同时自动创建了必要的目录结构。

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

苦于学习工作过程中,长期没有固定风格,不便于有效的关联自己的书写代码,特此,写了个shell脚本,用于以后以同意风格工程结构目录,记录如下:

#########################################################################
# File Name: createproject.sh
# Author: ma6174
# mail: ma6174@163.com
# Created Time: Mon 30 Jul 2018 09:26:00 PM CST
#########################################################################
#!/bin/bash

#创建顶层Makefile
function CreateTopMakefile()
{
    text="include Makefile.in\n\n"
    text+="MODULEDIR=modules\n\n"
    text+=".PHONY:all clean \n\n"
    text+="all: MODULE \$(CUR_OBJS) \$(TARGET)\n\n"
    text+="MODULE:\$(MODULEDIR)\n"
    text+="\tmake -C \$^\n\n"
    text+="\$(CUR_OBJS):%.o:%.c\n"
    text+="\t\$(CC) -c \$(CFLAGS) -o \$@ $< \$(INCLUDE)\n\n"
    text+="\$(TARGET):\$(CUR_OBJS)\n"
    text+="\t\$(CC) -o \$@ \$< \$(INCLUDE) \$(LDFLAGS)\n\n"
    text+="clean:\n"
    text+="\t@\$(foreach N, \$(MODULEDIR),make clean -C \$(N);)\n"
    text+="\trm -rf \$(CUR_OBJS) \$(TARGET)\n\n"
    text+="\trm -rf \$(LIBSDIR)\n\n"
    echo -e $text >$1
}
function CreateTopMakefileIn()
{
    text="APP=main\n\n"
    text+="CC=gcc\n\n"
    text+="CFLAGS= -Wall -ggdb3\n\n"
    text+="LIBSDIR=lib\n\n"
    text+="LDFLAGS=\n\n"
    text+="INCLUDE=-I./include\n\n"
    text+="CUR_SOURCE=\$(wildcard src/*.c)\n\n"
    text+="CUR_OBJS=\$(patsubst %.c, %.o, \$(CUR_SOURCE))\n\n"
    text+="BINDIR=bin/\n\n"
    text+="TARGET=\$(BINDIR)\$(APP)\n"
    echo -e $text >$1
}
function CreateModuleDirMakefile()
{
    text="SUBDIRS :="$2"\n\n"
    text+=".PHONY:all clean\n\n"
    text+="all:\n"
    text+="\t@\$(foreach N, \$(SUBDIRS),make -C \$(N);)\n\n"
    text+="clean:\n"
    text+="\t@\$(foreach N, \$(SUBDIRS),make clean -C \$(N);)\n"
    echo -e $text >$1
}
function CreateModuleMakefile()
{
    text="include Makefile.in\n\n"
    text+=".PHONY:all clean\n\n"
    text+="all:\$(CUR_OBJS) \$(TARGET)\n\n"
    text+="\$(CUR_OBJS):%.o:%.c\n"
    text+="\t\$(CC) -c \$(CFLAGS) -o \$@ \$< \$(INCLUDE)\n\n"
    text+="\$(TARGET):\$(CUR_OBJS)\n"
    text+="\t\$(CC) -fPIC -shared -o \$@ \$<\n"
    text+="\tcp \$(TARGET) \$(INSTALLDIR)/lib\n"
    text+="\tcp \$(INCLUDEDIR)/*.h \$(INSTALLDIR)/include\n\n"
    text+="clean:\n"
    text+="\trm -rf \$(CUR_OBJS) \$(TARGET)\n" 
    echo -e $text >$1
}
function CreateModuleMakefileIn()
{
    text="MODULE=lib"$2"\n\n"
    text+="CC=gcc\n"
    text+="CFLAGS= -Wall -ggdb3 -fPIC\n"
    text+="LIBSDIR=\n"
    text+="INCLUDEDIR=include\n"
    text+="INSTALLDIR=../..\n"
    text+="INCLUDE=-I\$(INCLUDEDIR)\n"
    text+="CUR_SOURCE=\$(wildcard src/*.c)\n"
    text+="CUR_OBJS=\$(patsubst %.c, %.o, \$(CUR_SOURCE))\n"
    text+="LIBSDIR=lib/\n"
    text+="TARGET=\$(LIBSDIR)\$(MODULE).so"
    echo -e $text >$1
}
function CreateMainMakefile()
{
    text="include Makefile.in\n\n"
    text+=".PHONY:all clean\n\n"
    text+="all:\$(CUR_OBJS) \$(TARGET)\n\n"
    text+="\$(CUR_OBJS):%.o:%.c\n"
    text+="\t\$(CC) -c \$(CFLAGS) -o \$@ \$< \$(INCLUDE)\n\n"
    text+="\$(TARGET):\$(CUR_OBJS)\n"
    text+="\t\$(CC) -o \$@ \$<\n\n"
    text+="clean:\n"
    text+="\t\rm -rf \$(CUR_OBJS) \$(TARGET)\n"
    echo -e $text >$1
}

function CreateMainMakefileIn()
{
    text="APP="$2"\n"
    text+="CC=gcc\n"
    text+="CFLAGS= -Wall -ggdb3\n"
    text+="LIBS=\n"
    text+="INCLUDE=-I./include\n"
    text+="CUR_SOURCE=\$(wildcard *.c)\n"
    text+="CUR_OBJS=\$(patsubst %.c, %.o, \$(CUR_SOURCE))\n"
    text+="BINDIR=lib/\n"
    text+="TARGET=\$(BINDIR)\$(APP)\n" 
    echo -e $text >$1
}

dirlist="bin include doc lib  src modules test"
modules="sample "
moduledir="include lib src test"
IFS=" " 

dirarr=($dirlist)
modulearr=($modules)
moduledirarr=($moduledir)
function createmoduledir()
{

    for var in ${moduledirarr[@]};
    do
        mkdir -p $1/$var
    done
}
function create_project()
{
    projname=$1

    mkdir -p $projname
    if [  -d $projname  ];then
        CreateTopMakefile $projname/Makefile
        CreateTopMakefileIn $projname/Makefile.in
    fi
    for dir in ${dirarr[@]};
    do
        mkdir -p $projname/$dir
    done

    if [  -d $projname/modules  ];then
        CreateModuleDirMakefile  $projname/modules/Makefile $modules

        for modl in ${modulearr[@]};
        do
            mkdir -p $projname/modules/$modl
            CreateModuleMakefile $projname/modules/$modl/Makefile
            CreateModuleMakefileIn $projname/modules/$modl/Makefile.in $modl
            createmoduledir $projname/modules/$modl
        done
    fi
    #if 0
    if [  -d $projname/src  ];then
        CreateMainMakefile  $projname/src/Makefile
        CreateMainMakefileIn  $projname/src/Makefile.in $projname
    fi
    #endif
}

if [ $# -ne 1 ];then     
    echo "==[E]please input project name!" 
fi 

project=$1

if [  -d $project  ];then
    echo "文件夹已经存在"
    exit
fi

create_project $project

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值