鱼C论坛

 找回密码
 立即注册
查看: 1856|回复: 2

[作品展示] 关于在Ubuntu上面每次使用gcc ...太麻烦,所以写了一个简易的编译程序,需要自取

[复制链接]
发表于 2022-12-31 18:29:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>

  5. int main(int argc, char* argv[])
  6. {
  7.         int i, j, n;
  8.         char c;
  9.         char chs[255] = { 0 };
  10.         char ch[255] = { 0 };
  11.         char input[255] = { 0 };
  12.         char* yes[2] = { "y","Y" }, * no[2] = { "n","N" };
  13.         bool b = false, b2 = false;
  14.         char output[255] = { 0 };

  15.         for (i = 0; i < argc; i++)//寻找要编译的下标索引和是否需要-lm编译
  16.         {
  17.                 for (j = 0; j < strlen(argv[i]); j++)
  18.                 {
  19.                         //查询命令行中输入的是否是.c文件名
  20.                         if (*(argv[i] + j) == '.')
  21.                         {
  22.                                 if (*(argv[i] + j + 1) == 'c')//找到就给input
  23.                                 {
  24.                                         b2 = true;
  25.                                         strcpy(input, argv[i]);
  26.                                         strcpy(output, input);
  27.                                 }
  28.                         }
  29.                         //查询命令行中是否是数字字符串
  30.                         else if (*(argv[i] + j) >= 48 && *(argv[i] + j) <= 57)
  31.                         {
  32.                                 b2 = true;
  33.                                 strcpy(input, argv[i]);
  34.                         }
  35.                 }
  36.                 //查找是否有n,N,y,Y
  37.                 if (!strcmp(argv[i], yes[0]) || !strcmp(argv[i], yes[1]))
  38.                 {
  39.                         b = true;
  40.                 }
  41.                 else if (!strcmp(argv[i], no[0]) || !strcmp(argv[i], no[1]))
  42.                 {
  43.                         b = false;
  44.                 }
  45.                 else//都没找到,就默认不加-lm
  46.                 {
  47.                         b = false;
  48.                 }
  49.         }
  50.         if (!b2)//没找到有.c或数字的下标索引
  51.         {
  52.                 printf("请手动输入要编译的名称:");
  53.                 scanf("%s", input);
  54.                 getchar();//吃掉这里多打的回车
  55.                 strcpy(output, input);
  56.         }

  57.         if (input[0] >= 48 && input[0] <= 57)
  58.         {
  59.                 n = atoi(input);
  60.                 switch (n)
  61.                 {
  62.                 case 0:
  63.                         strcat(chs, "t0.c");
  64.                         strcpy(output, "t0.c");
  65.                         break;
  66.                 case 1:
  67.                         strcat(chs, "t1.c");
  68.                         strcpy(output, "t1.c");
  69.                         break;
  70.                 case 2:
  71.                         strcat(chs, "t2.c");
  72.                         strcpy(output, "t2.c");
  73.                         break;
  74.                 case 3:
  75.                         strcat(chs, "t3.c");
  76.                         strcpy(output, "t3.c");
  77.                         break;
  78.                 case 4:
  79.                         strcat(chs, "t4.c");
  80.                         strcpy(output, "t4.c");
  81.                         break;
  82.                 case 5:
  83.                         strcat(chs, "t5.c");
  84.                         strcpy(output, "t5.c");
  85.                         break;
  86.                 case 6:
  87.                         strcat(chs, "t6.c");
  88.                         strcpy(output, "t6.c");
  89.                         break;
  90.                 case 7:
  91.                         strcat(chs, "t7.c");
  92.                         strcpy(output, "t7.c");
  93.                         break;
  94.                 case 8:
  95.                         strcat(chs, "t8.c");
  96.                         strcpy(output, "t8.c");
  97.                         break;
  98.                 case 9:
  99.                         strcat(chs, "t9.c");
  100.                         strcpy(output, "t9.c");
  101.                         break;
  102.                 case 10:
  103.                         strcat(chs, "test.c");
  104.                         strcpy(output, "t10.c");
  105.                         break;
  106.                 case 11:
  107.                         strcat(chs, "t11.c");
  108.                         strcpy(output, "t11.c");
  109.                         break;
  110.                 case 12:
  111.                         strcat(chs, "t12.c");
  112.                         strcpy(output, "t12.c");
  113.                         break;
  114.                 case 13:
  115.                         strcat(chs, "t13.c");
  116.                         strcpy(output, "t13.c");
  117.                         break;

  118.                 case 14:
  119.                         strcat(chs, "t14.c");
  120.                         strcpy(output, "t14.c");
  121.                         break;
  122.                 case 15:
  123.                         strcat(chs, "t15.c");
  124.                         strcpy(output, "t15.c");
  125.                         break;
  126.                 case 16:
  127.                         strcat(chs, "t16.c");
  128.                         strcpy(output, "t16.c");
  129.                         break;
  130.                 case 17:
  131.                         strcat(chs, "t17.c");
  132.                         strcpy(output, "t17.c");
  133.                         break;
  134.                 case 18:
  135.                         strcat(chs, "t18.c");
  136.                         strcpy(output, "t18.c");
  137.                         break;
  138.                 case 19:
  139.                         strcat(chs, "t19.c");
  140.                         strcpy(output, "t19.c");
  141.                         break;
  142.                 case 20:
  143.                         strcat(chs, "t20.c");
  144.                         strcpy(output, "t20.c");
  145.                         break;
  146.                 default:
  147.                         strcat(chs, "bianyi.c");
  148.                         strcpy(output, "bianyi.c");
  149.                         break;
  150.                 }
  151.         }
  152.         else
  153.         {
  154.                 strcat(chs, input);
  155.         }

  156.         //打印细节
  157.         printf("要编译的名字为:%s\n", output);
  158.         if (b)
  159.         {
  160.                 printf("已加-lm进行编译!\n");
  161.         }
  162.         else
  163.         {
  164.                 printf("未加-lm进行编译!\n");
  165.         }

  166.         printf("\n\n\n");

  167.         int len = strlen(chs);

  168.         strcat(ch, "gcc ");
  169.         strcat(ch, chs);
  170.         if (b)
  171.         {
  172.                 strcat(ch, " -lm ");
  173.         }
  174.         strcat(ch, " -o ");
  175.         strncat(ch, chs, len - 2);
  176.         strcat(ch, " && ./");
  177.         strncat(ch, chs, len - 2);

  178.         system(ch);

  179.         return 0;
  180. }
复制代码


用法:先将bianyi.c进行gcc编译后,根据你编译后的命名进行直接运行该程序
如我编译后的命名为bianyi,要编译的c文件为t1.c
运行指令:./bianyi t1.c
或者你可以修改开关语句中的你的常用命名,然后直接输入数字进行编译,如 ./bianyi 1
如果运行的有math.h的头文件,可以在.c文件名后加y,如 ./bianyi t1.c y (不加y就默任为n)
需要更牛逼的运行,可以自行修改,加深对C语言的操作!!!加油,奋斗逼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-31 18:42:50 | 显示全部楼层
其实不需要这样的程序
shell有一个alias命令,用来简化输入的命令
如果c文件不多,alias简化一下命令就足够了
如果c文件比较多,那就用makefile
例如你经常使用的ll命令,其实是 ls -alF 的简写

  1. $ alias ll
  2. ll='ls -alF'
  3. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-31 21:55:02 | 显示全部楼层
        不用那么麻烦,试一下:
  1. make t1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 23:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表