简体   繁体   中英

g++ not creating "a.out" file

btw, I know this is a duplicate, but I can't find the answer!

I'm trying to make a C++ file that compiles and runs another C++ file and checks if the output is correct or incorrect (I know that's kinda weird).

(the Desktop folder has a eval.cpp and an a.out + a "test" folder
the test folder has got main.cpp, tytytyt.in, tytytyt.out, tytytyt.ok)

eval.cpp

#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;
}

When I run ~/Desktop$ g++ eval.cpp it creates an a.out file.
Then I run ~/Desktop$ ./a.out .
When my program prints In ce folder se afla fisierul pe care ai dori sa testezi? and I write ~/Desktop/test/main.cpp , the program prints
Compilare sursa cu ajutorul comenzii g++ ~/Desktop/test/main.cpp ...
Rulare fisier...
and then comes the error: sh: 1: /home/steven/Desktop/test/a.out: not found
and if I check the test folder, the file a.out doesn't exist.

Can someone help me? `

Having

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

if the compilation success a.out is produced in the current directory because you do not specify where it must be generated

if you want to generate ~/Desktop/test/a.out replace

string str = "g++ -o a.out" + filename;

by

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

except that I do not understand how the compilation can be done because there is no space after a.out to be separated with the filename , filename is set through cin >> filename; so it is a word and cannot starts by a space or an other separator, are you sure the compilation was done ? You do not even test the result of system ...

For me it must be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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