Skip to content

embed SAPI: unable to link on Mac #8533

Closed

Description

@dunglas

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

Activity

  1. dunglas commented on May 11, 2022

    @dunglas
    MemberAuthor

    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:
    
  2. stancl commented on Sep 24, 2024

    @stancl
    Contributor

    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.

  3. AndrewKusov commented on Sep 29, 2024

    @AndrewKusov

    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

  4. stancl commented on Sep 29, 2024

    @stancl
    Contributor
    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;
    }
  5. stancl commented on Sep 29, 2024

    @stancl
    Contributor

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

  6. dunglas commented on Dec 16, 2024

    @dunglas
    MemberAuthor

    I proposed a fix in this PR: #17183

  7. added 3 commits that reference this issue on Mar 12, 2025
    74a455f
    a191e7a
    009b5e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions