简体   繁体   English

调用shell命令从Ruby运行C ++代码

[英]Calling shell command to run C++ code from Ruby

I have a compiled c++ file that I would like to run from within a temporary directory in ruby: 我有一个编译的c ++文件,我想在ruby的临时目录中运行:

folder = Dir.mktmpdir
      begin
      puts folder
      File.open("#{folder}/input.xml", 'w') { |file| file.write(builder.to_xml) }

      ensure
      system '.././program'
      FileUtils.remove_entry folder
      end

The above code properly creates all the desired file/folder, but does not run the system call, and does not produce an error. 上面的代码正确创建了所有所需的文件/文件夹,但没有运行系统调用,也不会产生错误。 I have tried the all of the varieties listed here: Calling shell commands from Ruby but cannot seem to get it to function. 我尝试了这里列出的所有功能: 从Ruby调用shell命令,但似乎无法使其正常运行。 If I comment out the file deletion line and manually go and execute the command listed above, the program functions properly. 如果我注释掉文件删除行并手动执行上面列出的命令,该程序将正常运行。 Also, if I instead save the file to the same location as the ./program, the ./program works. 另外,如果我将文件保存到./program所在的位置,则./program也可以工作。 What is preventing me from running the command from a sub-folder? 是什么使我无法从子文件夹运行命令?

maybe system finding a program has name .././program 也许系统找到一个程序的名称是.././program

try the backtick operator or %x function 尝试反引号运算符或%x函数

puts `.././program`

puts %x{.././program}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM