• 快速编译c/cpp文件


    • 描述:写一个程序,编译成功得到exe文件,用来编译其他c/cpp文件,方法:拖拽,即将.c/.cpp 文件拖拽到 生成的exe文件上,argv[1]即其路径
    • 思路:使用控制台程序,采用拖拽的方式获得 源文件路径
      • int main(int argc, char *argv[]);
    • 编译原理:system(" gcc -o a.exe a.c"); 
      • 返回值:成功时返回0,失败返回1
    • 可能遇到的问题
      • 所在路径含有空格,在命令行中解读为 不同的参数 
    • 环境:vs2015
    • 二次编译:采用交互式方法,确定用户是否想要替换

     

      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <iostream>
      5 #include <string>
      6 #include <cstring>
      7 using namespace std;
      8 
      9 /************************************************************
     10                         快捷编译器
     11             使用方法:将 .c/.cpp 文件拖到exe图标上
     12 ************************************************************/
     13 
     14 int main(int argc, char *argv[])
     15 {
     16     //char szFileName[1024] = { 0 };
     17     //printf("请输入文件名:");
     18     //scanf("%s", szFileName);
     19 
     20     string szCmdLine1("gcc -o ");
     21     string szCmdLine2;
     22     string szCmdLine3;
     23     string szCmdLine;
     24     int isCreate;        // 通过system 函数返回值判断是否 生成成功
     25     bool isFileValiable = false;
     26     int len = strlen(argv[1]);        //源文件路径长度
     27 
     28     //exe 文件路径
     29     char szFileNameTemp[1024] = { 0 };
     30     strcpy(szFileNameTemp, argv[1]);
     31 
     32     char *szFileNameRev = strrev(szFileNameTemp);    //strrev 将szFileNameTemp反置 并返回首元素指针
     33 
     34     if (0 == strncmp(szFileNameRev, "ppc.", 4))
     35     {
     36         szCmdLine2 = string(argv[1]).substr(0, len - 3);
     37         szCmdLine2 += string("exe");
     38         isFileValiable = true;
     39     }
     40     if (0 == strncmp(szFileNameRev, "c.", 2))
     41     {
     42         szCmdLine2 = string(argv[1]).substr(0, len - 1);
     43         szCmdLine2 += string("exe");
     44         isFileValiable = true;
     45     }
     46     if (false == isFileValiable)
     47     {
     48         printf("
    --->  请拖入正确的c/cpp文件^-^  <---
    
    ");
     49         system("pause");
     50         return 0;
     51     }
     52 
     53     //原文件路径
     54     szCmdLine3 = string(argv[1]);
     55 
     56     //合并
     57     szCmdLine = szCmdLine1 + szCmdLine2 + string(" ") + szCmdLine3;
     58 
     59     //判断之前是否存在文件,如果存在,判断是否替换
     60     FILE *pFile;
     61     pFile = fopen(szCmdLine2.c_str(), "r");
     62     if (NULL != pFile) {
     63         fclose(pFile);
     64         //检测到存在文件 进行交互式判断
     65         printf("Replace the existing one? y/n ");
     66         char ch = getchar();
     67 
     68         //若用户选择 不替换,则退出,其他情况 继续执行
     69         if (ch == 'n' || ch == 'N') {
     70             printf("
    <---成功退出--->
    
    ");
     71             system("pause");
     72             return 0;    //不替换 则退出
     73         }
     74     }
     75 
     76     //cout << szCmdLine << endl;
     77     //cout << argv[1] << endl;
     78     //cout << szCmdLine << endl;
     79 
     80     try {
     81         //system执行成功返回0,失败返回1
     82         isCreate = 1 - system(szCmdLine.c_str());
     83 
     84         if (isCreate) {
     85 
     86             printf("
    <---生成成功--->
    ");
     87             cout << endl;
     88             cout << " -->源文件路径:" << argv[1] << endl;
     89             cout << " -->生成exe文件路径:" << szCmdLine2 << endl << endl;
     90         }
     91         else {
     92             printf("
    <---生成失败--->
    
    ");
     93             printf(" 可能的原因:
    ");
     94             printf("    1.路径中 文件夹名中含有空格, 建议您换个路径重试
    ");
     95             printf("    2.语法错误 即编译错误
    
    ");
     96         }
     97     }
     98     catch (...) {
     99         // failed to create
    100     }
    101 
    102     system("pause");
    103     return 0;
    104 }
    View Code

     

  • 相关阅读:
    ABI与ARM,X86的概念
    数据库升级,如何操作
    shell脚本
    数据库设计范式
    jQuery基础教程
    git clone 失败 fatal: early EOF fatal: the remote end hung up unexpectedly fatal: index-pack failed
    windowserver中PowerShell禁止脚本执行的解决方法
    移动端延迟300ms的原因以及解决方案
    将伪数组转为真正的数组
    cnpm安装时候出现“Unexpected end of JSON input“的解决办法
  • 原文地址:https://www.cnblogs.com/guoyujiang/p/12292545.html
Copyright © 2020-2023  润新知