YLFCY_ 2022-07-03 22:07 采纳率: 0%
浏览 29

G++编译时遇到错误,如何解决?

问题遇到的现象和发生背景

是这样的,我今天打算用一个程序实现中文编代码,所以我将G++复制进目录来想直接调用,但发生了奇怪的错误。

问题相关代码,请勿粘贴截图
#include<bits/stdc++.h>
#include<windows.h>
#include<wchar.h>
#include<Shlobj.h>
#define elif else if
//感谢:https://www.5axxw.com/questions/content/cuot5e
using namespace std;
string str[100000];
BOOL SetConsoleColor(WORD wAttributes) {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hConsole == INVALID_HANDLE_VALUE)
        return FALSE;

    return SetConsoleTextAttribute(hConsole, wAttributes);
}
string GetEeEPath() {
    TCHAR szFilePath[MAX_PATH + 1] = { 0 };
    string strPath2 = "";
    GetModuleFileName(NULL, szFilePath, MAX_PATH);
    (strrchr(szFilePath, '\\'))[0] = 0;                //删除文件名,只获得路径字串
    strPath2 = (string)szFilePath;
    return strPath2;
}//感谢:https://www.csdn.net/tags/NtTaEg1sMzAtYmxvZwO0O0OO0O0O.html
void charTowchar(const char *chr, wchar_t *wchar, int size) {
    MultiByteToWideChar( CP_ACP, 0, chr,
                         strlen(chr)+1, wchar, size/sizeof(wchar[0]) );
}//感谢:https://blog.csdn.net/weixin_30826761/article/details/96334010
wchar_t *wchar = (wchar_t *)malloc(sizeof(wchar_t) * 64);
string stringbianliangming[100000];
int main(int argc, char** argv) {
    for(int u=0; u<100000; u++) {
        stringbianliangming[u]="";
    }
    cout<<"请将文件拖入这里后按回车  -->   ";
    string wjm;
    getline(cin,wjm);
    wjm.erase(std::remove(wjm.begin(),wjm.end(),'\"'),wjm.end());
    string str_url = _pgmptr;
    for(int i=str_url.length(); i>=0; i--) {
        if(str_url[i]=='\\') {
            break;
        } else {
            str_url[i]=0;
        }
    }
    cout<<"convertmaster: convert "<<wjm<<" to "<<str_url<<"..."<<endl;
    freopen(wjm.c_str(),"r",stdin);
    FILE *fp;
    fp=fopen("out.cpp","w");
    if(fp==NULL) {
        cout<<"fatal:FILE error!"<<endl;
    }
    fflush(fp);
    FILE *pcon;
    pcon=fopen("CON","w");
    ofstream fout1("out.cpp",ios::out);
    fprintf(fp,"#include<bits/stdc++.h>\n using namespace std;\n #define elif else if\n int main(){");
    fprintf(pcon,"print includes...\n");
    fprintf(fp,"//By YLFCY\n");
    string::size_type idx;
    string::size_type lili;
    string finds="输出";
    string bianliang="定义变量";
    string zhengxing="整型";
    string floatss="浮点型";
    string stringss="字符串";
    string boolss="布尔型";
    int zhegeshizhendeu=0;
    for(int i=0; getline(cin,str[i]); i++) {
        idx=str[i].find(finds);
        if(i!=0) {
            fprintf(fp,"\n");
        }
        if(idx!=string::npos) {
            fprintf(fp,"cout<<");
            if(str[i][4]=='"') {
                fprintf(fp,"\"");
                string aaaaa="";
                for(int k=5; k<=str[i].length()-2; k++) {
                    aaaaa+=str[i][k];
                }
                fprintf(fp,aaaaa.c_str());
                fprintf(fp,"\"");
            } else {
                string aaaaa="";
                for(int k=4; k<=str[i].length()-1; k++) {
                    aaaaa+=str[i][k];
                }
                fprintf(fp,aaaaa.c_str());
            }
            fprintf(fp,";");
        } else {
            idx=str[i].find(bianliang);
            if(idx!=string::npos) {
                string::size_type zx=str[i].find(zhengxing);
                string::size_type fd=str[i].find(floatss);
                string::size_type zf=str[i].find(stringss);
                string::size_type br=str[i].find(boolss);
                if(zx!=string::npos) {
                    fprintf(fp,"int ");
                }
                if(fd!=string::npos) {
                    fprintf(fp,"double ");
                }
                if(zf!=string::npos) {
                    fprintf(fp,"string ");
                }
                if(br!=string::npos) {
                    fprintf(fp,"bool ");
                }
                string strstrstrstr;
                for(int jjj=8; str[i][jjj]!='='; jjj++) {
                    strstrstrstr+=str[i][jjj];
                    //if(!strstrstrstr[0]<0)
                }
                fprintf(fp,strstrstrstr.c_str());
                fprintf(fp," = ");
                bool lr;
                if(zx!=string::npos) {
                    lr=false;
                    string scscsc="";
                    for(int jjj=8; jjj<str[i].length()-5; jjj++) {
                        if(lr) {
                            if(str[i][jjj]>='0'&&str[i][jjj]<='9') {
                                scscsc+=str[i][jjj];
                            }
                        } else {
                            if(str[i][jjj]=='=') {
                                lr=true;
                            }
                        }
                    }
                    charTowchar(scscsc.c_str(), wchar, sizeof(wchar_t) * 64);
                    fwprintf(fp,wchar);
                }
                if(fd!=string::npos) {
                    lr=false;
                    string scscsc="";
                    for(int jjj=8; jjj<str[i].length()-7; jjj++) {
                        if(lr) {
                            if((str[i][jjj]>='0'&&str[i][jjj]<='9')||str[i][jjj]=='.') {
                                scscsc+=str[i][jjj];
                            }
                        } else {
                            if(str[i][jjj]=='=') {
                                lr=true;
                            }
                        }
                    }
                    fprintf(fp,scscsc.c_str());
                }
                if(zf!=string::npos) {
                    stringbianliangming[zhegeshizhendeu]=strstrstrstr;
                    fprintf(fp,"\"");
                    lr=false;
                    string scscsc="";
                    for(int jjj=8; jjj<str[i].length()-7; jjj++) {
                        if(lr) {
                            if((str[i][jjj]>='A'&&str[i][jjj]<='Z')||(str[i][jjj]>='a'&&str[i][jjj]<='z')||(str[i][jjj]>='0'&&str[i][jjj]<='9')) {
                                scscsc+=str[i][jjj];
                            } else {
                                system("start CE.exe");
                                exit(0);
                            }
                        } else {
                            if(str[i][jjj]=='=') {
                                lr=true;
                            }
                        }
                    }
                    zhegeshizhendeu++;
                    fprintf(fp,scscsc.c_str());
                    fprintf(fp,"\"");
                }
                if(br!=string::npos) {
                    //fprintf(fp,"test");
                    lr=false;
                    string scscsc="";
                    for(int jjj=7; jjj<=str[i].length()-8; jjj++) {
                        if(lr) {
                            //if((str[i][jjj]>='A'&&str[i][jjj]<='Z')||(str[i][jjj]>='a'&&str[i][jjj]<='z')) {
                            scscsc+=str[i][jjj];
                        } else {
                            if(str[i][jjj]=='=') {
                                lr=true;
                            }
                        }
                    }
                    if(scscsc=="true"||scscsc=="True"||scscsc=="TRUE") {
                        fprintf(fp,"true");
                    } else {
                        if(scscsc=="false"||scscsc=="False"||scscsc=="FALSE") {
                            fprintf(fp,"false");
                        } else {
                            system("start CE.exe");
                            exit(0);
                        }
                    }
                }
                fprintf(fp,";");
            } else {
                string xiugaibianliang="修改变量";
                idx=str[i].find(xiugaibianliang);
                if(idx!=string::npos) {
                    string _strstrstrstr;
                    for(int jjj=8; str[i][jjj]!='='; jjj++) {
                        _strstrstrstr+=str[i][jjj];
                        //if(!strstrstrstr[0]<0)
                    }
                    //cout<<_strstrstrstr<<endl;
                    fprintf(fp,_strstrstrstr.c_str());
                    fprintf(fp," = ");
                    bool _isstring=false;
                    for(int w=0; w<zhegeshizhendeu; w++) {
                        if(_strstrstrstr==stringbianliangming[w]) {
                            _isstring=true;
                        }
                    }
                    if(_isstring) {
                        fprintf(fp,"\"");
                        bool lr=false;
                        string scscsc="";
                        for(int jjj=8; jjj<str[i].length(); jjj++) {
                            if(lr) {
                                if((str[i][jjj]>='A'&&str[i][jjj]<='Z')||(str[i][jjj]>='a'&&str[i][jjj]<='z')||(str[i][jjj]>='0'&&str[i][jjj]<='9')) {
                                    scscsc+=str[i][jjj];
                                } else {
                                    system("start CE.exe");
                                    exit(0);
                                }
                            } else {
                                if(str[i][jjj]=='=') {
                                    lr=true;
                                }
                            }
                        }
                        fprintf(fp,scscsc.c_str());
                        fprintf(fp,"\"");
                    } else {
                        bool lr=false;
                        string scscsc="";
                        for(int jjj=0; jjj<str[i].length(); jjj++) {
                            if(lr) {
                                if(str[i][jjj]!='"') {
                                    scscsc+=str[i][jjj];
                                } else {
                                    system("start CE.exe");
                                    exit(0);
                                }
                            } else {
                                if(str[i][jjj]=='=') {
                                    lr=true;
                                }
                            }
                        }
                        fprintf(fp,scscsc.c_str());
                    }
                    fprintf(fp,";");
                } else {
                    string ruguo="如果";
                    idx=str[i].find(ruguo);
                    lili=str[i].find("否则");
                    if((idx!=string::npos)&&(lili==string::npos)) {
                        fprintf(fp,"if(");
                        string abababab="";
                        for(int jj=4; jj<str[i].length()-2; jj++) {
                            abababab+=str[i][jj];
                        }
                        fprintf(fp,abababab.c_str());
                        fprintf(fp,"){");
                    } else {
                        if(str[i]=="结束") {
                            fprintf(fp,"}");
                        }
                        elif(str[i]=="否则") {
                            fprintf(fp,"else{");
                        }
                        elif(str[i]=="换行") {
                            fprintf(fp,"cout<<endl;");
                        }
                        else {
                            string shururu="输入";
                            idx=str[i].find(shururu);
                            if(idx!=string::npos) {
                                fprintf(fp,"cin>>");
                                string shurubianliang="";
                                for(int _1l=4; _1l<str[i].length(); _1l++) {
                                    shurubianliang+=str[i][_1l];
                                }
                                fprintf(fp,shurubianliang.c_str());
                                fprintf(fp,";");
                            } else {
                                string l1l11l="否则如果";
                                idx=str[i].find(l1l11l);
                                if(idx!=string::npos) {
                                    fprintf(fp,"elif(");
                                    string abababab123="";
                                    for(int jj=8; jj<str[i].length()-2; jj++) {
                                        abababab123+=str[i][jj];
                                    }
                                    fprintf(fp,abababab123.c_str());
                                    fprintf(fp,"){");
                                }
                            }
                        }
                    }
                }
            }
        }
        fprintf(pcon,"第");
        int aaaaaaaaaaaaaaaa=i+1;
        char shuchushuchushuchu[256];
        itoa(aaaaaaaaaaaaaaaa,shuchushuchushuchu,10);
        fprintf(pcon,shuchushuchushuchu);
        fprintf(pcon,"行完成...\n");
    }
    fprintf(fp,"\nsystem(\"pause\");\nreturn 0;}");
    fclose(fp);
    fclose(pcon);
    cout<<"转换好的c++代码文件已经存放至 ";
    SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
    cout<<str_url<<"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bout.cpp";
    SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    cout<<" 并准备好编译。"<<endl;
    cout<<"开始编译独立文件..."<<endl;
    cout<<"...准备中"<<endl;
    string chaobiao[24]={"&","(",")","{","}","[","]",",","`"};
    string bianyimingling="\""+GetEeEPath()+"\\MinGW64\\bin\\g++.exe\"";
    string::size_type weigui;
    for(int i=0;i<9;i++)
    {
        weigui=bianyimingling.find(chaobiao[i]);
        if(weigui!=string::npos)
        {
            bianyimingling="MinGW64\\bin\\g++.exe";
            cout<<"发现文件夹名包含非法字符,为防止编译失败,已为您更改编译程序打开方式(绝对路径—>相对路径)"<<endl;
            cout<<chaobiao[i]<<endl;//测试代码 
            break;
        }
    }
    char desktoppath[255];
    SHGetSpecialFolderPath(
        NULL,
        desktoppath,
        CSIDL_DESKTOPDIRECTORY,
        FALSE
    );
    {
        bianyimingling+=" \"";
        bianyimingling+=GetEeEPath();
        bianyimingling+="\\out.cpp\" -o \"";
        bianyimingling+=desktoppath;
        bianyimingling+="\\out.exe\" -m32 -I\"";
        bianyimingling+=GetEeEPath();
        bianyimingling+="\\MinGW64\\include\" -I\"";
        bianyimingling+=GetEeEPath();
        bianyimingling+="\\MinGW64\\x86_64-w64-mingw32\\include\" -I\"";
        bianyimingling+=GetEeEPath();
        bianyimingling+="\\MinGW64\\lib\\gcc\\x86_64-w64-mingw32\\4.9.2\\include\" -I\"";
        bianyimingling+=GetEeEPath();
        bianyimingling+="\\MinGW64\\lib\\gcc\\x86_64-w64-mingw32\\4.9.2\\include\\c++\" -L\"";
        bianyimingling+=GetEeEPath();
        bianyimingling+="\\MinGW64\\x86_64-w64-mingw32\\lib32\" -static-libgcc -m32";
    }/*这段加不加都一样 测试过了 
    for(int i=0;i<bianyimingling.length();i++)
    {
        if(bianyimingling[i]=='\\'){
            bianyimingling[i]='/';
        }
    }*/
    SetConsoleColor(FOREGROUND_GREEN);
    cout<<"Cmd:"<<bianyimingling.data()<<endl;
    SetConsoleColor(FOREGROUND_GREEN| FOREGROUND_INTENSITY| BACKGROUND_INTENSITY);
    cout<<"- 输出文件名:"<<desktoppath<<"\\out.exe"<<endl;
    SetConsoleColor(FOREGROUND_RED);
    freopen("CON", "r", stdin);
    if(system(bianyimingling.data())!=0) {
        cout<<"编译错误!"<<endl;
    } else {
        SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
        cout<<"编译进程结束。"<<endl;
    }
    SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
    system("pause");
    return 0;
}

运行结果及报错内容

但是,当我往程序里边拖文件并按回车后,如果文件夹包含非法字符会报错:

img

否则,会报错:

img

我的解答思路和尝试过的方法

我还把这个程序打了个包,所以这个编译程序是从快捷方式访问的。
我尝试过把路径里** 改成 **/**,但是没用。
命令粘贴到cmd中可以正常运行。
程序在特定情况下可以编译成功,例如在某些情况下直接从目录启动而非通过快捷方式。

我想要达到的结果

如果你有什么好的改进建议,请帮帮我,因为这个程序对我来说还是相当重要的。谢谢您!

  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-07-04 09:38
    关注

    程序员的常识之一:不要将程序源代码或项目或exe放在桌面这种复杂(多半带空格和汉字且在防火墙或杀毒软件严密控制的C盘上)的目录下。

    评论

报告相同问题?

问题事件

  • 修改了问题 7月3日
  • 创建了问题 7月3日

悬赏问题

  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上
  • ¥15 装完kali之后下载Google输入法 重启电脑后出现以下状况 且退不出去 桌面消失 反复重启没用
  • ¥15 ESP-IDP-BLE配网连接wifi