Jmake compiling the current directory C + + Single file specifies the file to add options

Source: Internet
Author: User
Tags sprintf strcmp

The base version of Jmake is to generate a single-file-compilation command for all C + + files under the current folder, and the Jmake command is not optional.

Now the improvement is to be able to add some options when entering the command jmake, '-' the options that begin with are added to the build command for each compiled single file, and the other option is to specify the source file to be compiled. Of course, if the source file is not specified, all the. c,.cc,.cpp files are compiled separately.

The code is as follows:

/** author:huanglianjing * * * This is a program to compile all a C + + file of current directory * * Usage:jmak E <files> <-options> * If no files assigned, all files in directory would be searched * any option would be Adde D to every instruction*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<dirent.h>#include<string.h>#include<sys/stat.h>Charfile[Panax Notoginseng][Panax Notoginseng], option[Panax Notoginseng][Panax Notoginseng];intFileNum, Optionnum;Charsuf[Panax Notoginseng];intFILE_IN_ARGV (Char*filename)//whether filename in argv{    inti;  for(i=0; i<filenum; ++i) {if(strcmp (file[i],filename) = =0)return 1; }    return 0;}voidSuffixChar*filename)//get filetype, return empty string if not have{    intLen =strlen (filename); intPOS;  for(pos=len-1; pos>=0; --POS) {        if(Filename[pos] = ='.') Break; }    if(Pos <=0|| pos = = len-1) suf[0] =0; Else {        inti;  for(i=0; i<len-1-pos; ++i) Suf[i] = filename[pos+1+i]; Suf[len-1-pos] =0; }}intMainintargcChar*argv[]) {    structDirent *entry; structstat statbuf; inti; FileNum= Optionnum =0;  for(i=1; i<argc; ++i) {if(argv[i][0] =='-') {//optionstrcpy (option[optionnum++],argv[i]); }        Else{//filestrcpy (file[filenum++],argv[i]); }} DIR*DP = Opendir ("."); intInsnum =0;  while((Entry=readdir (DP))! =NULL) {Lstat (Entry->d_name,&statbuf); if(!S_isdir (Statbuf.st_mode)) {            Charfname[ $], ename[ $], 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 the this file            intLen =strlen (fname);            Suffix (fname); if(strcmp (Suf,"C") ==0) {//. Cename[len-2] =' /'; 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) {//. CCename[len-3] =' /'; 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) {//. cppename[len-4] =' /'; 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 compiling the current directory C + + Single file specifies the file to add options

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.