gpt4 book ai didi

c++ - 从应用程序打开控制台

转载 作者:太空狗 更新时间:2023-10-29 23:53:29 25 4
gpt4 key购买 nike

我正在开发一个 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

(cerr 使用 freopen() 在文件中重定向)

我不断收到“无法打开控制台”。我尝试替换控制台名称:

console->open( "/dev/console", ios::out );

但这并没有改变。

我的方向正确吗?接下来我可以尝试什么?我应该尝试专门打开终端应用程序 (xterm) 吗?但是,我如何才能将该控制台与我的应用程序“连接”起来呢?

最佳答案

解决方案一

您可能不喜欢的非常简单的解决方案:使用脚本在终端中运行您的应用程序 gnome-terminal -x <your_program> <your_args> .双击脚本将打开终端。

方案二

稍微复杂一点的解决方案向您的应用程序添加一个“--noconsole”参数。如果参数存在,只需运行您的应用程序。如果“--noconsole”不存在:

if( fork() == 0 ) {
execlp("gnome-terminal", "gnome-terminal", "-x", argv[0], "--noconsole", NULL );
} else {
exit( 0 );
}

这会创建一个子进程,在其中运行 gnome-terminal 中的应用程序使用 --noconsole争论。说得通?有点 hacky,但是,嘿,它有效。

方案三

这是最棘手的解决方案,但在某些方面更优雅。这个想法是将我们的标准输出重定向到一个文件并创建一个运行 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;
}

关于c++ - 从应用程序打开控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10668958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com