您的位置:首页 > 编程语言 > PHP开发

PHP5加载|安装外部C动态库

2013-12-16 15:49 399 查看
[1] cd php-5.3.9/ext

[2] ./ext_skel --extname=ncdocxml

[3] cd ncdocxml

[4] nano -w config.m4

############

删除 3 个 dnl

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])

或者删除 下边这3个 dnl

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])

############

[5] nano -w ncdocxml.c

############
/*
+----------------------------------------------------------------------+
| PHP Version 5                                                        |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group                                |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license,      |
| that is bundled with this package in the file LICENSE, and is        |
| available through the world-wide-web at the following url:           |
| http://www.php.net/license/3_01.txt                                  |
| If you did not receive a copy of the PHP license and are unable to   |
| obtain it through the world-wide-web, please send a note to          |
| license@php.net so we can mail you a copy immediately.               |
+----------------------------------------------------------------------+
| Author:                                                              |
+----------------------------------------------------------------------+
*/

/* $Id: header 321634 2012-01-01 13:15:04Z felipe $ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ncdocxml.h"
#include "/usr/local/libncdocxml.h"
/*extern int NcXmlDocGetClass(char * infile); */
/* If you declare any globals in php_ncdocxml.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(ncdocxml)
*/

/* True global resources - no need for thread safety here */
static int le_ncdocxml;

/* {{{ ncdocxml_functions[]
*
* Every user visible function must have an entry in ncdocxml_functions[].
*/
const zend_function_entry ncdocxml_functions[] = {
PHP_FE(confirm_ncdocxml_compiled,       NULL)           /* For testing, remove later. */
PHP_FE(ncdocxml, NULL)
PHP_FE_END      /* Must be the last line in ncdocxml_functions[] */
};
/* }}} */

/* {{{ ncdocxml_module_entry
*/
zend_module_entry ncdocxml_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"ncdocxml",
ncdocxml_functions,
PHP_MINIT(ncdocxml),
PHP_MSHUTDOWN(ncdocxml),
PHP_RINIT(ncdocxml),                    /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(ncdocxml),                /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(ncdocxml),
#if ZEND_MODULE_API_NO >= 20010901
"0.1", /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_NCDOCXML
ZEND_GET_MODULE(ncdocxml)
#endif

/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("ncdocxml.global_value",      "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_ncdocxml_globals, ncdocxml_globals)
STD_PHP_INI_ENTRY("ncdocxml.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_ncdocxml_globals, ncdocxml_globals)
PHP_INI_END()
*/
/* }}} */

/* {{{ php_ncdocxml_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_ncdocxml_init_globals(zend_ncdocxml_globals *ncdocxml_globals)
{
ncdocxml_globals->global_value = 0;
ncdocxml_globals->global_string = NULL;
}
*/
/* }}} */

/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ncdocxml)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ncdocxml)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(ncdocxml)
{
return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(ncdocxml)
{
return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ncdocxml)
{
php_info_print_table_start();
php_info_print_table_header(2, "ncdocxml support", "enabled");
php_info_print_table_end();

/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
}
/* }}} */

/* Remove the following function when you have succesfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */

/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_ncdocxml_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_ncdocxml_compiled)
{
char *arg = NULL;
int arg_len, len;
char *strg;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}

len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "ncdocxml", a
rg);
RETURN_STRINGL(strg, len, 0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/

PHP_FUNCTION(ncdocxml)
{
char *dbg = NULL;
int dbg_len, len;
int result;

if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dbg, &dbg_len) == FAILURE){
return;
}
result = NcXmlDocGetClass(dbg);
RETURN_LONG(result);
}
############

[6] nano -w php_ncdocxml.h

############
/*
+----------------------------------------------------------------------+
| PHP Version 5                                                        |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group                                |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license,      |
| that is bundled with this package in the file LICENSE, and is        |
| available through the world-wide-web at the following url:           |
| http://www.php.net/license/3_01.txt                                  |
| If you did not receive a copy of the PHP license and are unable to   |
| obtain it through the world-wide-web, please send a note to          |
| license@php.net so we can mail you a copy immediately.               |
+----------------------------------------------------------------------+
| Author:                                                              |
+----------------------------------------------------------------------+
*/

/* $Id: header 321634 2012-01-01 13:15:04Z felipe $ */

