I just moved from Windows to Linux and I’m try to create a simple application that opens a console, displays a message and wait for key press to close. I have created it on Windows and it works, then I just moved the files to Linux. Didn’t make any change, just compiled it with g++ and I get no errors. The problem is that on Linux (Ubuntu 12.04) I can’t see the console and some message asking me to press any key before closing. My code is as simple as this:
#include <iostream>
#include <cstdio>
int main() {
cout << "Writing file...\n";
FILE *myfile = fopen("testfile.txt", "w");
fwrite("test", sizeof(char), 4, myfile);
fclose(myfile);
cout << "Press any key to exit...\n";
cin.ignore();
return 0;
}
On Windows, when I start the executable, the console windows will show me the messages and close when pressing any key. On Linux, when I execute the program I don’t get anything. It does create the testfile.txt file and insert the text, so cstdio related functions does work, but I can’t see any console with those messages and I don’t understand why. Maybe I don’t know how to open a simple executable on Linux. What I want is to double click on it and see a console with two simple messages. What do you thin I’m doing wrong? Thanks!
Also, I use g++ to compile the cpp file: g++ -Wall -s -O2 test.cpp -o test
On Windows the “natural” form of an application is a GUI application. When you run a console application the system creates a window to run a console and runs the application in that window. That is done by Windows, it is not an inherent property of C++ and is not implied by the code you wrote.
C++ doesn’t do this automatically and UNIX-like systems don’t do this for you.
On UNIX-like systems the “natural” type of application is (arguably) a console application and you would typically run them from a console or terminal.
When you run your program the output goes to the terminal where your X11 session is running, but you don’t see it because the X11 session is controlling your display.
So to get the behaviour you want, first open a terminal, then run the program.
To make the program run in a terminal, try running something like
xterm -e ./testTo make that automatic you could kluge it with something like:
Now if you run the program with the argument
-xtermit will run in an xterm.N.B. I fixed your non-portable code to use
std::qualification on the names from<cstdio>