_php tutorial for executing A/C + + application through the Web in PHP

Source: Internet
Author: User
Tags php programming
   First, Introduction


If you know anything about unix/linux, you should be aware that most of them have their own C and C + + compilers, namely GCC and g++. UNIX uses these compilers in many places, such as program installation and make. With some console commands, C + + and PHP, I'll show you how to generate a complete example of a C + + program that can be executed with a PHP program and get the corresponding output. I'll make a C + + program code and compile it and talk about what we'll do if we use PHP's function PassThru to execute this program. In a sense, this article provides us with a way to access a general program through a Web page.


In order to get a better understanding of this article, you should have a Unix/linux server running Apache and the latest version of PHP. You should also be familiar with C + +, UNIX console commands, and of course some PHP programming experience is also required.


  Second, write a C + + program


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


SampleApp? parameter two? parameter three


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 enter the following code in this file:




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": "Be") << ":" << Endl << Endl;


for (int i = 1; i < argc; i++)

cout << "[" << I << "]" << argv[i] << Endl;


return 0;


This C + + program contains the entry point of the program: Main (), the main () function with two parameters: ARGC (the number of command-line parameters passed in) and argv (a character-type pointer array containing the actual values of the arguments passed). 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 character-type pointer array is retrieved from 0, which contains at least one actual value (the path and name of the program), which is automatically appended by the C + + compiler. Conditional operator "?" is used to determine if there are more than one parameter in the command line. 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": "Be") << ":" << Endl << Endl;


Next, we also use the conditional operator to output another sentence. Keep in mind, though, that even if we don't pass any arguments 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 is:


for (int i = 1; i < argc; i++)

cout << "[" << I << "]" << argv[i] << Endl;


Finally, the main function outputs each of the arguments passed in the command line, which uses a simple for (;;) The loop statement, the function can be the number of parameters to the parameter value of one output. If we pass the program two parameters "first" and "second", the result of the For loop output 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 command line passed in parameters with the cout function displayed on the output screen.


Next, we will compile this. cpp file, and if you are under Windows platform, you need to telnet to the server you are using. Here, we compile this source file using the g++ compiler that is available on most Unix machines. However, in order to be sure that your machine has g++ installed, you can enter the following command: which g++. If g++ is already installed, the Unix shell will show the full path where g++ is located. If it is not installed, it will prompt you to say "command couldn ' t be found". You can download it here to g++.

Enter the following g++ command in the directory where the source files are located:

g++-C sampleapp.cpp.

With this command, we will. The CPP file is compiled into a target file that contains machine code. With LS-a command, you can see that a new file SAMPLEAPP.O appears in this directory, which is the result of the. cpp source file being compiled into machine code. But what we end up wanting is an executable file, because we're going to enter the following g++ command:


g++ sampleapp.cpp? o SampleApp


This allows us to obtain an executable file named SampleApp. Note, however, that the executable file under UNIX is not the same as windows, and it does not have any suffixes.


Below we can examine the results of the program execution, if the following command:


SampleApp One-two/three

We can see the following execution results:


You passed 3 arguments.

These arguments is:


[1] One




Now that the executable C + + program Genetic is complete, we will generate a PHP textbook program that can access the program through a Web browser.

Third, generate PHP script program


In order to be able to invoke our C + + program over the Internet, we need to generate a PHP scripting program. This PHP script will have a form form so that the user can enter parameters that can be passed to the program SampleApp. The code for the PHP script is too long to be listed here, so you can download it from the address below if you need it. (PHP code)


if (@ $submit)







http://www.bkjia.com/PHPjc/446993.html www.bkjia.com true http://www.bkjia.com/PHPjc/446993.html techarticle I. Introduction if you know something about unix/linux, you should know that most of them have their own C and C + + compilers, gcc and g++, respectively. UNIX is used in many places such as program installation and make ...

  • Related Article

    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.