Apache
  Home arrow Apache arrow Page 2 - Executing A C++ Application Over The Inter...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
APACHE

Executing A C++ Application Over The Internet With PHP
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2001-12-27

    Table of Contents:
  • Executing A C++ Application Over The Internet With PHP
  • Creating the C application
  • Creating the PHP script
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 -thirdarg

    The 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] –secondArg


    That’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 /myfiles

    This 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.cpp

    If 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 sampleapp

    This 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 /three

    The following output will be shown on our screen:

    You passed 3 arguments.

    These arguments are:

    [1] one

    [2] –two

    [3] /three


    Now that our executable file ready to go, let’s create a PHP script that we will use to access it from our web browser.

    More Apache Articles
    More By Mitchell Harper


       · Thank you very much for writing this article. I was trying to learn how to do just...
     

    APACHE ARTICLES

    - Programmatically Manipulating Microsoft Exce...
    - Installing PHP under Windows
    - Compressing Web Content with mod_gzip and mo...
    - Compressing Web Output Using mod_deflate and...
    - Setting Up Apache 2.0.45 to Parse PHP Pages
    - Custom Error 404 Documents with PHP
    - Using Apache and PHP on Mac OS X
    - ASP: Active Sessions, Active Logins and Tota...
    - Working With Oracle on Windows: Part 1
    - The Quick-n-Dirty Guide to Setting Up Apache...
    - Installing Apache With SSL: The Complete Gui...
    - 7 Powerful .htaccess Customization Tips
    - Trap And Get Notified: A Practical Solution ...
    - One Way To Use Server Side Includes
    - Using ForceType For Nicer Page URLs







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek