我有一个生成 dependencies.dot
的应用程序然后我想将其转换为 SVG 图像的文件。
当我从这样的简单应用程序中执行此操作时:
int main(int argc, char * argv[])
{
system("dot -Tsvg ../BUILD/dependencies.dot > ../BUILD/dependencies.svg");
return 0;
}
它工作得很好。 SVG 图像存在并且正在工作。当我从我的 Qt 应用程序运行它时,SVG 文件被创建(由 shell),但它仍然是空的。
知道什么可以阻止
dot
从输出数据到它的stdout
?以防万一,我还尝试了
cat
通过 stdin
发送输入而不是文件名:system("cat ../BUILD/dependencies.dot | dot -Tsvg > ../BUILD/dependencies.svg");
这没有任何区别。同样使用完整路径(
/usr/bin/dot
)也无济于事。另一个测试,我尝试使用
popen()
和第一个 fread()
立即返回 0(即 EOF 的标记)。它可能不是 Qt,但有些东西正在与
dot
交互做任何事的能力。关于为什么会这样的任何指示都会很棒。也许是一个重要的注意事项?我启动我的应用程序。从控制台,所以
stdin
, stdout
和 stderr
应该都按预期工作。我实际上可以看到那里出现调试日志,并且其他应用程序似乎按预期工作(例如,我的 Qt 应用程序可以成功运行 make
)。这是生成的 SVG 的示例(当我不在 Qt 应用程序中运行它时):
作为引用,可以在 github 上找到源代码。这是 snapbuilder 的一部分。我用来在启动板上运行构建的工具。它仍然不完整,但它正在到达那里。
https://github.com/m2osw/snapcpp/tree/master/snapbuilder
要查找的具体函数:
project::generate_svg()
.
最佳答案
我仍然不知道 Qt 对 system()
有什么副作用该dot
命令会失败。但是,如果使用我自己的 fork()
+ execve()
,然后它工作。
我想要一个新的 process class对于我的环境,所以我实现了它。这个较新的版本使用 FIFO 或直接打开它传递给进程的关闭文件。
...
// write the script in `std::stringstream dot` then:
//
std::string script(dot.str());
g_dot_process = std::make_shared<cppprocess::process>("dependencies");
g_dot_process->set_command("/usr/bin/dot");
g_dot_process->add_argument("-Tsvg");
g_dot_process->add_input(cppprocess::buffer_t(script.data(),
script.data() + script.length()));
g_dot_process->set_capture_output();
g_dot_process->set_output_capture_done(output_captured);
g_dot_process->start(); // TODO: check return value for errors
...
// and in output_captured()
//
void snap_builder::svg_ready(std::string const & svg)
{
std::string const svg_filename(...);
{
std::ofstream out;
out.open(svg_filename);
if(!out.is_open())
{
std::cerr << "error: \n";
return;
}
out.write(svg.c_str(), svg.size());
}
dependency_tree->load(QString::fromUtf8(svg_filename.c_str()));
}
现在点文件已生成并按预期显示。这很奇怪,因为我用简单的
system()
完成了大多数其他事情。通话按预期工作。 stdin
一定有什么东西或 stdout
这使得 dot
不做它的工作。
关于c++ - 从 Qt 应用程序运行命令行点程序不会产生任何输出,这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67863411/