Use PHP to execute the C/C ++ project_php tutorial through the Web

Source: Internet
Author: User
Use PHP to execute CC ++ programs on the Web. I. INTRODUCTION If you know something about UnixLinux, you should know that most of them come with C and C ++ compilers, GCC and G ++ respectively. Unix is used in many places such as program installation and Make. 1. Introduction



If you know something about Unix/Linux, you should know that most of them come with C and C ++ compilers, GCC and G ++ respectively. Unix uses these compilers in many places such as program installation and Make. Using some console commands, C ++ and PHP, I will introduce you to how to generate a complete C ++ program example, which can be executed using the PHP program, and get the corresponding output results. I will compile the C ++ program code and discuss how we will execute this program by using the PHP function passthru. In a sense, this article provides us with a way to access general programs through Web pages.

To better understand this article, you should have a unix/Linux server running apache and the latest php version. At the same time, you should also master the C ++ and unix console commands. of course, some PHP programming experience is also required.



2. compile a C ++ program



For example, we can write a simple C ++ program that can receive parameters through the command line and name it Sampleapp. then we can pass three different parameters to him as follows:

Sampleapp-parameter 1-parameter 2-parameter 3

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

Create a file named Sampleapp. cpp using the text editor you are used to, and enter the following code in the file:

# Include

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;

For (int I = 1; I <argc; I ++)
Cout <"[" <I <"]" <argv [I] <endl;

Return 0;
}



The entry point of the C ++ program: main (). The main () function includes two parameters: argc (number of parameters passed in through the command line) and argv (a struct pointer array containing the actual value of the passed parameter ). These two parameters can be automatically captured by the C ++ compiler.

Cout <endl <"You passed" <argc-1 <"arguement"
<(Argc-1 = 1? "": "S") <"." <endl ;;



This statement is used to obtain the number of parameters passed in from the command line. Argv: The struct pointer array is retrieved from 0. it contains at least one actual value (the path and name of the program ), this value is automatically appended by the C ++ compiler. Conditional operator "?" Is used to determine whether there are more than one input parameter in the command line. For example, if two parameters are input through the command line, the local 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;



Next, we also use conditional operators to output another sentence. Remember that, even if we do not pass any parameters from the program execution command line, the main function argv [] parameter also contains a value. Similarly, if we pass two parameters 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 parameter input from the command line one by one. it uses a simple for (;) loop statement, this function outputs parameter values one by one based on the number of parameters. If we pass two parameters "first" and second "to the program, the output of the for loop is as follows:

[1]-first
[2]-second



The above is a simple description of the C ++ program. its function is very simple, that is, the parameters passed in by the command line are displayed on the output screen using the cout function.

Next, we will compile this. cpp file. if you are on windows, telnet to the server you are using. Here, we use the G ++ compiler provided on most Unix machines to compile this source file. To ensure that your machine has installed G ++, run the following command: which g ++. If G ++ has been installed, Unix shell displays the full path of G ++. If it is not installed, it will prompt you to say "command couldn't be found". you can download it to G ++ here.

Enter the following G ++ command in the directory where the source file is located:



G ++-c sampleapp. cpp.



With this command, we compile the. cpp file into a target file containing the machine code. Through the ls-a command, you can find a new file sampleapp. o appears in the local directory. this is the result of the. cpp Source file being compiled into a machine code. But what we finally want is an executable file, because we need to enter the following G ++ command:

G ++ sampleapp. cpp-o sampleapp



In this way, an executable file named sampleapp is obtained. However, the executable file in Unix is different from that in Windows, and it does not have any suffix.

Next we can check the execution result of the program, if the following command:

Sampleapp one-two/three



We can see the following execution results:

You passed 3 arguments.
These arguments are:

[1] one
[2]-two
[3]/three

Now, the executable C ++ program is generated. next we will generate a PHP Tutorial program that can access this program through a web browser.



3. generate a PHP script



To call our C ++ program over the Internet, we need to generate a PHP script program. This PHP script contains a Form that allows you to input parameters that can be passed to the Sampleapp program. The PHP script code is not listed here because it is too long. you can download it at the address below if necessary. (Php code)

If (@ $ submit)
{

}
Else
{
}

First, check whether the variable $ submit has a value. The value of this variable $ submit is passed after the Form is submitted after the program. The default value is null. Symbol @ is used to ignore related error messages when the value of variable $ submit does not exist.

Because the variable $ submit is empty by default, the code in else {} is executed at the beginning, and a Form is displayed in the browser. The action attribute of Form is set to variable $ PHP_SELF, that is, this page is returned after the Form is submitted. At the same time, the Form contains a text input entry, which is a command line parameter that allows the user to input and pass to the C ++ program. Shows the Form:




Once we enter the execution command and submit the form, the variable $ submit (that is, the name of the Go button) will get a value, so that the PHP Tutorial will execute code between if.

If ($ args = "")
Echo "You didnt enter any arguments .";
Else
{
Echo "SampleApp Result ";
$ Command = "/htdocs/sampleapp". escapeshellcmd ($ args );

Passthru ($ command );
}



The variable $ args is automatically generated. its value is the value passed by text entries in the Form. If no information is entered, the program will simply tell the user that no value is entered.

If you enter any non-empty information, the program will pass the value of the text field, namely the variable $ args, to the C ++ program. The following code is the execution command of the C ++ program:

$ Command = "/htdocs/sampleapp". escapeshellcmd ($ args );



The eacapeshellcmd function is used as a security check tool to filter special characters such as ",", "", and. This prevents some users from attempting to enter certain characters to call internal system commands.

For example, if you enter "1-two/three" in the text field of the Form, the value of the variable $ command is:/htdocs/sampleapp 1-two/three.

You can find that we have defined the full path of the sampleapp program. In this example, the program file is located in the/htdocs directory. You can modify the directory where your program is located.

Passthru ($ command );



Finally, we use the PHP function passthru to execute the command contained in the variable $ command and output the original execution result to the browser. On my server, the returned HTML page is as follows:




W. before the end of this article, I would like to discuss several possible problems. First, when you execute sampleapp. php to teach the program, if you do not see any output information of the program, you may have enabled the security mode. In this case, the system will not allow PHP scripts to execute internal system programs. For details about how to disable the security mode, visit http://www.php.net/manual/en/features.safe-mode.php. Secondly, on some Unix systems, the PHP function passthru cannot pass the internal program output to the browser page.

If you know something about Unix/Linux, you should know that most of them come with C and C ++ compilers, GCC and G ++ respectively. Unix is used in many places such as program installation and Make...

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.