search
Home类库下载PHP类库How to use pkg-config in php extension development

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version