Executing A C++ Application Over The Internet With PHP - Creating the C application
(Page 2 of 4 )
For this example, I will create a simple C++ application that will accept arguments via the command line. If we have an application named “sampleapp”, for example, then we can pass it three command-line arguments like this:
sampleapp -firstarg –secondarg -thirdargThe application will output the number of arguments sent to it, as well as the values of each of these arguments. We will use a PHP script to execute the compiled C++ application via the passthru function.
Using your favorite text editor, create a new text file named sampleapp.cpp. Enter the following code into sampleapp.cpp:
#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;
}Our C++ file contains the programs entry point, main. The main function accepts two arguments: argc (the number of arguments passed to the application from the command line), and argv (an array of character-pointers, containing the actual values of the command line arguments). These two arguments are automatically captured and passed-in by the C++ compiler.
cout << endl << "You passed " << argc-1 << " arguement"
<< (argc-1 == 1 ? "" : "s") << "." << endl;The main function starts by outputting the number of arguments that were passed to the application from the command line. The argv pointer-to-character array is indexed from zero onwards, and will always contain at least one value (the full path and name of our application), which is automatically added by the C++ compiler. We use the conditional operator, “?”, to deduce whether or not there was more than one argument passed in from the command line. If, for example, we passed two arguments from the command line, then our application would output the following line:
You passed 2 arguments.cout << (argc-1 == 1 ? "This" : "These")
<< " arguement" << (argc-1 == 1 ? "" : "s") << " "
<< (argc-1 == 1 ? "is" : "are") << ": " << endl << endl;Next, we use the same conditional operator to output another sentence. Remember, that the argv[] argument of the main function will contain one value even if we don’t pass any in from the command line. Again, if we passed two arguments from the command line, then our application would output the following line:
These arguments are:for(int i = 1; i < argc; i++)
cout << "[" << i << "] " << argv[i] << endl;Lastly, the main function outputs each of the command line arguments that are passed in. It uses a simple for(;;) loop, which keeps count of the number of arguments which have been outputted so far. If we passed in two arguments, “-firstArg”, and “-secondArg”, then the output of the for loop would look like this:
[1] –firstArg
[2] –secondArgThat’s our C++ application explained. It simply accepts arguments from the command line, and then displays these arguments on the screen, using the cout function.
You’ll now need to upload the sampleapp.cpp file to your Unix web server using some sort of FTP program. I recommend
CuteFTP. Make sure you upload your .cpp file into a non-publicly accessible folder.
To compile the sampleapp.cpp file, we need to connect to our web server using telnet. We will use the G++ compiler (which compiles standard-ANSI C++ files on Unix), which is readily available on all Unix systems. Just to make sure you have G++ installed, enter the following command at the Unix prompt:
which g++If you have G++ installed, then the Unix shell will display the full path to its executable. If not, it will tell you that the command couldn’t be found. If this is the case, then you can download G++
here.
Change into the directory where you uploaded the sampleapp.cpp file into. Use the “cd” command to do this. The “cd” (change directory) command is the same on Unix as it is on windows, except you use forward slashes to change into a directory, not black slashes:
cd /myfilesThis would put me into the “myfiles” directory of my root folder (assuming I have access to the “myfiles” directory and that it exists).
Next, we need to use the G++ compiler to compile our .cpp file into an object file containing machine code. Use the G++ command like this:
g++ -c sampleapp.cppIf you use the “ls –a” command to view the contents of the current directory, then you will see that the G++ compiler has created a new file, “sampleapp.o”. This file contains the machine code version of our .cpp file. We actually want to create an executable file from this object file. To do this, we use the following command:
g++ sampleapp.cpp –o sampleappThis creates a new executable file named “sampleapp”. Keep in mind that Unix executables don’t contain any file extension.
To test the “sampleapp” executable, enter the following command at the Unix shell:
sampleapp one -two /threeThe following output will be shown on our screen:
You passed 3 arguments.
These arguments are:
[1] one
[2] –two
[3] /threeNow that our executable file ready to go, let’s create a PHP script that we will use to access it from our web browser.
Next: Creating the PHP script >>
More Apache Articles
More By Mitchell Harper