Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

embed SAPI: unable to link on Mac #8533

Open
dunglas opened this issue May 11, 2022 · 6 comments
Open

embed SAPI: unable to link on Mac #8533

dunglas opened this issue May 11, 2022 · 6 comments

Comments

@dunglas
Copy link
Contributor

dunglas commented May 11, 2022

Description

I compiled PHP with the following options:

./configure \
    --enable-zts \
    --enable-embed=shared \
    --with-iconv=/opt/homebrew/opt/libiconv/ \
    --without-pcre-jit \
    --enable-debug

When trying to link libphp.so on Mac with the following flags:

CFLAGS: -Wall -Wno-unused-variable
CFLAGS: -I/usr/local/include/php -I/usr/local/include/php/Zend -I/usr/local/include/php/TSRM -I/usr/local/include/php/main
LDFLAGS: -L/usr/local/lib -lphp

I get this error:

link: running clang failed: exit status 1
ld: can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB) file '/usr/local/lib/libphp.so' for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This looks related to this StackOverflow answer: https://stackoverflow.com/a/32765319/1352334

On Mac, it looks lile the -bundle flag passed to cc must be replaced by -dynamiclib.

PHP Version

PHP 8.1.5

Operating System

Mac OS X

@dunglas
Copy link
Contributor Author

dunglas commented May 11, 2022

I'm able to link by adding this target to the Makefile:

libphp.dylib: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS)
	$(LIBTOOL) --mode=link $(CC) -dynamiclib $(LIBPHP_CFLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) -rpath $(phptempdir) $(EXTRA_LDFLAGS) $(LDFLAGS) $(PHP_RPATHS) $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@
	-@$(LIBTOOL) --silent --mode=install cp $@ $(phptempdir)/$@ >/dev/null 2>&1

I use the following configuration:

./configure \
    --enable-zts \
    --enable-embed \
    --with-iconv=/opt/homebrew/opt/libiconv/ \
    --without-pcre-jit

However, if I add the --enable-debug flag, I get the following error:

Undefined symbols for architecture arm64:
  "__emalloc_24", referenced from:

@stancl
Copy link

stancl commented Sep 24, 2024

Had the same issue and the target above worked for me, though I had to also specify the install name otherwise the linker kept ignoring it even when the path was explicitly set in linker flags.

@AndrewKusov
Copy link

Had the same issue and the target above worked for me, though I had to also specify the install name otherwise the linker kept ignoring it even when the path was explicitly set in linker flags.

Can you tell me where you put the name? My target isn't working either

@stancl
Copy link

stancl commented Sep 29, 2024

libphp.dylib: $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS)
-	$(LIBTOOL) --mode=link $(CC) -dynamiclib $(LIBPHP_CFLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) -rpath $(phptempdir) $(EXTRA_LDFLAGS) $(LDFLAGS) $(PHP_RPATHS) $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@
+	$(LIBTOOL) --mode=link $(CC) -dynamiclib $(LIBPHP_CFLAGS) $(CFLAGS_CLEAN) $(EXTRA_CFLAGS) -rpath $(phptempdir) -install_name $(phptempdir)/$@ $(EXTRA_LDFLAGS) $(LDFLAGS) $(PHP_RPATHS) $(PHP_GLOBAL_OBJS) $(PHP_SAPI_OBJS) $(EXTRA_LIBS) $(ZEND_EXTRA_LIBS) -o $@
	-@$(LIBTOOL) --silent --mode=install cp $@ $(phptempdir)/$@ >/dev/null 2>&1

This makes me able to link it, but the path seems to be hardcoded so I can't move the .dylib to another directory. Don't have enough linker/build system knowledge to know what the right solution is here.

CFLAGS=-I/Users/samuel/php-src -I/Users/samuel/php-src/main -I/Users/samuel/php-src/TSRM -I/Users/samuel/php-src/Zend
LDFLAGS=-Wl,-rpath,/Users/samuel/php-src/libs -L/Users/samuel/php-src/libs -lphp
#include <sapi/embed/php_embed.h>

int main(int argc, char **argv) {
    php_embed_init(argc, argv);

    zend_eval_string("echo 'Hello from PHP!\n';", NULL, "Embedded PHP code");

    php_embed_shutdown();

    return 0;
}

@stancl
Copy link

stancl commented Sep 29, 2024

-install_name @rpath/$@ seems to make it movable.

@dunglas
Copy link
Contributor Author

dunglas commented Dec 16, 2024

I proposed a fix in this PR: #17183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants