码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-04-01 11:08:02      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/huanglianjing/p/4383210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!