Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:27:53+00:00 2026-06-15T19:27:53+00:00

I just moved from Windows to Linux and I’m try to create a simple

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:27:54+00:00Added an answer on June 15, 2026 at 7:27 pm

    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 ./test

    To make that automatic you could kluge it with something like:

    #include <iostream>
    #include <string>
    #include <unistd.h>
    #include <cstdio>
    
    int main(int argc, char** argv)
    {
      if (argc > 1 && std::string(argv[1]) == "-xterm")
      {
        if (::execl("/usr/bin/xterm", "xterm", "-e", argv[0], (char*)NULL))
        {
          std::perror("execl");
          return 1;
        }
      }
    
      std::cout << "Writing file...\n";
    
      FILE* myfile = std::fopen("testfile.txt", "w");
      std::fwrite("test", sizeof(char), 4, myfile);
      std::fclose(myfile);
    
      std::cout << "Press any key to exit...\n";
      std::cin.get();
    }
    

    Now if you run the program with the argument -xterm it will run in an xterm.

    N.B. I fixed your non-portable code to use std:: qualification on the names from <cstdio>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently moved my app from a linux machine to windows, and I
I just moved from an Ubuntu PHP workstation dev environment back to Windows and
I just moved from .net development to LINUX MONO development... and i don have
We just moved our application from a MDI container to a single document interface.
I just moved my Wordpress site from using nginx to Apache2, and found that
I just started with WPF. Moved from Window Form. Where do those openDialog, saveDialog
I just moved from Visual Studio 2008 to Visual Studio 2010 (final) and noticed one major
So i have just moved everything from my localhost to a new online server
I've just moved over from shared to dedicated hosting, and suddenly POST is not
I have just moved a site from a dedicated server to a GoDaddy shared

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.