Home  >  Article  >  类库下载  >  How to use pkg-config in php extension development

How to use pkg-config in php extension development

高洛峰
高洛峰Original
2016-10-14 10:49:511248browse

First of all, let’s learn about what pkg-config can do. Here is what Wikipedia says about pkg-config:
pkg-config is computer software that provides a unified interface for querying installed libraries when compiling software from source code. pkg-config was originally designed for Linux, but now there are versions available on various versions of BSD, Windows, Mac OS X, and Solaris.
Simply put, pkg-config mainly provides the following functions:

1. Check the version number of the library. If the version of the required library does not meet the requirements, it will print out an error message to avoid linking the wrong version of the library file.
2. Obtain compilation preprocessing parameters, such as macro definitions and the location of header files.
3. Obtain link parameters, such as the location of libraries and other dependent libraries, file names and other connection parameters.
4. Automatically add settings for other libraries it depends on.

Recently developed a php extension for image cropping using opencv. Opencv needs to load a lot of so libraries. It is too troublesome to manually add them to config.m4, and it does not take advantage of post-maintenance. Fortunately, opencv provides the opencv.pc file, so you can use pkg-config to automatically obtain the compiled parameters.

The following is the code I wrote in config.m4 to automatically load opencv related so libraries and header files.

dnl # --with-tclip -> check with-path
  SEARCH_PATH="/usr/lib/pkgconfig" # 定义pkgconfig文件,即扩展名pc文件存放路径
  SEARCH_FOR="opencv.pc"  # 要寻找的文件
  if test -r $PHP_TCLIP/$SEARCH_FOR; then # 
     TCLIP_DIR=$PHP_TCLIP
  else # search default path list
     AC_MSG_CHECKING([for tclip files in default path])
     for i in $SEARCH_PATH ; do
       if test -r $i/$SEARCH_FOR; then
         TCLIP_DIR=$i
         AC_MSG_RESULT(found in $i)
       fi
     done
  fi
  dnl
  if test -z "$TCLIP_DIR"; then
     AC_MSG_RESULT([not found])
     AC_MSG_ERROR([Please reinstall the tclip distribution])
  fi
  
  OPENCV_FLAGS="`pkg-config opencv --libs --cflags opencv`"
  for i in $OPENCV_FLAGS;do
        if test ${i:0:2} = "-I" ;then
                PHP_ADD_INCLUDE(${i:2})
        elif test ${i:${#i}-3} = ".so" ;then
                dir_name=`dirname $i`
                file_name=${i/$dir_name/}
                file_name=${file_name/\/lib/}
                file_name=${file_name/.so/}
        PHP_ADD_LIBRARY_WITH_PATH($file_name,$dir_name,TCLIP_SHARED_LIBADD)
        fi
  done

Note:
Please do not use braces in the conditional part of if, otherwise there will be syntax errors in the makefile generated by m4.

Reference document
http://www.chenjunlu.com/2011/03/understanding-pkg-config-tool/ "Understanding pkg-config tool"
http://www.php.net/manual/zh/internals2 .buildsys.configunix.php "Interacting with UNIX build systems: config.m4"


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php-fpm startup errorNext article:php-fpm startup error

Related articles

See more