swoole-src
swoole-src copied to clipboard
Compile with static php shows error
Please answer these questions before submitting your issue. Thanks!
- 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
-
What did you expect to see? Compile success with an php binary.
-
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
-
What version of Swoole are you using (show your
php --ri swoole
)? latest version(github), commit: e86eac3320c6d2bb007346abbad15d10d1a72cd5 -
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)
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.
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'
@crazywhalecc You should enable hash extension
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
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?
Another problem: when using
--enable-swoole-curl
option, it showsmultiple 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
@crazywhalecc ty for this
https://github.com/crazywhalecc/static-php-cli/blob/master/docker/check-extensions.sh#L226
it saved my life.
@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.
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.
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.
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.
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
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
δΈ€η§ι½ε―δ»₯