#ifndef PHP_NCDOCXML_H
#define PHP_NCDOCXML_H

extern zend_module_entry ncdocxml_module_entry;
#define phpext_ncdocxml_ptr &ncdocxml_module_entry

#ifdef PHP_WIN32
#       define PHP_NCDOCXML_API __declspec(dllexport)
#elif defined(__GNUC__) && __GNUC__ >= 4
#       define PHP_NCDOCXML_API __attribute__ ((visibility("default")))
#else
#       define PHP_NCDOCXML_API
#endif

#ifdef ZTS
#include "TSRM.h"
#endif

PHP_MINIT_FUNCTION(ncdocxml);
PHP_MSHUTDOWN_FUNCTION(ncdocxml);
PHP_RINIT_FUNCTION(ncdocxml);
PHP_RSHUTDOWN_FUNCTION(ncdocxml);
PHP_MINFO_FUNCTION(ncdocxml);

PHP_FUNCTION(confirm_ncdocxml_compiled);        /* For testing, remove later. */
PHP_FUNCTION(ncdocxml);

/*
Declare any global variables you may need between the BEGIN
and END macros here:

ZEND_BEGIN_MODULE_GLOBALS(ncdocxml)
long  global_value;
char *global_string;
ZEND_END_MODULE_GLOBALS(ncdocxml)
*/

/* In every utility function you add that needs to use variables
in php_ncdocxml_globals, call TSRMLS_FETCH(); after declaring other
variables used by that function, or better yet, pass in TSRMLS_CC
after the last function argument and declare your utility function
with TSRMLS_DC after the last declared argument.  Always refer to
the globals in your function as NCDOCXML_G(variable).  You are
encouraged to rename these macros something shorter, see
examples in any other php module directory.
*/

#ifdef ZTS
#define NCDOCXML_G(v) TSRMG(ncdocxml_globals_id, zend_ncdocxml_globals *, v)
#else
#define NCDOCXML_G(v) (ncdocxml_globals.v)
#endif

#endif  /* PHP_NCDOCXML_H */

/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
############

[7] /usr/local/php5/bin/phpize --with-apxs2=/usr/local/apache/bin/apxs

[8] apt-get install autoconf

[9] ./configure --with-ncdocxml --with-php-config=/usr/local/php5/bin/php-config

[10] nano -w Makefile

############

/*修改*/

EXTRA_LIBS=-lncdocxml

############

[11] make clean; make LDFLAGS=-lncdocxml

[12] ldd modules/ncdocxml.so

############

    linux-gate.so.1 =>  (0xb772e000)

        libncdocxml.so => /usr/local/lib/libncdocxml.so (0xb76a5000)

        libc.so.6 => /lib/i686/cmov/libc.so.6 (0xb755e000)

        libxml2.so.2 => /usr/local/lib/libxml2.so.2 (0xb7435000)

        libz.so.1 => /usr/local/lib/libz.so.1 (0xb741f000)

        libpthread.so.0 => /lib/i686/cmov/libpthread.so.0 (0xb7406000)

        libm.so.6 => /lib/i686/cmov/libm.so.6 (0xb73e0000)

        /lib/ld-linux.so.2 (0xb772f000)

        libdl.so.2 => /lib/i686/cmov/libdl.so.2 (0xb73dc000)

        libiconv.so.2 => /usr/local/lib/libiconv.so.2 (0xb72fd000)

############

[13] make install

############

Installing shared extensions:     /usr/local/php5/lib/php/extensions/no-debug-zts-20090626/

############

[14] nano -w /etc/php.ini

############

extension = /usr/local/php5/lib/php/extensions/no-debug-zts-20090626/ncdocxml.so

############

[15] /usr/local/apache/bin/apachectl restart

[16] nano -w loadmoudle.php

############

<?php
if (!extension_loaded("ncdocxml")) {
echo "无法加载 ncdocxml.so !".PHP_EOL;
} else {
echo "加载 ncdocxml.so !".PHP_EOL;
#调用加载的ncdocxml($filename)函数
$res = ncdocxml("/usr/local/test.txt");
print_r($res);
 }
?>
############

[17] /usr/local/php5/bin/php /usr/local/loadmoudle.php

############

加载 ncdocxml.so !

Array(

          [0]=>success

         )

############
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP ext extension C so