gpt4 book ai didi

c++ - 在 C++ 程序中连续运行两个 shell 命令

转载 作者:行者123 更新时间:2023-11-28 03:23:39 25 4
gpt4 key购买 nike

以下 shell 命令附有它们执行的操作:

lex q5.l   ->produces lex.yy.c
g++ lex.yy.c -o q5 ->compiles lex.yy.c and produces a relocatable object file

现在我想在 c++ 程序 (main.cpp) 中执行此操作。因此,我在 main.cpp 中执行以下操作:

system("lex q5.l");
system("gcc lex.yy.c -o q5");

但是当我在 shell 中执行 g++ main.cpp -o main 时,会生成以下错误:

q5.l:1: premature EOF
g++: error: lex.yy.c: No such file or directory

当我检查文件夹时,没有lex.yy.c。但是,如果我从 shell 显式运行 lex q5.l,则该文件夹包含 lex.yy.c。所以,我认为 lex q5.l 没有在我的程序 main.cpp 中执行。

谁能指出问题所在以及正确执行 main.cpp 的方法。

最佳答案

在您的程序中,在执行您的 system 调用之前,您不会刷新或关闭编写 q5.l 的流...无法预期输出文件有预期的内容....

关于c++ - 在 C++ 程序中连续运行两个 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744569/

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