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

Compile with static php shows error #4172

Open
crazywhalecc opened this issue Apr 24, 2021 · 13 comments
Open

Compile with static php shows error #4172

crazywhalecc opened this issue Apr 24, 2021 · 13 comments

Comments

@crazywhalecc
Copy link

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a simple script for reproducing the error.
    compile static swoole and php. Scripts:
cd php-7.4.16/
./buildconf --force && ./configure LDFLAGS=-static --disable-all --enable-shared=no --enable-static=yes --enable-inline-optimization --enable-mbstring --with-layout=GNU --enable-filter --with-zlib --enable-json --enable-ctype --enable-swoole --disable-phpdbg --disable-cgi
make LDFLAGS=-ldl -j8
  1. What did you expect to see?
    Compile success with an php binary.

  2. What did you see instead?

/usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../x86_64-alpine-linux-musl/bin/ld: ext/swoole/src/lock/mutex.o: in function `swoole::Mutex::lock_wait(int)':
/root/compiler/source/php-7.4.16/ext/swoole/include/swoole.h:548: undefined reference to `swoole_clock_gettime(int, timespec*)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:245: sapi/cli/php] Error 1
  1. What version of Swoole are you using (show your php --ri swoole)?
    latest version(github), commit: e86eac3

  2. What is your machine environment used (show your uname -a & php -v & gcc -v) ?

  • php: 7.4.16
  • Linux my-computer 5.4.72-microsoft-standard-WSL2 编译出错 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 Linux
  • gcc version 9.3.0 (Alpine 9.3.0)
@crazywhalecc
Copy link
Author

Recently I tried to build an php statically linked binary with swoole and some other extensions, to make php real portable, flexible. But I have some trouble, like this issue.

Now I can successfully build it and run my own swoole application with no dependencies. But I still cannot compile xml(libxml2 required) and curl(libcurl required), some environment problem have not been solved.

@crazywhalecc
Copy link
Author

Recently I tried to build an php statically linked binary with swoole and some other extensions, to make php real portable, flexible. But I have some trouble, like this issue.

Now I can successfully build it and run my own swoole application with no dependencies. But I still cannot compile xml(libxml2 required) and curl(libcurl required), some environment problem have not been solved.

I successfully built it with xml and curl. https://github.com/crazywhalecc/static-php-swoole

But it seems php 7.3 cannot be compiled (swoole 4.6.6) and it shows another error like swoole_mysql_coro.cc:550: undefined reference to 'PHP_SHA256Init'

@matyhtf
Copy link
Member

matyhtf commented Apr 29, 2021

@crazywhalecc You should enable hash extension

@suconghou
Copy link

same error when static compile swoole4.6.6 php7.4.19 in alpine

#19 264.4 /usr/lib/gcc/x86_64-alpine-linux-musl/10.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: ext/swoole/src/lock/mutex.o: in function `swoole::Mutex::lock_wait(int)':
#19 264.4 /php-7.4.19/ext/swoole/include/swoole.h:547: undefined reference to `swoole_clock_gettime(int, timespec*)'

dockerfile

https://github.com/suconghou/docker/blob/master/php/74/Dockerfile-swoole

@matyhtf

you can see all the configuration and produce this error easily

@crazywhalecc
Copy link
Author

crazywhalecc commented May 21, 2021

Another problem: when using --enable-swoole-curl option, it shows multiple definition of _php_curl_verify_handlers and some same type errors. Maybe caused by curl extension and swoole extension compilation order?

@crazywhalecc
Copy link
Author

crazywhalecc commented May 21, 2021

Another problem: when using --enable-swoole-curl option, it shows multiple definition of _php_curl_verify_handlers and some same type errors. Maybe caused by curl extension and swoole extension compilation order?

configure arguments:

./configure LDFLAGS=-static --prefix=/static-php-cli/php-dist --disable-all --enable-shared=no --enable-static=yes --enable-inline-optimization --with-layout=GNU --enable-calendar --enable-ctype --enable-filter --enable-openssl --enable-bcmath --with-openssl-dir=/usr --enable-pcntl --enable-openssl --with-openssl --with-iconv --enable-json --enable-mbstring --enable-phar --enable-pdo --with-pdo-mysql=mysqlnd --enable-sockets --with-curl --enable-swoole --enable-swoole-curl --enable-inotify --enable-redis --disable-redis-session --enable-simplexml --enable-dom --enable-xml --enable-xmlwriter --enable-xmlreader --with-zlib --enable-posix --enable-mysqlnd --enable-tokenizer --with-pear=no --disable-cgi --disable-phpdbg --enable-gd --with-libxml

make command:

make LDFLAGS=-ldl -j8 && make install

And curl is compiled manually, using arguments:

CC=gcc CXX=g++ CFLAGS=-fPIC CPPFLAGS=-fPIC ./configure --prefix="$_opt_dir/curl" \
            --without-nghttp2 \
            --with-ssl=/usr \
            --with-pic=pic \
            --enable-ipv6 \
            --enable-shared=no \
            --enable-static=yes \
            --without-libidn2 \
            --disable-ldap \
            --without-libpsl \
            --without-lber \
            --enable-ares

image

@wandersonwhcr
Copy link

wandersonwhcr commented Jul 22, 2021

@crazywhalecc
Copy link
Author

@crazywhalecc ty for this

https://github.com/crazywhalecc/static-php-cli/blob/master/docker/check-extensions.sh#L226

it saves my life.

it's written by me, a temporary solution 😢 , and this issue is created for it.

@wandersonwhcr
Copy link

And did --enable-shared=no and --enable-static=yes work for you?

Also, I had to add a sed -i 's/-export-dynamic/-all-static/g' Makefile to work.

@crazywhalecc
Copy link
Author

And did --enable-shared=no and --enable-static=yes work for you?

Also, I had to add a sed -i 's/-export-dynamic/-all-static/g' Makefile to work.

Yeah, it's the only solution for compiling static php I think.

@wandersonwhcr
Copy link

wandersonwhcr commented Aug 4, 2021

Unfortunately --enable-shared=no --enable-static=yes didn't work for me and I discovered why.

If I am in Alpine and I compile SAPI cli I always have a dynamic link with MUSL. I want to compile it statically but PHP source code has -export-dynamic fixed and can't be modified.

https://github.com/php/php-src/search?q=%22-export-dynamic%22

Maybe I am confusing about shared or static extensions versus system libraries.

At the end using the sed mentioned before it will work.

@wandersonwhcr
Copy link

wandersonwhcr commented Aug 10, 2021

FYI sed from -export-dynamic to -all-static disables dynamic load of extensions 😢

https://github.com/wandersonwhcr/docker-php-smallest/blob/main/Base.Dockerfile#L28

@jingjingxyk
Copy link
Contributor

jingjingxyk commented Apr 30, 2023

export LDFLAGS="-all-static"
make -j cli 

more info https://github.com/php/php-src/blob/bbe42a5ccd9edfee3a85cbf5a1b6b86054ddeb58/build/ltmain.sh#L1193

or

   sed -i 's/-export-dynamic/-all-static/g' Makefile
   make -j  cli 

两种都可以

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