liudf0716 / xfrpc

The xfrpc project is a lightweight implementation of the FRP client written in C language for OpenWRT and IoT systems. It is designed to provide an efficient solution for resource-constrained devices such as OpenWRT routers and IoT devices, which often have limited ROM and RAM space.
GNU General Public License v3.0
712 stars 89 forks source link

build: support build static binary #21

Closed skmkt closed 1 year ago

skmkt commented 1 year ago

Build static binary in Alpine container.

Possible solution for https://github.com/liudf0716/xfrpc/issues/17

liudf0716 commented 1 year ago

@skyraintm Thank you for your PR! We appreciate your contribution. We hope you can contribute more in the future.

liudf0716 commented 1 year ago

@skyraintm
In my Ubuntu 20.04.03 LTS system, I seem to be encountering some link errors.

liudf@liudf:/srv/openwrt/xfrpc/build$ pwd
/srv/openwrt/xfrpc/build
liudf@liudf:/srv/openwrt/xfrpc/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  Makefile  xfrpc
liudf@liudf:/srv/openwrt/xfrpc/build$ rm -rf *
liudf@liudf:/srv/openwrt/xfrpc/build$ 
liudf@liudf:/srv/openwrt/xfrpc/build$ 
liudf@liudf:/srv/openwrt/xfrpc/build$ 
liudf@liudf:/srv/openwrt/xfrpc/build$ 
liudf@liudf:/srv/openwrt/xfrpc/build$ pwd
/srv/openwrt/xfrpc/build
liudf@liudf:/srv/openwrt/xfrpc/build$ ls
liudf@liudf:/srv/openwrt/xfrpc/build$ cmake -D STATIC_BUILD=ON ..
-- The C compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
CMake Warning (dev) in CMakeLists.txt:
  A logical block opening on the line

    /srv/openwrt/xfrpc/CMakeLists.txt:5 (if)

  closes on the line

    /srv/openwrt/xfrpc/CMakeLists.txt:25 (endif)

  with mis-matching arguments.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Found libevent: /usr/lib/x86_64-linux-gnu/libevent.so
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libssl.so;/usr/lib/x86_64-linux-gnu/libcrypto.so (found version "1.1.1f") 
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.29.1") 
-- Found JSON-C: /usr/lib/x86_64-linux-gnu/libjson-c.so  
CMake Warning (dev) in CMakeLists.txt:
  A logical block opening on the line

    /srv/openwrt/xfrpc/CMakeLists.txt:63 (if)

  closes on the line

    /srv/openwrt/xfrpc/CMakeLists.txt:65 (endif)

  with mis-matching arguments.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /srv/openwrt/xfrpc/build
liudf@liudf:/srv/openwrt/xfrpc/build$ make
Scanning dependencies of target xfrpc
[  5%] Building C object CMakeFiles/xfrpc.dir/main.c.o
[ 10%] Building C object CMakeFiles/xfrpc.dir/client.c.o
[ 15%] Building C object CMakeFiles/xfrpc.dir/config.c.o
[ 20%] Building C object CMakeFiles/xfrpc.dir/control.c.o
[ 25%] Building C object CMakeFiles/xfrpc.dir/ini.c.o
[ 30%] Building C object CMakeFiles/xfrpc.dir/msg.c.o
[ 35%] Building C object CMakeFiles/xfrpc.dir/xfrpc.c.o
[ 40%] Building C object CMakeFiles/xfrpc.dir/debug.c.o
[ 45%] Building C object CMakeFiles/xfrpc.dir/zip.c.o
[ 50%] Building C object CMakeFiles/xfrpc.dir/commandline.c.o
[ 55%] Building C object CMakeFiles/xfrpc.dir/crypto.c.o
[ 60%] Building C object CMakeFiles/xfrpc.dir/fastpbkdf2.c.o
[ 65%] Building C object CMakeFiles/xfrpc.dir/utils.c.o
[ 70%] Building C object CMakeFiles/xfrpc.dir/common.c.o
[ 75%] Building C object CMakeFiles/xfrpc.dir/login.c.o
[ 80%] Building C object CMakeFiles/xfrpc.dir/proxy_tcp.c.o
[ 85%] Building C object CMakeFiles/xfrpc.dir/proxy_ftp.c.o
[ 90%] Building C object CMakeFiles/xfrpc.dir/proxy.c.o
[ 95%] Building C object CMakeFiles/xfrpc.dir/tcpmux.c.o
[100%] Linking C executable xfrpc
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(b_addr.o): in function `BIO_lookup_ex':
(.text+0xd02): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(b_sock.o): in function `BIO_gethostbyname':
(.text+0x75): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libevent.a(evutil.o): in function `evutil_getaddrinfo_common_':
(.text+0x1c14): warning: Using 'getprotobynumber' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: (.text+0x1b79): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/libgcc_eh.a(unwind-dw2.o): in function `uw_init_context_1':
(.text+0x1f05): undefined reference to `pthread_once'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_lock_new':
(.text+0x4a): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_read_lock':
(.text+0x89): undefined reference to `pthread_rwlock_rdlock'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_write_lock':
(.text+0xa9): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_unlock':
(.text+0xc9): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_lock_free':
(.text+0xee): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_run_once':
(.text+0x129): undefined reference to `pthread_once'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_init_local':
(.text+0x149): undefined reference to `pthread_key_create'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_set_local':
(.text+0x17b): undefined reference to `pthread_setspecific'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_cleanup_local':
(.text+0x19b): undefined reference to `pthread_key_delete'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `openssl_init_fork_handlers':
(.text+0x207): undefined reference to `pthread_once'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_get_local':
(.text+0x167): undefined reference to `pthread_getspecific'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): in function `dlfcn_globallookup':
(.text+0x17): undefined reference to `dlopen'
/usr/bin/ld: (.text+0x2a): undefined reference to `dlsym'
/usr/bin/ld: (.text+0x35): undefined reference to `dlclose'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): in function `dlfcn_bind_func':
(.text+0x1b7): undefined reference to `dlsym'
/usr/bin/ld: (.text+0x282): undefined reference to `dlerror'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): in function `dlfcn_load':
(.text+0x2f5): undefined reference to `dlopen'
/usr/bin/ld: (.text+0x369): undefined reference to `dlclose'
/usr/bin/ld: (.text+0x3a5): undefined reference to `dlerror'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): in function `dlfcn_pathbyaddr':
(.text+0x466): undefined reference to `dladdr'
/usr/bin/ld: (.text+0x4d7): undefined reference to `dlerror'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): in function `dlfcn_unload':
(.text+0x6b8): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/xfrpc.dir/build.make:354: xfrpc] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/xfrpc.dir/all] Error 2
make: *** [Makefile:130: all] Error 2
liudf@liudf:/srv/openwrt/xfrpc/build$ 
skmkt commented 1 year ago

Yes, it is expected to fail with -static when link with glibc, may need rebuild glibc with different configuration flags, that why I use static link with musl libc in Alpine environment. Try following if docker is installed DOCKER_BUILDKIT=1 docker build --output out . -f docker/Dockerfile

liudf0716 commented 1 year ago

@skyraintm I understand now. In fact, I believe that Docker is sufficient for use on x86 platforms. We have another team member who is attempting to resolve the problem by including all third-party code within the xfrpc project. This solution may allow for the porting of xfrpc to certain IoT systems, such as RTOS.