You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP-CPP扩展初始化失败:模块与PHP API版本不兼容求助

问题:PHP-CPP扩展与PHP模块API版本不匹配错误

初始报错

在XAMPP服务器上运行PHP项目,使用基于PHP-CPP开发的C++扩展时,执行/opt/lampp/bin/php -m出现以下警告:

PHP Warning:  PHP Startup: module_nametwo: Unable to initialize module
Module compiled with module API=20210902
PHP    compiled with module API=20220829

系统环境信息:

  • 系统PHP版本:8.2.15
  • XAMPP的PHP版本:8.2.12
  • 系统Apache版本:2.4.52
  • XAMPP的Apache版本:2.4.58

更新操作后新报错

尝试升级模块后,出现新的版本不匹配错误:
在XAMPP中执行:

$ /opt/lampp/bin/php -m
PHP Warning:  PHP Startup: simple_two: Unable to initialize module
Module compiled with module API=20230831
PHP    compiled with module API=20220829

在本地PHP中执行:

$ php -m
PHP Warning:  PHP Startup: simple_two: Unable to initialize module
Module compiled with module API=20230831
PHP    compiled with module API=20220829

随后执行sudo apt remove php8.3*卸载PHP8.3,通过sudo update-alternatives --display php确认当前PHP版本为8.2。

修改后的Makefile内容

更新Makefile如下:

#   Makefile template
#
#   This is an example Makefile that can be used by anyone who is building
#   his or her own PHP extensions using the PHP-CPP library. 
#
#   In the top part of this file we have included variables that can be
#   altered to fit your configuration, near the bottom the instructions and
#   dependencies for the compiler are defined. The deeper you get into this
#   file, the less likely it is that you will have to change anything in it.
#


#
#   Name of your extension
#
#   This is the name of your extension. Based on this extension name, the
#   name of the library file (name.so) and the name of the config file (name.ini)
#   are automatically generated
#


NAME                =   simple_two




#
#   Php.ini directories
#
#   In the past, PHP used a single php.ini configuration file. Today, most
#   PHP installations use a conf.d directory that holds a set of config files,
#   one for each extension. Use this variable to specify this directory.
#


#INI_DIR             =   /etc/php/8.2/cli/conf.d




#
#   The extension dirs
#
#   This is normally a directory like /usr/lib/php5/20121221 (based on the 
#   PHP version that you use. We make use of the command line 'php-config' 
#   instruction to find out what the extension directory is, you can override
#   this with a different fixed directory
#


EXTENSION_DIR       =   $(shell  /opt/lampp/bin/php-config --extension-dir)




#
#   The name of the extension and the name of the .ini file
#
#   These two variables are based on the name of the extension. We simply add
#   a certain extension to them (.so or .ini)
#


EXTENSION           =   ${NAME}.so
INI                 =   ${NAME}.ini




#
#   Compiler
#
#   By default, the GNU C++ compiler is used. If you want to use a different
#   compiler, you can change that here. You can change this for both the 
#   compiler (the program that turns the c++ files into object files) and for
#   the linker (the program that links all object files into the single .so
#   library file. By default, g++ (the GNU C++ compiler) is used for both.
#


COMPILER            =   g++
LINKER              =   g++




#
#   Compiler and linker flags
#
#   This variable holds the flags that are passed to the compiler. By default, 
#   we include the -O2 flag. This flag tells the compiler to optimize the code, 
#   but it makes debugging more difficult. So if you're debugging your application, 
#   you probably want to remove this -O2 flag. At the same time, you can then 
#   add the -g flag to instruct the compiler to include debug information in
#   the library (but this will make the final libphpcpp.so file much bigger, so
#   you want to leave that flag out on production servers).
#
#   If your extension depends on other libraries (and it does at least depend on
#   one: the PHP-CPP library), you should update the LINKER_DEPENDENCIES variable
#   with a list of all flags that should be passed to the linker.
#


COMPILER_FLAGS      =   -Wall -c -O2 -std=c++11 -fpic -o
LINKER_FLAGS        =   -shared
LINKER_DEPENDENCIES =   -lphpcpp -L/usr/lib/php/20220829




#
#   Command to remove files, copy files and create directories.
#
#   I've never encountered a *nix environment in which these commands do not work. 
#   So you can probably leave this as it is
#


RM                  =   rm -f
CP                  =   cp -f
MKDIR               =   mkdir -p




#
#   All source files are simply all *.cpp files found in the current directory
#
#   A builtin Makefile macro is used to scan the current directory and find 
#   all source files. The object files are all compiled versions of the source
#   file, with the .cpp extension being replaced by .o.
#


SOURCES             =   $(wildcard *.cpp)
OBJECTS             =   $(SOURCES:%.cpp=%.o)




#
#   From here the build instructions start
#


all:                    ${OBJECTS} ${EXTENSION}


${EXTENSION}:           ${OBJECTS}
                       ${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}


${OBJECTS}:
                       ${COMPILER} ${COMPILER_FLAGS} $@ ${@:%.o=%.cpp}


install:        
                       ${CP} ${EXTENSION} ${EXTENSION_DIR}
#                       ${CP} ${INI} ${INI_DIR}
               
clean:
                       ${RM} ${EXTENSION} ${OBJECTS}

后续配置操作

  • 手动将extension=simple_two.so添加到/opt/lampp/etc/php.ini文件中
  • 将simple_two.ini复制到/etc/php/8.2/mods-available/,并在/etc/php/8.2/cli/conf.d中创建指向该文件的链接
  • simple_two.so文件已放置在/opt/lampp/lib/php/no-debug-non-zts-20220829目录下

但上述操作后问题仍未解决,怀疑错误与PHP-CPP库的编译有关,特此求助。


内容的提问来源于stack exchange,提问作者reham501

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:54:54