Php c extension how to call a third-party library because I am writing C, so I am not very familiar with this matter, the first note is that the program in linux, php version is 5.3.13, I can use a simple addition operation php extension written in C. I think the problem is that there is no correct link to the C library.
The C library has two types:
1. it is provided by the linux system, but it must be linked with-l when compiling executable files in gcc, such as the C thread library-lpthread.
2. it is a third party. for example, I now have a C. so dynamic link library libdecode. so, which provides a C API function long SetCode (long l_input );
I wrote the function for php to call.
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);}
Added in config. m4
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 the generated test. so and C third-party library libdecode. so are copied to/usr/local/php/lib/php/extension/no-debug-non-zts-20090626
Now I have added the following statements in php. ini:
Extension = test. so
Extension = libdecode. so
Finally, the php_setcode function cannot be successfully called in php.
Ask where I need to add the above steps to implement the call to php_setcode
Reply to discussion (solution)
If you want to implement the c processing function or want php to call a user-defined function, why not make the latter a php extension?
If the former: http://blog.csdn.net/franzhong/article/details/7265429
If you want to implement the c processing function or want php to call a user-defined function, why not make the latter a php extension?
If the former: http://blog.csdn.net/franzhong/article/details/7265429
This is the case, libdecode. so is not the library I wrote, it is the C library provided by a third party, I am preparing to write a PHP extension to call this C library, there is no problem in compilation, it is the last step, know how to link this third-party library. I do not have the source code. c file for the libdecode. so Library.
This is irrelevant to whether php extension is used.
You only need to know how to connect so in the c program.
This is not a php issue. please go to the C/C ++ layout to solve the problem. There are many experts there.
I don't know. I have to find a tutorial on the internet. from the principle point of view, php should include the header file, and the dl series should dynamically load the interfaces in the dynamic library during execution, you need to tell php which interfaces are exposed by your dynamic library.
To the parent directory of your custom extension, it should be the ext directory. See how the extension is actually implemented, such as mysql
# Clear
Make clean
# Add a module ncdocxml
./Configure -- with-php-config =/usr/nc-home/php5/bin/php-config -- enable-ncdocxml
# Compiling module ncdocxml
Make LDFLAGS =-lncdocxml
This is irrelevant to whether php extension is used.
You only need to know how to connect so in the c program.
This is not a php issue. please go to the C/C ++ layout to solve the problem. There are many experts there.
The link to the dynamic library in the C is equivalent to the .exe link in windows when the executable program is generated. the problem at this time is that the final executable program is PHP rather than C, so the last link. so is not completed by C.
This post was last edited by xuzuning at 14:14:13 on
You are talking about static loading. you only have so and there is no corresponding lib file or header file. Static loading is not possible.
What you need is dynamic loading, which is of course completed in the c program. For example, the BC in the window has the LoadLibrary function to load the specified dll. of course, it is still very painful to have no header file.
In addition, we have been dynamically loading the dynamic link library of the operating system in both vb and vfp to complete the work that the script language cannot do.
Help you find this blog for reference http://blog.csdn.net/offbye/article/details/6832536
You are talking about static loading. you only have so and there is no corresponding lib file or header file. Static loading is not possible.
What you need is dynamic loading, which is of course completed in the c program. For example, the BC in the window has the LoadLibrary function to load the specified dll. of course, it is still very painful to have no header file.
In addition, we have been dynamically loading the dynamic link library of the operating system in both vb and vfp to complete the work that the script language cannot do.
Of course, there are some header files, otherwise they won't be compiled successfully. for this dynamic loading problem, The-ldl compilation option is used when an executable program is generated, it will eventually involve the need to link this libdl in php. so the dynamic library that comes with the linux system is to extract the above problems from libdecode. so is changed to libdl. so
Thank you very much for your enthusiastic help.
Thank you for solving the problem. The problem should still be in config. m4. The solution is as follows:
Vi config. m4
Add it 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 commands in sequence
Phpize
./Configure -- enable-test -- with-php-config =/usr/local/php/bin/php-config
Make
Finally, the PHP Extension Library test. so obtained in modules can call the API functions of libdecode. so C.
Hello, could you tell me where else you have handled this issue? I have encountered a similar problem and there is no way to deal with it now?
Thank you for solving the problem. The problem should still be in config. m4. The solution is as follows:
Vi config. m4
Add it 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 commands in sequence
Phpize
./Configure -- enable-test -- with-php-config =/usr/local/php/bin/php-config
Make
Finally, the PHP Extension Library test. so obtained in modules can call the API functions of libdecode. so C.