基础版本的jmake是将所有当前文件夹下的C/C++文件生成单文件编译命令,并且jmake命令不可加选项。
现在做的改进是能在输入命令jmake时加上一些选项了,‘-’开头的选项加入到每个编译单文件的生成命令中去,其他的选项则是指定要编译的源文件。当然,如果没有指定源文件,就把所有.c,.cc,.cpp文件都分别编译。
代码如下:
/* * author: huanglianjing * * this is a program to compile all single c/c++ file of current directory * * usage: jmake <files> <-options> * if no files assigned, all files in directory will be searched * any option will be added to every instruction */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <dirent.h> #include <string.h> #include <sys/stat.h> char file[37][37], option[37][37]; int filenum, optionnum; char suf[37]; int file_in_argv(char *filename)//whether filename in argv { int i; for (i=0; i<filenum; ++i) { if (strcmp(file[i],filename) == 0) return 1; } return 0; } void suffix(char *filename)//get filetype, return empty string if not has { int len = strlen(filename); int pos; for (pos=len-1; pos>=0; --pos) { if (filename[pos] == ‘.‘) break; } if (pos <=0 || pos == len-1) suf[0] = 0; else { int i; for (i=0; i<len-1-pos; ++i) suf[i] = filename[pos+1+i]; suf[len-1-pos] = 0; } } int main(int argc, char *argv[]) { struct dirent *entry; struct stat statbuf; int i; filenum = optionnum = 0; for (i=1; i<argc; ++i) { if (argv[i][0] == ‘-‘) {//option strcpy(option[optionnum++],argv[i]); } else {//file strcpy(file[filenum++],argv[i]); } } DIR *dp = opendir("."); int insnum = 0; while ((entry=readdir(dp)) != NULL) { lstat(entry->d_name,&statbuf); if (!S_ISDIR(statbuf.st_mode)) { char fname[57], ename[57], instruction[207]; strcpy(fname,entry->d_name); strcpy(ename,entry->d_name); if (filenum == 0 || file_in_argv(fname)) ;//consider this file else continue;//not consider this file int len = strlen(fname); suffix(fname); if (strcmp(suf,"c") == 0) {//.c ename[len-2] = ‘\0‘; sprintf(instruction,"gcc %s -o %s",fname,ename); for (i=0; i<optionnum; ++i) { strcat(instruction," "); strcat(instruction,option[i]); } ++insnum; printf("%s\n",instruction); system(instruction); } else if (strcmp(suf,"cc") == 0) {//.cc ename[len-3] = ‘\0‘; sprintf(instruction,"g++ %s -o %s",fname,ename); for (i=0; i<optionnum; ++i) { strcat(instruction," "); strcat(instruction,option[i]); } ++insnum; printf("%s\n",instruction); system(instruction); } else if (strcmp(suf,"cpp") == 0) {//.cpp ename[len-4] = ‘\0‘; sprintf(instruction,"g++ %s -o %s",fname,ename); for (i=0; i<optionnum; ++i) { strcat(instruction," "); strcat(instruction,option[i]); } ++insnum; printf("%s\n",instruction); system(instruction); } } } if (insnum == 0) puts("no file compiled"); closedir(dp); exit(0); }
时间: 2024-11-10 23:58:36