Connecting Tech Pros Worldwide Help | Site Map

C Code and PHP

Newbie
 
Join Date: Nov 2009
Posts: 12
#1: 2 Weeks Ago
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's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#2: 2 Weeks Ago

re: C Code and PHP


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)
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#3: 2 Weeks Ago

re: C Code and PHP


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.
dheerajjoshim's Avatar
Needs Regular Fix
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 256
#4: 2 Weeks Ago

re: C Code and PHP


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
Newbie
 
Join Date: Nov 2009
Posts: 12
#5: 2 Weeks Ago

re: C Code and PHP


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
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,642
#6: 2 Weeks Ago

re: C Code and PHP


Quote:

Originally Posted by urib View Post

Comment 3:
I do not understand the following:
to compile use the following commands

Expand|Select|Wrap|Line Numbers
  1. swig -php MyModule.i 
  2. gcc -c -o MyModule.c 
  3. gcc `php-config --includes` -fpic -c MyModule_wrap.c 
  4. 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.
dheerajjoshim's Avatar
Needs Regular Fix
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 256
#7: 2 Weeks Ago

re: C Code and PHP


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

Regards
Dheeraj Joshi
Newbie
 
Join Date: Nov 2009
Posts: 12
#8: 1 Week Ago

re: C Code and PHP


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

http://www.swig.org/index.php

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
http://wiki.php.net/internals/windows/stepbystepbuild

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.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#9: 1 Week Ago

re: C Code and PHP


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.
Newbie
 
Join Date: Nov 2009
Posts: 12
#10: 1 Week Ago

re: C Code and PHP


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)

http://www.swig.org/tutorial.html

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
dheerajjoshim's Avatar
Needs Regular Fix
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 256
#11: 1 Week Ago

re: C Code and PHP


Try this link...

SWIG AND PHP

Regards
Dheeraj Joshi
Reply