How C extensions for PHP call third-party libraries

Source: Internet
Author: User
Tags what interface
Because I am writing C's so not very understanding of this thing, first of all, it is the program under Linux, PHP version is 5.3.13, I write a simple addition to C PHP extension can be used, I think the problem is not the right link into the C library
There are two types of C libraries:
1. It is the Linux system itself, but need to be in the GCC compile executable file with-l to link, such as C line libraries-lpthread

2. is a third party, for example, I now have a C. So dynamic link library libdecode.so, provides a C API function long Setcode (long l_input);

The function I rewritten for PHP calls is written like this.
Php_function (Php_setcode) {Long arg_1 = 0;long re = 0;if (Zend_parse_parameter (Zend_num_args () tsrmls_cc, "L", &arg_1 ) = = FAILURE) {return;} Re = Setcode (arg_1);}

Config.m4 added a

Php_add_library_with_path (decode,/home/wzb/lib, Test_shared_libadd)
Php_add_include (/home/wzb/include)

Execute command
./congfigure--with-php-config=/usr/local/php/bin/php-config
Make


and copied the generated test.so and C's third-party library libdecode.so to/usr/local/php/lib/php/extension/no-debug-non-zts-20090626.
Now I have added the following statement in PHP.ini
Extension=test.so
Extension=libdecode.so

Finally, the Php_setcode function cannot be successfully called in PHP.

Ask the above steps where I need to add to implement a call to Php_setcode


Reply to discussion (solution)

Do you want to implement the C processing function or do you want PHP to call the custom function, if the latter is not made PHP extension?
If the former: http://blog.csdn.net/franzhong/article/details/7265429

Do you want to implement the C processing function or do you want PHP to call the custom function, if the latter is not made PHP extension?
If the former: http://blog.csdn.net/franzhong/article/details/7265429
This is the case, libdecode.so is not my library, is provided by the third party C library, I am ready to write a PHP extension to call this C library, compile no problem, is the last step, know how to put this third-party library to chain in. I did not libdecode.so the source code of this library. c File

This is not related to whether it is a php extension

You just need to know how to connect so in your C program.
This is not a problem with PHP, please go to the C + + layout solution, there is a lot of tall

I do not understand, online to find a tutorial toss and toss it, from the principle point of view, PHP has to include the header file, the implementation of the DL series dynamically loading the interface in the dynamic library, you need to tell PHP what interface is your dynamic library burst out of the leak.

To the parent directory of the custom extension you generated, it should be the Ext directory. See how the extension is actually implemented, such as MySQL

#清除
Make clean

#添加模块ncdocxml
./configure--with-php-config=/usr/nc-home/php5/bin/php-config--enable-ncdocxml

#编译模块ncdocxml
Make Ldflags=-lncdocxml

This is not related to whether it is a php extension

You just need to know how to connect so in your C program.
This is not a problem with PHP, please go to the C + + layout solution, there is a lot of tall
In C, a link to a dynamic library is a link to the last executable program (the equivalent of an. exe under Windows), and the problem is that the last executable is PHP instead of C, so the last step is link. So is not done by C

This post was last edited by xuzuning on 2012-06-07 14:14:13

You said that is static loading, you only so do not have the corresponding Lib file and header file. is not possible to implement static loading
What you need is dynamic loading, which of course is done in the C program. For example, the BC under window has the LoadLibrary function to load the specified DLL, of course, no header file is still very painful

In addition, we have been in VB, VFP has been dynamic loading operating system dynamic link library to complete the script language is impossible to complete the work

help you find this blog post for reference http://blog.csdn.net/offbye/article/details/6832536

You said that is static loading, you only so do not have the corresponding Lib file and header file. is not possible to implement static loading
What you need is dynamic loading, which of course is done in the C program. For example, the BC under window has the LoadLibrary function to load the specified DLL, of course, no header file is still very painful

In addition, we have been in VB, VFP has been dynamic loading operating system dynamic link library to complete the script language is impossible to complete the work
Of course, the header file is some, otherwise it will not compile successfully, about this dynamic loading problem, actually generate executable program when there is-ldl this compile option, Will eventually involve the need to link this libdl.so in PHP, this Linux system comes with a dynamic library, that is, I changed the above problem from libdecode.so to libdl.so
Thank you very much for your kind help.

Thank you, the problem has been solved, the problem should still be in config.m4 here. Here's how to fix it:
VI CONFIG.M4
Add in CONFIG.M4
Php_add_library_with_path (decode,/home/wzb/lib, Test_shared_libadd)
Php_add_include (/home/wzb/include)
Php_subst (Test_shared_libadd)

Then execute the command in turn
Phpize
./configure--enable-test--with-php-config=/usr/local/php/bin/php-config
Make

Finally, the PHP extension library test.so in modules can implement the call to Libdecode.so's C API function.

Hello, I would like to ask you that except this place to do the treatment is still in which place to do, I encountered a similar problem, there is no way to deal with AH

Thank you, the problem has been solved, the problem should still be in config.m4 here. Here's how to fix it:
VI CONFIG.M4
Add in CONFIG.M4
Php_add_library_with_path (decode,/home/wzb/lib, Test_shared_libadd)
Php_add_include (/home/wzb/include)
Php_subst (Test_shared_libadd)

Then execute the command in turn
Phpize
./configure--enable-test--with-php-config=/usr/local/php/bin/php-config
Make

Finally, the PHP extension library test.so in modules can implement the call to Libdecode.so's C API function.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.