Skip to main content
gamedev.net
🔒 Locked

Creating console on linux

Started by surfi 5 replies 1.2k views
Original Post
surfi
surfi
Hello, I'm making a small library for debugging/logging purposes in C++. I've an object that handles output to a console window. In Windows, I use the AllocConsole function to achieve this, in case the program using the library hasn't a console itself. Now I wanto to do that also in Linux, but I don't know how to do it. I've tried with a call to system("xterm"), but that holds the application on that line until the new created console window is closed. I'm trying with the functions openpty(...), login_tty(...) and forkpty(...), but I don't know how it works (actually I don't know if this is the way to go). Does anyone know how to create a console and redirect std::cout to it? Thanks!
droz
droz
Just run your application in a terminal, then you can print to stdout or stderr.
deadstar
deadstar
You can suffix a linux command with an ampersand (&) to let it run by itself. Try this:

system("xterm&")


But I'm still not sure this is the way to go. Any Linux app I've used that requires terminal output actually runs in a terminal in the first place.
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
surfi
surfi
This is a small library, for using in any kind of application (console or not). So since I don't know if the users will launch their application from a terminal, I want to bring a new one, where the output will be done. Actually, output can go to a file, terminal is just another option (for instance, for quickly debuging).

I'll try the options you mentioned. Thanks!
summaky
summaky
Actually, there is no "console or not" application in Linux. It all depends on how you start the application: If you start the application from the command line the shell will, by default, write the output of stdout and stderr to the screen. When running the application from a window manager, it can decide whether to do with the stdout and stderr, so it will depend on the window manager (although most manager will set it to the default, which is the terminal in which X is being run.)

So, there is no concept of "attached" console in a Linux application. Of course it doesn't mean that you can't create your own X window that connects to a file descriptor of your library (perhaps through a named pipe?) but I wouldn't go that route and xterm is not an option because it will just start a new shell unrelated . Under Linux I would just output to stderr and if the user wants to see the errors she can start the application (whatever application) from their preferred terminal. Of course, they can still redirect it to a file or even the network without you having to add this to your library. I don't meant not to include this functionality in your library, I just wanted to point out in Linux we have more flexibility in this regard.
Kambiz
Kambiz
I also suggest not opening an extra terminal under linux, but if you still think it is a good idea an easy method would writing a launcher script/application that executes 'xterm -hold -e ./your_app' .
surfi
surfi
Thanks for the replies. Then I think I'll just tell in the docs the user to start the application from a terminal if they want the output there.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.