exec()不适用于C / C ++可执行文件 [英] exec() doesn't work on C/C++ executables

查看:76
本文介绍了exec()不适用于C / C ++可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我是PHP的新手。我正在尝试运行一个简单的php

脚本(在带有apache web服务器的Red Hat linux机器上)

通过Windows Web浏览器(IE)。 />
我在我的php脚本中使用了exec()函数。

代码如下(带行号)。


01<?php

02 $ output = exec(" pwd");

03 echo"< br>工作目录:$ output" ;;


04 // $ prgfile =" touch out / testfile";

05 // $ prgfile =" ls";

06 //$prgfile="./hello.pl" ;;

07 $ prgfile =" ./ hello" ;;


08 exec($ prgfile,$ output,$ rc);

09 echo"< br>";

10 print_r($ output);

11 echo"< br>返回值:" ;;

12 print_r($ rc);


13 // $ output = system($ prgfile,$ rc);

14 // var_dump($ output);

15 // var_dump($ rc);

16?>

当我使用任何shell命令(如第2,4,5行)

或perl脚本(行)时,exec()和system()函数可以工作6)。 hello.pl打印一个

" hello world"到浏览器。 " PWD"打印

当前正在工作的目录。


但是当我使用C / C ++二进制文件(第7行)时,你好

可执行文件应该打印hello world浏览器,

脚本给我的返回代码为126,

$输出为空。但是当我运行脚本从

linux shell提示我可以看到输出。


apache错误日志说

sh :./ hello:无法执行二进制文件。


我想,解决方案应该很简单,但我不知道b $ b知道我在做什么在这里失踪。我改变了权限

的你好到777,给出绝对路径但仍然没有输出。


=========================== ======================

我用来创建hello的C / C ++代码
(我用gcc / g ++编译它们并且都运行)。


#include< iostream>

使用命名空间std;

int main()

{

cout<< Hello World << endl;

返回0;

}


#include< stdio.h>

int main()

{

printf(Hello World \ n);

返回0;

}


=================================== ==============

感谢任何帮助。


谢谢,

Hemanth

Hello there,

I''m new to PHP. I''m trying to run a simple php
script (on a Red hat linux machine with apache web server )
through a windows web browser (IE).
I''m using an exec() function in my php script.
The code is given below (with line numbers).

01 <?php
02 $output = exec("pwd");
03 echo "<br>Working dir: $output";

04 //$prgfile="touch out/testfile";
05 //$prgfile="ls";
06 //$prgfile="./hello.pl";
07 $prgfile="./hello";

08 exec($prgfile, $output, $rc);
09 echo "<br>";
10 print_r($output);
11 echo "<br> Return Value: ";
12 print_r($rc);

13 //$output = system($prgfile, $rc);
14 //var_dump($output);
15 //var_dump($rc);
16 ?>
The exec() and system() functions works when I
use any shell command (as in lines 2, 4, 5)
or a perl script (line 6). hello.pl prints a
"hello world" to the browser. "pwd" prints
the current working dir.

But when I use a C/C++ binary (line 7), "hello"
executable should print "hello world" to browser,
the script gives me a return code of 126 and the
$output is empty. But when I run the script from
linux shell prompt I can see the output.

The apache error log says
sh: ./hello: cannot execute binary file.

I guess, the fix should be simple but I don''t
know what I''m missing here. I changed the permissions
of "hello" to 777, gave absolute path but still no output.

=================================================

The C/C++ code I used to create "hello"
(I compiled them with gcc/g++ and both runs).

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
return 0;
}

#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}

=================================================
Any help is appreciated.

Thanks,
Hemanth

推荐答案

output = exec(" pwd");

03 echo"< br> ;工作目录:
output = exec("pwd");
03 echo "<br>Working dir:


输出" ;;


04 //
output";

04 //


prgfile ="触摸/ testfile" ;;

05 //
prgfile="touch out/testfile";
05 //


这篇关于exec()不适用于C / C ++可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