I. 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 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
{
}