如何应对"warning: ignoring return value of ‘system’, "

How to deal with "warning: ignoring return value of ‘system’, "

当我的 c 程序执行到某个点时,我需要 运行 一些 bash shell 命令。我通过谷歌搜索发现 system() 提供了这样的功能。但是,通过 gcc 编译代码会生成如下警告消息,并且 bash 命令不会执行。任何解决方案或替代方案?

warning: ignoring return value of ‘system’, declared with attribute warn_unused_result [-Wunused-result]
system(command);

这是一个库调用,您忽略了其中的 return 值,这就是编译器警告的原因。将其分配给一个变量,检查 return 值 (See man page of system)。该警告与您的 bash 命令未执行无关。