1

我想制作一个系统,我可以在一个程序中运行一个 make 文件和其他几个与 gcc 相关的东西,基本上是在程序中使用 gcc 和编译东西。如果我把我想做的所有事情都写到一个批处理文件中,那么我需要从程序中运行那个批处理文件。

现在每个人都说System(),由于安全性和其他各种原因,通话非常糟糕。因此,考虑到我正在使用 c++System()来运行批处理文件,这将是一个不错的选择。如果更可取,我想要跨平台的替代方案。

谢谢

4

1 回答 1

2

You could look to use the fork and execl family of calls although these are tied down to Unix / Linux and, depending on how you use them, are arguably no safer than system.

I doubt very much that you'll find a common, cross platform way of doing this if only because all platforms will have different and unique ways of doing this. Also, the scripts you're trying to run will no doubt have to be different on different platforms and there may be different ways of specifying things such as directory paths etc.

My suggestion would be to first ask yourself how you'll take the following questions - which would be my main concerns:

  • How am I going to prevent accidental / intentional misuse?
  • How am I going to detect errors or success status within the scripts I'm running?
  • How am I going to provide for dependencies? E.g. script A must run completely and correctly before script B runs.
  • How am I going to report the success and failure state.

My final question would be why do you want to do this in C++? Is there a specific reason? Naturally I'm a C++ evangelist although I would have thought this would be better tackled by a scripting language such as Perl, Python or possibly Bash unless you're embarking on something far more radical.

于 2012-08-01T15:02:36.107 回答