鱼C论坛

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

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

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

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

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

x
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

int main(int argc, char* argv[])
{
        int i, j, n;
        char c;
        char chs[255] = { 0 };
        char ch[255] = { 0 };
        char input[255] = { 0 };
        char* yes[2] = { "y","Y" }, * no[2] = { "n","N" };
        bool b = false, b2 = false;
        char output[255] = { 0 };

        for (i = 0; i < argc; i++)//寻找要编译的下标索引和是否需要-lm编译
        {
                for (j = 0; j < strlen(argv[i]); j++)
                {
                        //查询命令行中输入的是否是.c文件名
                        if (*(argv[i] + j) == '.')
                        {
                                if (*(argv[i] + j + 1) == 'c')//找到就给input
                                {
                                        b2 = true;
                                        strcpy(input, argv[i]);
                                        strcpy(output, input);
                                }
                        }
                        //查询命令行中是否是数字字符串
                        else if (*(argv[i] + j) >= 48 && *(argv[i] + j) <= 57)
                        {
                                b2 = true;
                                strcpy(input, argv[i]);
                        }
                }
                //查找是否有n,N,y,Y
                if (!strcmp(argv[i], yes[0]) || !strcmp(argv[i], yes[1]))
                {
                        b = true;
                }
                else if (!strcmp(argv[i], no[0]) || !strcmp(argv[i], no[1]))
                {
                        b = false;
                }
                else//都没找到,就默认不加-lm
                {
                        b = false;
                }
        }
        if (!b2)//没找到有.c或数字的下标索引
        {
                printf("请手动输入要编译的名称:");
                scanf("%s", input);
                getchar();//吃掉这里多打的回车
                strcpy(output, input);
        }

        if (input[0] >= 48 && input[0] <= 57)
        {
                n = atoi(input);
                switch (n)
                {
                case 0:
                        strcat(chs, "t0.c");
                        strcpy(output, "t0.c");
                        break;
                case 1:
                        strcat(chs, "t1.c");
                        strcpy(output, "t1.c");
                        break;
                case 2:
                        strcat(chs, "t2.c");
                        strcpy(output, "t2.c");
                        break;
                case 3:
                        strcat(chs, "t3.c");
                        strcpy(output, "t3.c");
                        break;
                case 4:
                        strcat(chs, "t4.c");
                        strcpy(output, "t4.c");
                        break;
                case 5:
                        strcat(chs, "t5.c");
                        strcpy(output, "t5.c");
                        break;
                case 6:
                        strcat(chs, "t6.c");
                        strcpy(output, "t6.c");
                        break;
                case 7:
                        strcat(chs, "t7.c");
                        strcpy(output, "t7.c");
                        break;
                case 8:
                        strcat(chs, "t8.c");
                        strcpy(output, "t8.c");
                        break;
                case 9:
                        strcat(chs, "t9.c");
                        strcpy(output, "t9.c");
                        break;
                case 10:
                        strcat(chs, "test.c");
                        strcpy(output, "t10.c");
                        break;
                case 11:
                        strcat(chs, "t11.c");
                        strcpy(output, "t11.c");
                        break;
                case 12:
                        strcat(chs, "t12.c");
                        strcpy(output, "t12.c");
                        break;
                case 13:
                        strcat(chs, "t13.c");
                        strcpy(output, "t13.c");
                        break;

                case 14:
                        strcat(chs, "t14.c");
                        strcpy(output, "t14.c");
                        break;
                case 15:
                        strcat(chs, "t15.c");
                        strcpy(output, "t15.c");
                        break;
                case 16:
                        strcat(chs, "t16.c");
                        strcpy(output, "t16.c");
                        break;
                case 17:
                        strcat(chs, "t17.c");
                        strcpy(output, "t17.c");
                        break;
                case 18:
                        strcat(chs, "t18.c");
                        strcpy(output, "t18.c");
                        break;
                case 19:
                        strcat(chs, "t19.c");
                        strcpy(output, "t19.c");
                        break;
                case 20:
                        strcat(chs, "t20.c");
                        strcpy(output, "t20.c");
                        break;
                default:
                        strcat(chs, "bianyi.c");
                        strcpy(output, "bianyi.c");
                        break;
                }
        }
        else
        {
                strcat(chs, input);
        }

        //打印细节
        printf("要编译的名字为:%s\n", output);
        if (b)
        {
                printf("已加-lm进行编译!\n");
        }
        else
        {
                printf("未加-lm进行编译!\n");
        }

        printf("\n\n\n");

        int len = strlen(chs);

        strcat(ch, "gcc ");
        strcat(ch, chs);
        if (b)
        {
                strcat(ch, " -lm ");
        }
        strcat(ch, " -o ");
        strncat(ch, chs, len - 2);
        strcat(ch, " && ./");
        strncat(ch, chs, len - 2);

        system(ch);

        return 0;
}

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

使用道具 举报

发表于 2022-12-31 18:42:50 | 显示全部楼层
其实不需要这样的程序
shell有一个alias命令,用来简化输入的命令
如果c文件不多,alias简化一下命令就足够了
如果c文件比较多,那就用makefile
例如你经常使用的ll命令,其实是 ls -alF 的简写
$ alias ll
ll='ls -alF'
$ 
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-31 21:55:02 | 显示全部楼层
        不用那么麻烦,试一下:
make t1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-20 23:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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