从应用程序打开控制台
Posted
技术标签:
【中文标题】从应用程序打开控制台【英文标题】:Opening a console from app 【发布时间】:2012-05-19 21:23:05 【问题描述】:我正在开发一个 C 和 C++ 应用程序,它使用一些图形引擎来处理 gtk 窗口 (Opencv/highgui)。这个应用程序对 stdout/cout 做了一些小的输出。
在 Windows 上,从桌面启动此类应用程序会自动打开一个控制台,通过“printf()”或“std::cout”向用户显示标准输出中写入的内容。
在 Linux 上,如果我从以前打开的控制台启动它,没有问题。但是如果我通过桌面启动它(双击),那么 linux 不会打开关联的控制台,并且在 stdout/cout 上写入的数据会丢失。 似乎这是 Linux 上的正常行为(?)。
我想在我的应用程序在 linux 平台上编译时自动打开控制台。
这似乎是this one 的欺骗,关键是,它不起作用!我目前有以下代码:
#ifndef __WIN32
filebuf* console = new filebuf();
console->open( "/dev/tty", ios::out );
if( !console->is_open() )
cerr << "Can't open console" << endl;
else
cout.ios::rdbuf(console);
#endif
(使用 freopen() 将 cerr 重定向到文件中)
我不断收到“无法打开控制台”。我尝试替换控制台名称:
console->open( "/dev/console", ios::out );
但这并没有改变。
我的方向正确吗?接下来我可以尝试什么?我应该尝试专门打开终端应用程序(xterm)吗?但是,我怎样才能将该控制台与我的应用程序“连接”呢?
【问题讨论】:
Hmm.. 我知道在某些桌面环境(至少是 GNOME)中,您可以在桌面上创建一个启动器(Windows 术语中的“快捷方式”),并指定从它启动的应用程序是与关联的终端一起运行。如果你想尝试,创建一个新的启动器,然后检查它的属性 - 你会在那里找到选项。不知道这是否是你想要的。 @kebs 如果您在图形环境(如 gnome 终端)下的终端中运行您的应用程序,您将能够看到终端的输出和应用程序打开的窗口。 @fullhack:是的,我知道,但在这种情况下它不是一个选项。 @ShinTakezou:当然,但问题是关于当不从终端启动应用程序时有终端。 我的看法是,通过终端提供有意义信息的应用程序,必须从终端执行... 【参考方案1】:解决方案 1
您可能不喜欢的非常简单的解决方案:拥有一个使用gnome-terminal -x <your_program> <your_args>
在终端中运行您的应用程序的脚本。双击脚本将打开终端。
解决方案 2
更复杂的解决方案为您的应用程序添加一个“--noconsole”参数。如果参数存在,只需运行您的应用程序。如果“--noconsole”不存在:
if( fork() == 0 )
execlp("gnome-terminal", "gnome-terminal", "-x", argv[0], "--noconsole", NULL );
else
exit( 0 );
这将创建一个子进程,它使用--noconsole
参数在gnome-terminal
中运行应用程序。说得通?有点hacky,但是嘿,它有效。
解决方案 3
这是最棘手的解决方案,但在某些方面更优雅。这个想法是将我们的标准输出重定向到一个文件并创建一个运行tail -f <file_name> --pid=<parent_pid>
的终端。这会打印父进程的输出并在父进程死亡时终止。
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
// Create terminal and redirect output to it, returns 0 on success,
// -1 otherwise.
int make_terminal()
char pidarg[256]; // the '--pid=' argument of tail
pid_t child; // the pid of the child proc
pid_t parent; // the pid of the parent proc
FILE* fp; // file to which output is redirected
int fn; // file no of fp
// Open file for redirection
fp = fopen("/tmp/asdf.log","w");
fn = fileno(fp);
// Get pid of current process and create string with argument for tail
parent = getpid();
sprintf( pidarg, "--pid=%d", parent );
// Create child process
child = fork();
if( child == 0 )
// CHILD PROCESS
// Replace child process with a gnome-terminal running:
// tail -f /tmp/asdf.log --pid=<parent_pid>
// This prints the lines outputed in asdf.log and exits when
// the parent process dies.
execlp( "gnome-terminal", "gnome-terminal", "-x", "tail","-f","/tmp/asdf.log", pidarg, NULL );
// if there's an error, print out the message and exit
perror("execlp()");
exit( -1 );
else
// PARENT PROCESS
close(1); // close stdout
int ok = dup2( fn, 1 ); // replace stdout with the file
if( ok != 1 )
perror("dup2()");
return -1;
// Make stdout flush on newline, doesn't happen by default
// since stdout is actually a file at this point.
setvbuf( stdout, NULL, _IONBF, BUFSIZ );
return 0;
int main( int argc, char *argv[])
// Attempt to create terminal.
if( make_terminal() != 0 )
fprintf( stderr, "Could not create terminal!\n" );
return -1;
// Stuff is now printed to terminal, let's print a message every
// second for 10 seconds.
int i = 0;
while( i < 10 )
printf( "iteration %d\n", ++ i );
sleep( 1 );
return 0;
【讨论】:
嘿,谢谢你这么长的回答,很有趣,我会试试看并回复。 解决方案 1 与 @fulhack 的建议没有太大区别(为 gnome 创建一个 .desktop 快捷方式,或为其他桌面创建等效的快捷方式)。您的解决方案可能更便携,但我不确定“gnome-terminal”是否在所有平台上都可用。解决方案 2 很好,我刚刚尝试过,所有内容都包含在应用程序中,但同样需要“gnome-terminal”。也许有一些通用终端可以保证在 all linux 平台上可用?解决方案 3 将在明天尝试。 好吧,如果也可以的话,第三个解决方案实际上与 sol 没有太大区别。 2. 我更喜欢后者,我认为 xterm 作为终端比 gnome-terminal 更常见(例如在 KDE 系统上)。感谢您花时间提供真正完整的答案;-) 没有任何终端可以保证在任何 linux 发行版上都可用,毕竟有人在没有 GUI 的情况下运行 linux,但是是的,xterm 是一个更好的选择。当我开始的时候我实际上不知道如何做到这一点,但是我学到了很多关于文件描述符是如何工作的——这就是为什么喜欢回答 *** 的东西,你学到的东西是完全值得花时间的。 另外,如果您决定使用解决方案 2,您应该注意,在使用 gnome-terminal 重新启动应用程序时,您可能需要将参数传递给应用程序,您应该使用 execvp 而不是 execlp它采用char *argv[]
参数而不是可变参数(请注意,不幸的是,您必须重新分配 argv[] 才能添加“--noconsole”)。【参考方案2】:
您的示例都“打开”了一个控制台-从打开文件的意义上说。这对 gui 没有任何作用。如果你想这样做,你必须打开一个 gtk 窗口并将输出定向到它。
【讨论】:
好的,所以我假设你建议启动一个终端,这是我的第二个想法。但是我怎样才能将这个终端“连接”到我的应用程序的标准输出?以上是关于从应用程序打开控制台的主要内容,如果未能解决你的问题,请参考以下文章
从 Vis C++ 控制台应用程序打开 OpenDialog?