首先我们应该先写一个C++程序,代码如下 :
//我首先编辑的这个C++的文件名为hello.cpp
#include <iostream>
#include <stdio.h>
int main(int argc, char* argv[])
{
cout<<endl<<"You passed "<<argc-1<<" arguement"<<(argc-1==1?"":"s")<<"."<<endl;
cout<<(argc-1==1?"This":"These")<<"arguement"<<(argc-1==1?"":"s")<<" "<<(argc-1==1?"is":"are")<<":"<<endl<<endl;
for (int i = 1; i<argc;i++)
cout<<argv[i]<<endl;
return 0;
}
然后我们编辑c++, 命令为:g++ -o hello hello.cpp
编辑好后生成 一个hello的文件
下面为PHP调用c++的代码:
//php的文件名:test.php
<?php
$args="Hello world";
if($args == "")
echo "<h1>You didn't enter any arguments.</h1>";
else
{
echo "<h1>SampleApp Result</h1>";
$command = "/var/www/test/hello " . escapeshellcmd($args);
///var/www/test/hello 是c++编译生成的hello 的路径
passthru($command);
}
说明,$args可以赋任意值,
最后,我们在浏览器上输入相对应的地址 http://localhost/test/test.php
就可以得到结果,结果为:
如果遇到编辑C++程序有错误,那就是编码问题,你们可以重新删除空格什么的,
要把它们全部转换成英文字符
-
顶
604



被折叠的 条评论
为什么被折叠?



