How to use PHP to implement a C + + program through the Web

Source: Internet
Author: User
Tags execution php and php programming php script linux

First, Introduction

If you have any knowledge of unix/linux, you should know that most of them bring in C and C + + compilers, gcc and g++ respectively. UNIX uses these compilers in many places, such as program installation and make. Take advantage of some console commands, C + + and PHP. I'll show you how to generate a complete example of C + + program that he can execute in PHP and get the corresponding output.

I will be Mr. C + + program code and compile it and then discuss what we will do if we use PHP's function PassThru to execute this program. In a sense, this article gives us a way to access general programs through Web pages.

To get a better understanding of this article, you should have a Unix/linux server running Apache and the latest version of PHP. Also should master C + +, UNIX console commands, of course, some PHP programming experience is also necessary.

Second, write a C + + program

For example, we can write a C + + simple program that can also receive parameters through the command line, named SampleApp. Then we can pass three different parameters to him in the following way:

Sampleapp ?参数一 ?参数二 ?参数三

The function of this program is to output the number of parameters passed to him and the value of each parameter, then we can use the PHP script program to execute the compiled C + + program.

Using your custom text editor, create a new file named Sampleapp.cpp, and then enter the following code in this file:

#include <iostream.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 << "[" << i << "] "
<< argv[i] << endl;
  return 0;
  }

The entry point of the program that this C + + program contains: Main (), and the main () function takes two parameters: ARGC (the number of incoming arguments in the command line) and argv (a character pointer array that contains the actual value of the passed parameter). This two parameter can be automatically captured by the C + + compiler.

cout << endl << "You passed " << argc-1
<< " arguement"
  << (argc-1 == 1 ? "" : "s")
<< "." << endl;
;

This means getting the number of arguments passed in from the execution command line. argv this array of character pointers is retrieved from 0 and contains at least one actual value (that is, the path and name of this program), which is automatically appended by the C + + compiler. Conditional operator "?" is used to determine whether the command line passes in more than one parameter. For example, if the command line passes through two parameters, our program will output the following information:

You passed 2 arguments.
cout << (argc-1 == 1 ?
"This" : "These")
  << " arguement"
<< (argc-1 == 1 ? "" : "s")
<< " "
  << (argc-1 == 1 ? "is" : "are")
<< ": " << endl << endl;

Next, we also use the conditional operator to output a different sentence. Remember, however, that even if we don't pass in any arguments from the program execution command line, the main function argv[] parameter also contains a value. Similarly, if we pass two arguments to the program from the command line, the program will output the following information:

These arguments are:
for(int i = 1;
i < argc; i++)
  cout << "[" << i << "] "
<< argv[i] << endl;

Finally, the main function outputs each of the arguments passed by the command line, and it uses a simple for (;;) Loop statement, which can output a parameter by one value based on the number of parameters. If we pass the program two parameters "first" and "second", the result of the For loop output is as follows:

[1] ?first
[2] ?second

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.