jmake 编译当前目录c/c++单文件 指定文件 可加选项

基础版本的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);
}

 

转载于:https://www.cnblogs.com/huanglianjing/p/4383210.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值