C Code and PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • urib
    New Member
    • Nov 2009
    • 14

    C Code and PHP

    I already asked in the javascript forum
    step by step instructions how to write a code that is going to
    use some simple C function and show it in the server.

    see the following link

    http://bytes.com/topic/javascript/answers/876590-how-do-i-write-code-javascript-use-function-c

    I have visual C++ 2008 and microsoft visual studio 2008
    I hope that you can help me to give me step by step instructions how to write code that simply show how to write code that does the same as my javascript code does that mean showing the result of plus1(5) in the server by calling the simple C function that I have.

    Thanks in advance,
    Uri
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you may want to try SWIG.

    you can also make a PECL extension.

    and if the C function is simple, it’s better to write it in PHP (so you don’t have the interface overhead)

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      SWIG article (not published).

      Or if you want to write the PHP extension yourself (it's not so hard), see this link on extension development for PHP on Windows.

      Mark.

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Yeah... You can use SWIG... Its easy to use.

        As MARK told you can create your own PHP extensions also... Both will serve your purpose.

        Regards
        Dheeraj Joshi

        Comment

        • urib
          New Member
          • Nov 2009
          • 14

          #5
          I do not understand how I write the C code in the example
          of
          SWIG -- Easy way of communication between C and PHP

          Here are my comments:
          comment 1:
          I know to write a new code in C in visual C++ 2008 by the following steps:

          1)file->new project
          when I select empty project and select a name for the project(I chose example

          2)add new files to the project
          project->add new item when I can choose between .cpp file and .h file(when practically .cpp include also .c if I save the file as .c)

          Comment 2:
          I am not sure what to do about adding .i files to project.

          When I add a new file to the project I need to choose between code and UI and property sheet.
          My experience is only with choosing code and in this case I simply choose between source file and header file).

          Comment 3:
          I do not understand the following:
          to compile use the following commands
          swig -php MyModule.i
          gcc -c -o MyModule.c
          gcc `php-config --includes` -fpic -c MyModule_wrap.c
          gcc -shared MyModule.o MyModule_wrap.o -o MyModule.


          I will be happy if you give me step by step instructions how to do it.
          My experience is that I simply compile by build solution command from visual C++ 2008 to compile projects in C.

          Uri

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by urib
            Comment 3:
            I do not understand the following:
            to compile use the following commands
            Code:
            swig -php MyModule.i 
            gcc -c -o MyModule.c 
            gcc `php-config --includes` -fpic -c MyModule_wrap.c 
            gcc -shared MyModule.o MyModule_wrap.o -o MyModule
            these are CLI* commands. swig is the command to start the SWIG programme, gcc (GNU Compiler Collection) is a widespread C-compiler.

            * Command Line Interface, in *NIX systems often called Terminal or Shell, in windows it would be the Command Prompt.

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              SWIG has support for Windows too... Download the latest stable SWIG executable and start coding...

              Regards
              Dheeraj Joshi

              Comment

              • urib
                New Member
                • Nov 2009
                • 14

                #8
                I download SWIG for windows from the download in the following page

                Download SWIG for free. A code generator for connecting C/C++ with other programming languages. SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG is used with different types of target languages including common scripting languages such as Javascript, Perl, PHP, Python, Tcl and Ruby.


                I guess that swigwin-1.3.40 is the right file.

                Unfortunately I have no idea how to continue when my target is only to show the result of a simple function in C on the server.

                I see no step by step instructions and the instructions that I got in a different page also did not help.

                The different page is


                I could do steps 1,2 in the Quick 'n' dirty part but did not understand how to do step 3 and I am also not sure if I did steps 7-10 correctly(I chose with the mouse start->programs->microsoft windows SDK v6.1->cmd shell and got some window with the name microsoft windows server 2008 DEBUG build enviroment
                I printed the words in step 7 and clicked enter and repeated it with step 8,9,10.

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Please make the effort and read the tutorials available on the SWIG website. For information on how to integrate SWIG with VS, see this article.

                  Mark.

                  Comment

                  • urib
                    New Member
                    • Nov 2009
                    • 14

                    #10
                    1)f you mean to the tutorial in the link in this post then it does not talk about PHP but about other langauges that I do not know(I also do not know PHP)



                    I see
                    Building a Tcl module
                    Building a Python module
                    Building a Perl module
                    Building a Java module
                    Building a C# module

                    I am simply not sure which link I need to read.

                    2)The article that you give talk about microsoft visual C++2005 and older versions of C.

                    Did not try it but
                    I am not sure if the part about Visual C++2005 is also good for
                    Visual C++2008

                    Comment

                    • Dheeraj Joshi
                      Recognized Expert Top Contributor
                      • Jul 2009
                      • 1129

                      #11
                      Try this link...

                      SWIG AND PHP

                      Regards
                      Dheeraj Joshi

                      Comment

                      Working...