简体   繁体   中英

Gmp PHP extension - MAMP PRO

There can be many reasons why a PHP extension is not loaded, but it is not always easy to directly point to reasons (and fixes) as the distance between compiling from sources to showing phpinfo() and then finally missing the extension is large.


Get the real error message first

One way to reduce the distance in trouble-shooting is to see if the extension can be loaded by PHP and if not, showing an error message.

A common test for that is to use the CLI SAPI ( PHP on the commandline Docs ) as it allows to reduce and easier control the PHP runtime environment while being compatible with the extension.

To start PHP with the default configuration (no .ini files), loading only the single extension binary to test and showing the configuration information, run:

$ php -n -d extension=gmp.so -i

Excerpt from Options Docs , also there is php --help :

-n               No php.ini file will be used
-d foo[=bar]     Define INI entry foo with value 'bar'
-i               PHP information

This should provoke an error (shown in the terminal on standard error) or show the extension loaded in the PHP information output (on standard output).

Alternatively, to reduce the output and only check for the error, execute an empty PHP statement with the -r command-line switch:

-r <code>        Run PHP <code> without using script tags <?..?>

The example with the GMP extension in question:

$ php -n -d extension=gmp.so -r ';'

This will exit non-zero ( exit status ) if there is a problem loading the extension displaying and error message on standard error and would exit with zero status in case the extension could be loaded:

$ php -n -d extension=gmp.so -r ';'
PHP Warning:  PHP Startup: Unable to load dynamic library 'gmp.so' (tried: /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so (dlopen(/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so, 9): no suitable image found.  Did find:
    /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture
    /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so: mach-o, but wrong architecture), /Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so.so (dlopen(/Applications/MAMP/bin/php/php7.3.27/lib/php/extensions/no-debug-non-zts-20180731/gmp.so.so, 9): image not found)) in Unknown on line 0
$ echo $?
254

As the example shows, the error is already in "PHP Startup" which is the typical phase where PHP signals diagnosis messages loading extensions.


Dlopen 9: no suitable image found: mach-o, but wrong architecture

The error message above shows that a) PHP is first of all unable to load the extension (as a dynamic library , .so file, a shared object file, that is the compiled extension) and b) that it failed to load as no suitable image was found:

PHP Warning: PHP Startup: Unable to load dynamic library 'gmp.so' (tried: <path> (dlopen( <path> , 9): no suitable image found. Did find: <path> : mach-o, but wrong architecture ...), <path> (dlopen( <path>.so , 9): image not found)) in Unknown on line 0

That means the file is available on disk (can be opened) but the image is not suitable, which means it does not match the architecture.


(there is some noise in the try for gmp.so.so that is done by php so one can pass -d extension=gmp without the extension to work directly, eg in a php- .ini to work on both *nix ( .so ) or windows ( .dll ). This part can be ignored, that is "image not found" as the file does not exists which is expected)


It must be the same architecture as PHP itself as PHP is already running and wants to load the binary extension - they need to fit.

To get the architecture of PHP, locate the PHP command:

$ which php
/Applications/MAMP/bin/php/php7.3.27/bin/php

This is the absolute path to the php binary. With it, it is now possible with the file(1) utility to obtain more information about it:

$ file /Applications/MAMP/bin/php/php7.3.27/bin/php
/Applications/MAMP/bin/php/php7.3.27/bin/php: Mach-O 64-bit executable x86_64

(or call $ file "$(which php)" for running both at once)

It shows the php binaries info incl. the x86_64 architecture at the end:

Mach-O 64-bit executable x86_64

As the shared object image to load (the compiled php extension gmp.so file) also needs to match it, the same file(1) utility can be used on the compiled extensions .so file in the same manner.

The comparison then should show the difference.

With this information at hand finally the extension can be compiled with the appropriate architecture.


Closing notes:

On Apple Silicon M1 I'm not specifically profound of compiling and its architectures and others can tell it better. From what I've seen you manged it by running brew with the arch(1) utility setting the architecture by arch -x86_64 <command> to x86_64 . On Apple Silicon this may require more tooling, namely Rosetta .

It seems to be something common M1 users blog about ( via Austen Cameron in Nov 2020 ) but this is entirely not my system.

From my own understanding it should be possible to set the architecture with compiler flags or on the configure line and thats normally it.

As brew has the information how to compile an extension on a system (the brew formula , here for gmp ) it is perhaps most straight forward to go with it and run the install under the correct architecture.

With the caveat that you need to start brew in (?) the correct architecture with the arch(1) utility as well (and the brew install).

I have a problem with you and the same problem, is it solved?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM