关于在Ubuntu上面每次使用gcc ...太麻烦,所以写了一个简易的编译程序,需要自取
#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 = { 0 };
char ch = { 0 };
char input = { 0 };
char* yes = { "y","Y" }, * no = { "n","N" };
bool b = false, b2 = false;
char output = { 0 };
for (i = 0; i < argc; i++)//寻找要编译的下标索引和是否需要-lm编译
{
for (j = 0; j < strlen(argv); j++)
{
//查询命令行中输入的是否是.c文件名
if (*(argv + j) == '.')
{
if (*(argv + j + 1) == 'c')//找到就给input
{
b2 = true;
strcpy(input, argv);
strcpy(output, input);
}
}
//查询命令行中是否是数字字符串
else if (*(argv + j) >= 48 && *(argv + j) <= 57)
{
b2 = true;
strcpy(input, argv);
}
}
//查找是否有n,N,y,Y
if (!strcmp(argv, yes) || !strcmp(argv, yes))
{
b = true;
}
else if (!strcmp(argv, no) || !strcmp(argv, no))
{
b = false;
}
else//都没找到,就默认不加-lm
{
b = false;
}
}
if (!b2)//没找到有.c或数字的下标索引
{
printf("请手动输入要编译的名称:");
scanf("%s", input);
getchar();//吃掉这里多打的回车
strcpy(output, input);
}
if (input >= 48 && input <= 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语言的操作!!!加油,奋斗逼 其实不需要这样的程序
shell有一个alias命令,用来简化输入的命令
如果c文件不多,alias简化一下命令就足够了
如果c文件比较多,那就用makefile
例如你经常使用的ll命令,其实是 ls -alF 的简写
$ alias ll
ll='ls -alF'
$
不用那么麻烦,试一下:
make t1
页:
[1]