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);
}
时间: 2024-08-29 23:29:46

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

jmake 编译当前目录所有c/c++单文件

在一个目录下写一些单文件的c或者c++文件时,每次敲出命令如g++ a.cpp -o a感觉比较麻烦. 所以就模仿makefile的功能,实现了扫描当前目录,并将所有c文件.cc文件.cpp文件直接调用gcc/g++编译. 本程序的缺点之一就是不能用于文件间有相互include的情况,因为要扫描代码include了其他什么文件比较麻烦.而且不能在编译命令中加入其他库的选项. 使用方式: 1 jmake 源代码: 1 /* 2 * author: huanglianjing 3 * 4 * thi

dos 下删除文件、文件夹

删除文件 /p 删除每一个文件之前提示确认/f 强制删除只读文件 /s 从当前目录及所有子目录删除指定文件/q 安静模式.删除全局通配符时,不要求确认/a 根据属性选择要删除的文件 指定下列文件属性中的任意一种:属性 说明r 只读a 存档s 系统h 隐藏- 前缀表明“非” del /p/f test\a.txtdel /a:r test\*.txt 删除所以只读 txt 文件(r 为只读属性) 删除文件夹 rd test1 (test1须为空文件夹)rd /s/q test(删除整个文件夹及文件

自动生成版本信息,重新编译指定文件,一键编译IAR工程同时生成hex,bin,out文件

平台:IAR + windows_bat 1.自动生成版本信息 目的: 脚本自动更新程序svn对应的的版本号 前提: svn需要安装command line (参考 https://jingyan.baidu.com/article/a3a3f8113f89198da2eb8aed.html) 主要技术点: 具体.bat脚本主要来源于网络,文件放置在agv_dev_platform->User目录下,编译脚本将生成svnversion.h文件,文件内容仅是SVN_REVISION的一个宏,最终程

Linux编译多个不同目录下的文件以及静态库、动态库的使用

先看两篇博文,作为基础知识.如果对C/C++编译链接过程都了解的话,可以跳过不看. http://www.firedragonpzy.com.cn/index.php/archives/2556 http://www.cppblog.com/shifan3/archive/2007/01/05/17325.html 一.  编译不同目录下的多个文件 各个文件的布局如下: head.h文件的代码: [cpp] view plaincopy #ifndef  HEAD_H #define  HEAD

SuSE12安装MySQL5.7.22:编译安装方式、单实例

摘要:SuSE12.1 64位操作系统. MySQL5.7.22 编译安装.单实例 带boost和不带boost的编译安装 注:kingtry是我的主机名 一.环境准备 操作系统:SuSE版本12.1,64位 kingtry:~ # uname -a Linux kingtry 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64 GNU/Linux kingtry:

JavaSE8基础 File list 获取指定文件夹下的第一层文件和文件夹的名字

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0) information: 被查看文件夹中的内容截图. code: package jizuiku0; import java.io.File; /* * @version V17.09 */ public class GetName { public static void main(String[] args) { String

【转载】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 &ndash; 指定文件编码类型

下面的内容来自:http://www.cnblogs.com/blueskylcc/p/5332642.html, 对方也是转的,不过没有找到转载的出处: 1.#!/usr/bin/python 是用来说明脚本语言是 python 的 是要用 /usr/bin下面的程序(工具)python,这个解释器,来解释 python 脚本,来运行 python 脚本的. 2.# -*- coding: utf-8 -*- 是用来指定文件编码为 utf-8 的 详情可以参考:PEP 0263 - Defin

使用ajax上传表单(带文件)

在使用form表单的时候上传文件+表单,会使得页面跳转,而在某些时候不希望跳转,只变化页面中的局部信息 通过查找资料,可以使用FormData进行ajax操作. FormData介绍:XMLHttpRequest Level 2添加了一个新的接口FormData.利用FormData对象,我们可以通过JavaScript用一些键值对来模拟一系列表单控件,我们还可以使用XMLHttpRequest的send()方法来异步的提交这个"表单".比起普通的ajax,使用FormData的最大优

Github用.gitignore忽略指定文件

.gitignore Github提供.gitignore这种功能,可以自己指定哪些文件可以不被管理.具体方法是在版本管理的根目录下(与.git文件夹同级)创建一个.gitignore. 应用实例 项目中有clist.h clist.c  main.c三个文件,编译执行后,生成了三个文件  clist.o  main.o   main.这三个文件是不需要进行版本管理的,所以需要忽略这些文件,使用 git  stauts查看后,发现这三个文件也是处于 Untracked files状态.而实际上我