编译pngquant的静态版本

3
我正在尝试在Oracle Linux Server release 7.1中创建pngquant的静态链接版本。我已经编译了静态版本的zlib和libpng。

然后,当我配置pngquant时,总是会得到它将与共享版本的zlib链接的信息。

$ ./configure --with-libpng=../libpng-1.6.21 --extra-cflags="-I../zlib-1.2.8" --extra-ldflags="../zlib-1.2.8/libz.a"
Compiler: gcc Debug: no SSE: yes OpenMP: no libpng: static (1.6.21) zlib: shared (1.2.7) lcms2: no
如果我执行make,在输出中似乎选项已正确传递给编译器。然而,生成的二进制文件需要libz.so才能执行。看来我的指令被忽略或者安装的版本总是优先。

有没有任何方法可以强制编译使用静态版本的zlib呢?
2个回答

1

我不确定是否理解正确,但这是一个对我有效的pngquant配置补丁。现在configure接受--with-zlib=<dir>作为参数。将其存储到pngquant.patch中,并使用patch -uN -p1 -i pngquant.patch进行应用。

    diff -ur pngquant-2.9.0/configure pngquant-2.9.0.fixed/configure
--- pngquant-2.9.0/configure    2017-03-06 09:37:30.000000000 +0100
+++ pngquant-2.9.0.fixed/configure  2017-03-07 09:57:20.246012152 +0100
@@ -48,6 +48,7 @@
         help "--with-cocoa/--without-cocoa  use Cocoa framework to read images"
 fi
         help "--with-libpng=<dir>           search for libpng in directory"
+        help "--with-zlib=<dir>             search for zlib in directory"
         echo
         help "CC=<compiler>                 use given compiler command"
         help "CFLAGS=<flags>                pass options to the compiler"
@@ -97,6 +98,9 @@
     --with-libpng=*)
         LIBPNG_DIR=${i#*=}
         ;;
+    --with-zlib=*)
+        ZLIB_DIR=${i#*=}
+        ;;
     --prefix=*)
         PREFIX=${i#*=}
         ;;
@@ -238,6 +242,19 @@
     echo "${MAJ}${MIN}"
 }
 
+# returns full zlib.h version string
+zlibh_string() {
+    echo "$(grep -m1 "define ZLIB_VERSION" "$1" | \
+            grep -Eo '"[^"]+"' | grep -Eo '[^"]+')"
+}
+
+# returns major minor version numbers from png.h
+zlibh_majmin() {
+    local MAJ=$(grep -m1 "define ZLIB_VER_MAJOR" "$1" | grep -Eo "[0-9]+")
+    local MIN=$(grep -m1 "define ZLIB_VER_MINOR" "$1" | grep -Eo "[0-9]+")
+    echo "${MAJ}${MIN}"
+}
+
 error() {
     status "$1" "error ... $2"
     echo
@@ -420,11 +437,42 @@
     error "libpng" "not found (try: $LIBPNG_CMD)"
 fi
 
-# zlib
-if ! find_library "zlib" "z" "zlib.h" "libz.a" "libz.$SOLIBSUFFIX*"; then
-    error "zlib" "not found (please install zlib-devel package)"
+# try if given flags are enough for zlib
+HAS_ZLIB=0
+if echo "#include \"zlib.h\"
+    int main(){
+    uLong test = zlibCompileFlags();
+    return 0;
+}" | "$CC" -xc -std=c99 -o /dev/null $CFLAGS $LDFLAGS - &> /dev/null; then
+    status "zlib" "custom flags"
+    HAS_ZLIB=1
 fi
 
+if [ "$HAS_ZLIB" -eq 0 ]; then
+    # try static in the given directory
+    ZLIBH=$(find_h "$ZLIB_DIR" "zlib.h")
+    if [ -n "$ZLIBH" ]; then
+        ZLIBH_STRING=$(zlibh_string "$ZLIBH")
+        ZLIBH_MAJMIN=$(zlibh_majmin "$ZLIBH")
+        if [[ -n "$ZLIBH_STRING" && -n "$ZLIBH_MAJMIN" ]]; then
+            ZLIBA=$(find_f "$ZLIB_DIR" "libz${ZLIBH_MAJMIN}.a")
+            if [ -z "$ZLIBA" ]; then
+                ZLIBA=$(find_f "$ZLIB_DIR" "libz.a")
+            fi
+            if [ -n "$ZLIBA" ]; then
+                cflags "-I${ZLIBH%/*}"
+                lflags "${ZLIBA}"
+                status "zlib" "static (${ZLIBH_STRING})"
+                HAS_ZLIB=1
+            fi
+        fi
+    fi
+fi
+# zlib
+if ! find_library "zlib" "z" "zlib.h" "libz.a" "zlib.$SOLIBSUFFIX*"; then
+    error "zlib" "not found (please install zlib-devel package)"
+fi
+
 # lcms2
 if [ "$LCMS2" != 0 ]; then
     if find_library "lcms2" "lcms2" "lcms2.h" "liblcms2.a" "liblcms2.$SOLIBSUFFIX*"; then

我应用了这个补丁,但现在配置脚本没有显示任何 zlib。它不应该输出:zlib static (x.x.x),其中 x 是被链接的版本号吗? - Bryan
啊,我明白了。你在补丁的底部把"libz.a"改成了"zlib.a"。那个名字是错误的,找不到对应的文件。使用正确的名字(libz.a)会让它按预期工作。 - Bryan

0

抱歉,配置脚本不支持它。修改配置以传递适当的标志至pkg-config或者进行与libpng相同的解决方法应该不会太难。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接