|
View:
New views
5 Messages
—
Rating Filter:
Alert me
|
|
|
Class not found when using c++ classes with PHPHi,
I'm a SWIG newbie and am currently experimenting with exporting the c++ classes interface into PHP. I'm sorry if this problem sounds trivial or has been discussed before but extensive googling gave me little information with regards to c++ and PHP compatibility issues. Now, I have created the following files, example1.cpp using namespace std; class Example1 { public: Example1(); ~Example1(); void get_val(int a1, int b1, int c1); int sum(void); int a; int b; int c; }; void Example1::get_val(int a1, int b1, int c1) { a = a1; b = b1; c = c1; } int Example1::sum() { return(a+b+c); } example1.h class Example1 { public: Example1(); ~Example1(); void get_val(int a1, int b1, int c1); int sum(void); int a; int b; int c; }; example1.i /* example1.i */ %module example1 %{ //using namespace std; #include "example1.h" %} class Example1 { public: Example1(); ~Example1(); void get_val(int a1, int b1, int c1); int sum(void); int a; int b; int c; }; After this, I run the following commands and everything compiles without any problems, swig -c++ -php5 example1.i g++ -c example1.cpp example1_wrap.cpp `php-config --includes` -fPIC c++ -shared example1.o example1_wrap.o -o example1.so I then place the generated example1.so file in the extension directory of php using mv example.so `php-config --extension-dir` And also place it in the apache www root along with the php test script. The php test script is defined below, <?php echo dl('example1.so'); $v = new Example1(); $v->get_val(1,2,3); echo $v->sum(); ?> This throws an exception on the 3rd line i.e. where i try to create an object of the class. Fatal error: Class 'Example1' not found in /var/www/test.php on line 3 php.ini file has dynamic loading enabled because I ran everything with a simple C file and it worked perfectly. I get the same error as above when I try using structures as well. Any help in this matter would be really appreciated. Thanks a lot. Yours sincerely -- Shishir I am running Ubuntu 8 with all the latest packages for everything involved. Also the ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Class not found when using c++ classes with PHPAlso, I have included the php file generated in the php script right
after the dl() call , include('example1.php'); But it still does not seem to work. Thanks On Fri, May 30, 2008 at 12:06 PM, Shishir Malani <ssmalani@...> wrote: > Hi, > > I'm a SWIG newbie and am currently experimenting with exporting the > c++ classes interface into PHP. I'm sorry if this problem sounds > trivial or has been discussed before but extensive googling gave me > little information with regards to c++ and PHP compatibility issues. > > Now, I have created the following files, > > example1.cpp > > using namespace std; > > class Example1 { > public: > Example1(); > ~Example1(); > void get_val(int a1, int b1, int c1); > int sum(void); > int a; > int b; > int c; > > }; > > void Example1::get_val(int a1, int b1, int c1) > { > a = a1; > b = b1; > c = c1; > } > > int Example1::sum() > { > return(a+b+c); > } > > example1.h > > class Example1 { > public: > Example1(); > ~Example1(); > void get_val(int a1, int b1, int c1); > int sum(void); > int a; > int b; > int c; > > }; > > example1.i > > /* example1.i */ > %module example1 > > %{ > //using namespace std; > #include "example1.h" > %} > > class Example1 { > public: > Example1(); > ~Example1(); > void get_val(int a1, int b1, int c1); > int sum(void); > int a; > int b; > int c; > > }; > > After this, I run the following commands and everything compiles > without any problems, > > swig -c++ -php5 example1.i > g++ -c example1.cpp example1_wrap.cpp `php-config --includes` -fPIC > c++ -shared example1.o example1_wrap.o -o example1.so > > I then place the generated example1.so file in the extension directory > of php using > > mv example.so `php-config --extension-dir` > > And also place it in the apache www root along with the php test script. > > The php test script is defined below, > > <?php > > echo dl('example1.so'); > > $v = new Example1(); > > $v->get_val(1,2,3); > > echo $v->sum(); > > ?> > > This throws an exception on the 3rd line i.e. where i try to create an > object of the class. > > Fatal error: Class 'Example1' not found in /var/www/test.php on line 3 > php.ini file has dynamic loading enabled because I ran everything with > a simple C file and it worked perfectly. I get the same error as above > when I try using structures as well. > > Any help in this matter would be really appreciated. > > Thanks a lot. > > Yours sincerely > -- > Shishir > > > > I am running Ubuntu 8 with all the latest packages for everything > involved. Also the > ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Class not found when using c++ classes with PHPOn 2008-05-30, Shishir Malani <ssmalani@...> wrote:
> Now, I have created the following files, If sending example files, it's better to send them as mime attachments so they can easily be saved and tried. I'm afraid I haven't tested your example because I don't have the patience to cut and paste the text from the message to recreate the various files, and have to fix up any word wrap damage and other mailer mangling. It would also be helpful to tell us what versions of PHP and SWIG you are using. I'm guessing some version of PHP5 since you use "-php5" below. > After this, I run the following commands and everything compiles > without any problems, > > swig -c++ -php5 example1.i > g++ -c example1.cpp example1_wrap.cpp `php-config --includes` -fPIC > c++ -shared example1.o example1_wrap.o -o example1.so I don't think this is causing you problems, but it would be better to compile and link with either g++ *OR* c++ (i.e. use the same one in both cases). > I then place the generated example1.so file in the extension directory > of php using > > mv example.so `php-config --extension-dir` Do you mean "example1.so" here? That's what you compiled above and try to load below. > And also place it in the apache www root along with the php test script. > > The php test script is defined below, > ><?php > > echo dl('example1.so'); > > $v = new Example1(); > > $v->get_val(1,2,3); > > echo $v->sum(); > > ?> (As you seem to have realised from your follow-up email) you need the PHP wrapper to be included: include 'example1.php'; This will dl the shared object for you if it isn't already loaded, so you don't need to do that yourself. > Fatal error: Class 'Example1' not found in /var/www/test.php on line 3 > php.ini file has dynamic loading enabled because I ran everything with > a simple C file and it worked perfectly. I get the same error as above > when I try using structures as well. Sounds like php failed to load example1.so. Try this from CLI php: php5 -r 'include "example1.php"; $v = new Example1();' If that works, then it's some difference in the apache environment. Look in apache's error_log for clues. A random guess - perhaps your umask is very restrictive, so example1.so isn't readable by the web server user. Cheers, Olly ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Class not found when using c++ classes with PHPHI Olly,
Thanks so much for replying. PHP Version : 5.2.4-2ubuntu5.1 SWIG Version : 1.3.33 Apache Version: Apache/2.2.8 (Ubuntu) I have attached the =4 necessary files as an attachment to this email, They are, example.cpp example.h example.i test.php The commands I run on these are,
swig -c++ -php5 example.i g++ -c example.cpp example_wrap.cpp `php-config --includes` -fPIC g++ -shared example.o example_wrap.o -o example.so mv example.so `php-config --extension-dir` A few points, When i use the include('example.php') directive in the test.php file, it asks me to download the php file instead of executing the script. Hence I am currently using dl(example.so) Now running in the browser still gives me the same error, viz. Fatal error: Class 'Example' not found in /var/www/test.php on line 7 And running your above mentioned script on CLI php5 -r 'include "example.php";$v = new Example();' gives me, PHP Warning: Module 'gd' already loaded in Unknown on line 0 Warning: dl(): Dynamically loaded extensions aren't enabled in /usr/share/php/example.php on line 23 Fatal error: Call to undefined function new_Example() in /usr/share/php/example.php on line 50 I have made sure gd is installed and the gd.so file is present in the extension directory of PHP. Also the php.ini files of both apache2 and CLI have the following entry, extension = gd.so I am pretty sure I am making some trivial mistake someplace.Any guidance would be appreciated. Thanks a lot, SIncerely -- Shishir On Fri, Jun 6, 2008 at 7:48 AM, Olly Betts <olly@...> wrote: > On 2008-05-30, Shishir Malani <ssmalani@...> wrote: >> Now, I have created the following files, > > If sending example files, it's better to send them as mime attachments > so they can easily be saved and tried. I'm afraid I haven't tested your > example because I don't have the patience to cut and paste the text from > the message to recreate the various files, and have to fix up any word > wrap damage and other mailer mangling. > > It would also be helpful to tell us what versions of PHP and SWIG you > are using. I'm guessing some version of PHP5 since you use "-php5" > below. > >> After this, I run the following commands and everything compiles >> without any problems, >> >> swig -c++ -php5 example1.i >> g++ -c example1.cpp example1_wrap.cpp `php-config --includes` -fPIC >> c++ -shared example1.o example1_wrap.o -o example1.so > > I don't think this is causing you problems, but it would be better to > compile and link with either g++ *OR* c++ (i.e. use the same one in > both cases). > >> I then place the generated example1.so file in the extension directory >> of php using >> >> mv example.so `php-config --extension-dir` > > Do you mean "example1.so" here? That's what you compiled above and try > to load below. > >> And also place it in the apache www root along with the php test script. >> >> The php test script is defined below, >> >><?php >> >> echo dl('example1.so'); >> >> $v = new Example1(); >> >> $v->get_val(1,2,3); >> >> echo $v->sum(); >> >> ?> > > (As you seem to have realised from your follow-up email) you need the > > include 'example1.php'; > > This will dl the shared object for you if it isn't already loaded, so > you don't need to do that yourself. > >> Fatal error: Class 'Example1' not found in /var/www/test.php on line 3 >> php.ini file has dynamic loading enabled because I ran everything with >> a simple C file and it worked perfectly. I get the same error as above >> when I try using structures as well. > > Sounds like php failed to load example1.so. > > Try this from CLI php: > > php5 -r 'include "example1.php"; $v = new Example1();' > > If that works, then it's some difference in the apache environment. > > Look in apache's error_log for clues. A random guess - perhaps your > umask is very restrictive, so example1.so isn't readable by the web > server user. > > Cheers, > Olly > > > ------------------------------------------------------------------------- > Check out the new SourceForge.net Marketplace. > It's the best place to buy or sell services for > just about anything Open Source. > http://sourceforge.net/services/buy/index.php > _______________________________________________ > Swig-user mailing list > Swig-user@... > https://lists.sourceforge.net/lists/listinfo/swig-user > [example.cpp] class Example { public: Example(); ~Example(); void get_val(int a1, int b1, int c1); int sum(void); int a; int b; int c; }; void Example::get_val(int a1, int b1, int c1) { a = a1; b = b1; c = c1; } int Example::sum() { return(a+b+c); } [example.h] class Example { public: Example(); ~Example(); void get_val(int a1, int b1, int c1); int sum(void); int a; int b; int c; }; ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
|
|
Re: Class not found when using c++ classes with PHPFOllowup Email :
The php.ini file of CLI had the enable_dl switch Off and i basically switched it On. SO now running the command line script gives me the following errors, PHP Warning: Module 'gd' already loaded in Unknown on line 0 php5: symbol lookup error: /usr/lib/php5/20060613+lfs/example.so: undefined symbol: _ZN7ExampleC1Ev I am still perplexed as to why, calling the test.php script above causes the test.php script to get downloaded as a blank file, whereas if i replace the include "example.php" with just any other php file defining say some variable, it reads it and works perfectly. On Thu, Jun 12, 2008 at 11:48 AM, Shishir Malani <ssmalani@...> wrote: HI Olly, ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ Swig-user mailing list Swig-user@... https://lists.sourceforge.net/lists/listinfo/swig-user |
| Free Forum Powered by Nabble | Forum Help |