繁体   English   中英

g++ 不创建“a.out”文件

[英]g++ not creating "a.out" file

顺便说一句,我知道这是重复的,但我找不到答案!

我正在尝试制作一个 C++ 文件来编译并运行另一个 C++ 文件并检查输出是正确还是不正确(我知道这有点奇怪)。

(桌面文件夹有一个 eval.cpp 和一个 a.out + 一个“test”文件夹
测试文件夹有 main.cpp, tytytyt.in, tytytyt.out, tytytyt.ok)

评估文件

#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#include <string>
using namespace std;
ifstream fa("~/Desktop/test/tytytyt.out");
ifstream fok("~/Desktop/test/tytytyt.ok");
string filename;
int a, ok;
int main()
{
    cout << "In ce folder se afla fisierul pe care ai dori sa testezi? \n";
    cin >> filename;
    string str = "g++ -o a.out" + filename;
    const char * command = str.c_str();
    cout << "Compilare sursa cu ajutorul comenzii " << command << " ... \n";
    system(command);
    cout << "Rulare fisier... \n";
    system("~/Desktop/test/a.out");
    fa >> a;
    fok >> ok;

    if(a == ok) cout << "Corect!";
    else cout << "Incorect!";
    return 0;
}

当我运行~/Desktop$ g++ eval.cpp它会创建一个a.out文件。
然后我运行~/Desktop$ ./a.out
当我的程序打印In ce folder se afla fisierul pe care ai dori sa testezi? 我写~/Desktop/test/main.cpp ,程序打印
Compilare sursa cu ajutorul comenzii g++ ~/Desktop/test/main.cpp ...
Rulare fisier...
然后出现错误: sh: 1: /home/steven/Desktop/test/a.out: not found
如果我检查 test 文件夹,文件a.out不存在。

有人能帮我吗? `

拥有

string str = "g++ -o a.out" + filename; const char * command = str.c_str(); cout << "Compilare sursa cu ajutorul comenzii " << command << " ... \\n"; system(command);

如果编译成功a.out是在当前目录下产生的,因为你没有指定必须在哪里产生

如果你想生成~/Desktop/test/a.out替换

string str = "g++ -o a.out" + 文件名;

经过

string str = "g++ -o ~/Desktop/test/a.out" + filename;

除了我不明白编译是如何完成的,因为a.out后面没有空格与文件分开,文件是通过cin >> filename; 所以它是一个单词,不能以空格或其他分隔符开头,你确定编译完成了吗? 你甚至不测试系统的结果......

对我来说必须是

string str = "g++ -o ~/Desktop/test/a.out " + filename;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM