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

How to use pkg-config in php extension development

Oct 14, 2016 am 10:49 AM

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

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

Copy after login

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 of this Website
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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)