从应用打开控制台
Opening a console from app
我正在开发一个C和C++应用程序,该应用程序使用一些图形引擎来处理gtk窗口(Opencv/highgui(。这个应用程序做一些小的输出到标准输出/cout。
在Windows上,从桌面启动此类应用程序会自动打开一个控制台,向用户显示标准输出上写入的内容,带有"printf(("或"std::cout"。
在 Linux 上,如果我从以前打开的控制台启动它,没有问题。但是如果我通过桌面(双击(启动它,那么 linux 不会打开关联的控制台,并且在 stdout/cout 上写入的数据会丢失。似乎这是 Linux 上的正常行为(?
我想在 linux 平台上编译时从我的应用程序自动打开控制台。
这似乎是这个的骗局,关键是,它不起作用!我目前有以下代码:
#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( 吗?但是,我如何将该控制台与我的应用程序"连接"?
解决方案 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
arugment 以gnome-terminal
运行应用程序。意义?有点笨拙,但嘿,它有效。
解决方案 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 %dn", ++ i );
sleep( 1 );
}
return 0;
}
您的示例都"打开"了一个控制台 - 从某种意义上说,打开了一个文件。这对 gui 没有任何作用。如果你想这样做,你必须打开一个gtk窗口并将输出定向到它。
- C++控制台应用程序阻止退出
- 如何在 Win32 C++控制台应用程序中调用 UWP 类库
- 什么是仅调用一次并调用参数的控制台应用
- 是否可以从控制台应用程序使用 C++/WinRT 创建窗口?
- 如何创建一个使用开源c项目的c++控制台应用程序
- c ++控制台应用程序,如何在控制台外部打印字符
- 为什么我不能在同一 MS VS 解决方案中的两个控制台应用中使用C++特征(仅标头库)?
- 将控制台应用程序直接输出到Visual Studio而不是外部Windows控制台中
- 如何在 Win32 控制台应用程序中注册不可见的窗口类?
- C++控制台应用,其中有两个冲突的对象不工作
- 如何将从控制台应用程序C++回调设置为C++ COM DLL
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 直接输入控制台应用程序
- C++调试控制台应用程序
- C++控制台应用程序调用C++/CLI静态库
- 如何在控制台应用程序中添加滚动功能以显示更大的输入
- 如何在将Visual Studio C++控制台应用程序连接到Microsoft SQL Server时修复"
- 如何读取windows服务启动的控制台应用程序的输出
- VS2017 中缺少 Win32 控制台应用程序.如何创建C++空项目?
- 将 Visual C++ 6 控制台应用程序作为 Windows 服务运行