簡體   English   中英

在 macOS 上打開終端/控制台並將標准輸出從 C++ 綁定到它

[英]Open Terminal/Console and bind stdout to it from C++ on macOS

我有一個共享庫,它作為插件從第三方應用程序加載,我想打開一個終端/控制台窗口以查看在調試期間寫入標准輸出的內容。 在 Windows 上,我使用Visual C++ Enable Console來啟用和綁定控制台。 現在我想對 macOS 做同樣的事情,我該怎么做?

您可以使用您指定的 shell 腳本生成Terminal.app 所以:

// Redirect stdout and stderr to a new temp file
char template[] = "/tmp/command_output_XXXXXX";
int fd = mkstemp(template);
dup2(fd, 1);
dup2(fd, 2);

// Prepare script
std::string command(template);
command += ".sh";

std::ofstream out(command);
out << "#!/bin/sh" << std::endl;
out << "exec /usr/bin/tail -f " << template << std::endl;
out.close();

// and go
std::stringstream ss;
ss << "open -a Terminal.app " << command;
system(ss.str().c_str());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM