简体   繁体   中英

Does running a compiled C file in Ruby capture C's printf()?

I'm on ruby-2.2.2 (on Windows, with MinGW and UnixUtils) and I am writing a script which will compile, run, and diff the output of a .c file, and I am running into an issue where I attempt to execute a recently compiled C source file.

The C file is simple:

#include <stdio.h
int main() {
   printf("hello world.");
   return 0;
}

In my ruby file, I am running from a class' internal method.

def makeAndTest(filename)
   # code which removes any pre-existing .exe file not included

   compileResult = `gcc -O3 -o %s %s.c` % [filename, filename]
   if (!$?.success?)
        return
   end

   fileCall = `.\%s.exe` % filename
   puts fileCall
end

The file compiles just fine, but the actual file call that I make gives no output. As a sanity check, I've run simple commands such as "ls," which and the output is printed out as expected by a system() call. However, a command like "echo 23132" doesn't work as expected -- in fact, Ruby just doesn't print anything for either the echo call or my executable.

I've tried using system(), %x(), and backticks to try these calls, but I can never seem to get any output. This had led me to believe that Ruby must be doing something with my files stdout, right? I can run "echo 23132" or executing my file from cmd just fine, but ruby seems to fail at this.

If I check $?.success? after each call, it seems that both calls failed (yet the simple ls call succeeds).

Has anyone a clue what could be going on here? How does Ruby treat the stdout of files which it runs? In my mind, Ruby be pretty able to run a compiled .exe, so don't understand what could be going on here.

如果您的呼叫失败,请尝试通过将stderr的输出重定向到stdout来打印输出

output = `echo 12345 2>&1`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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