AI智能
改变未来

shell自动编译简单入门


动机

用于测试的一组程序比较多,每次编译命令麻烦,写个shell自动执行。

示例

简单示例

# cleanecho \"************\"echo \"clean project\"rm ./bin/*echo \"clean complete\"echo \"************\"# compile linkecho \"************************\"echo \"gcc start\"gcc fork_parent_first.c -o ./bin/fork_parent_firstgcc fork_child_first.c -o ./bin/fork_child_firstecho \"gcc complete\"echo \"************************\"

简单项目示例1

test.c

/*test.c*/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>int main() {init_menu();start_menu();return 0;}

menu.c

/*menu.c*/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#define MENU_COUNT  5#define MENU_PROMPT  9char* menu[MENU_PROMPT];char* separator;char* prompt;int init_menu() {menu[0] = \"************************************\\n\";menu[1] = \"*      This is my shel demol. Please try it.   *\\n\";menu[2] = \"*      1 : File                                *\\n\";menu[3] = \"*      2 : Edit                                *\\n\";menu[4] = \"*      3 : View                                *\\n\";menu[5] = \"*      4 : Tool                                *\\n\";menu[6] = \"*      0 : Quit                                *\\n\";menu[7] = \"************************************\\n\";menu[8] = \"*      You can use \\\"0: Quit\\\" to exit.\\n\";separator = \"************************************\\n\";prompt  = \"my shell >>> \";return 0;}int start_menu() {int user_choice = 0;int quit = 0;while(1) {user_choice = display_menu();quit = dispatch_task(user_choice);if (quit)break;}return 0;}int display_menu() {int i;for ( i=0; i<MENU_PROMPT; i++ ) {printf(menu[i]);}printf(prompt);int choice = 0;scanf(\"%d\", &choice);return choice;}

util.c

/*util.c*/#include <stdio.h>#include <stdlib.h>#include <sys/types.h>int dispatch_task(int choice) {int result_quit = 0;switch (choice) {case 1:func1();break;case 2:func2();break;case 3:func3();break;case 4:func4();break;case 0:quit();result_quit = 1;break;}return result_quit;}int func1() {printf(\"********** Task is processing. **********\\n\");printf(\"This is function1 File. \\n\");printf(\"This is function1 File. \\n\");printf(\"This is function1 File. \\n\");printf(\"*********** Task is completed. **********\\n\");}int func2() {printf(\"********** Task is processing. **********\\n\");printf(\"This is function2 Edit. \\n\");printf(\"This is function2 Edit. \\n\");printf(\"This is function2 Edit. \\n\");printf(\"*********** Task is completed. **********\\n\");}int func3() {printf(\"********** Task is processing. **********\\n\");printf(\"This is function3 View. \\n\");printf(\"This is function3 View. \\n\");printf(\"This is function3 View. \\n\");printf(\"*********** Task is completed. **********\\n\");}int func4() {printf(\"********** Task is processing. **********\\n\");printf(\"This is function4 Tool. \\n\");printf(\"This is function4 Tool. \\n\");printf(\"This is function4 Tool. \\n\");printf(\"*********** Task is completed. **********\\n\");}int quit() {printf(\"************************************\\n\");printf(\"The Program demo is over. \\n\");printf(\"************************************\\n\");}

mymake.sh

# cleanecho \"************\"echo \"clean project\"rm ./bin/*echo \"clean complete\"echo \"************\"# compile linkecho \"************************\"echo \"gcc start\"gcc test.c menu.c util.c -g -o ./bin/testecho \"gcc complete\"echo \"************************\"

简单项目示例2

# cleanecho \"************\"echo \"clean project\"rm ./obj/*rm ./bin/*echo \"clean complete\"echo \"************\"# compile linkecho \"************************\"echo \"gcc start\"gcc -c util.c -o ./obj/util.ogcc -c menu.c -o ./obj/menu.ogcc -c test.c -o ./obj/test.ogcc ./obj/test.o ./obj/menu.o ./obj/util.o -o ./bin/testecho \"gcc complete\"echo \"************************\"

示例中的命令及参数说明

echo

命令

rm

命令

gcc

命令

-c

参数

-o

参数

-g

参数

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » shell自动编译简单入门