这事的起因是感觉vpnc在路由器上速度太慢了,当然结果是发现OpenConnect一样慢……

下面进入正题:

  1. 将交叉编译toolchain的bin目录添加到PATH环境变量中
  2. 配置环境变量CFLAGS="--static" LDFLAGS="-static -static-libgcc -static-libstdc++ "
    这里可以export,也可以在后面configure的过程中作为configure的参数。
    最关键的是CFLAGS的设置,之前没设这个,静态编译出来的玩意儿其实还是依赖了libc的(路由器上是uClibc),然后在路由器上会莫名其妙的can't resolve symbol 'rand_r',搜了很久,都只提到了LDFLAGS的设置,最后在一个SO答案的评论里才找到这个,当然也怪我gcc学艺不精……
  3. 编译zlib
    CROSS_PREFIX=mipsel-linux- ./configure --prefix=/jffs --static
  4. 编译libxml2
    ./configure --host=mipsel-linux --prefix=/jffs --without-c14n --without-catalog --without-debug --without-docbook --without-fexceptions --without-ftp --without-history --without-html --without-http --without-iso8859x --without-legacy --without-pattern --without-push --without-regexps --without-run-debug --without-sax1 --without-schemas --without-schematron --without-threads --without-valid --without-xinclude --without-xpath --without-xptr --without-zlib --without-lzma --without-coverage --without-python --enable-static --disable-shared
  5. 编译OpenSSL
    ./Configure --prefix=/jffs --cross-compile-prefix=mipsel-linux- no-shared mipsel-linux:gcc
  6. 编译OpenConnect
    ./configure --prefix=/jffs --host=mipsel-linux --disable-maintainer-mode --disable-shared --enable-static --disable-nls --with-vpnc-script=/etc/vpnc/vpnc-script --without-gnutls --without-openssl-version-check PKG_CONFIG_PATH=/jffs/lib/pkgconfig/
  7. file确认一下文件是statically linked,strip一下最终文件
  8. 复制到路由器上运行,会被警告“没编译libiconv将导致奇怪的问题”,无视即可(或者你可以自己再编译一下libiconv,将增加最终文件1MiB多的大小)
  9. 启动
    echo -n YOUR_PASS | /jffs/openconnect/sbin/openconnect --timestamp --syslog --base-mtu=1400 --no-proxy --script=/jffs/vpnc/vpnc-script --user=YOUR_USER --verbose --disable-ipv6 --passwd-on-stdin --no-cert-check YOUR_SERVER
    启动后依旧会报Set up DTLS failed; using SSL instead,无解……