[Libwebsockets] Compiling examples with static WolfSSL and static Libwebsockets

Andy Green andy at warmcat.com
Wed May 19 09:44:54 CEST 2021



On 5/19/21 3:59 AM, Joman Chu wrote:
> Hi all,
> 
> I'm trying to compile the examples but statically linking against
> WolfSSL and Libwebsockets, but the compilation fails when linking with
> one of the examples, missing references to `pow` and `log` in wolfssl.
> I can successfully get it to link if I add a `-lm` at the end of the
> invocation of gcc.
> 
> I'm compiling in an Ubuntu 20.04 container

Thanks for the clear explanation about the background.

Wolfssl pkgconfig is like this for me

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: wolfssl
Description: wolfssl C library.
Version: 4.5.0
Libs: -L${libdir} -lwolfssl
Cflags: -I${includedir}

ie, using it isn't going to inform us about the unresolved references to 
libm.

Since wolfssl is supposed to be good for embedded, binding to libm 
unconditionally is a bit unexpected.  Grepping for where pow() and log() 
are used in wolfssl, it's only built if WOLFSSL_DH_CONST is not defined. 
  But looking around, no obvious place to define that externally in 
wolfssl build, it's not mentioned in CMakeLists.txt.  It appears in some 
IDE-specific headers.  So you could get rid of them that way by hacking 
it in wolfssl CMakeLists.txt.

Depending what else you build, and what its pkgconfig has, libm may be 
part of the lws link anyway, concealing this problem.

> Compiling WolfSSL:
> 
> ```
> ./configure --enable-opensslextra --enable-debug --enable-static
> --disable-shared --enable-intelasm --enable-aesni --disable-examples
> --disable-crypttests --enable-libwebsockets
> make CFLAGS=-DHAVE_EX_DATA
> ```
> 
> Compiling libwebsockets:
> 
> ```
> CFLAGS=-DHAVE_EX_DATA cmake .. -DLWS_WITH_SHARED:BOOL=OFF
> -DCMAKE_BUILD_TYPE=DEBUG -DLWS_WITH_WOLFSSL=1
> -DLWS_WOLFSSL_INCLUDE_DIRS=../../wolfssl
> -DLWS_WOLFSSL_LIBRARIES=../../wolfssl/src/.libs/libwolfssl.a
> -DLWS_WITH_MINIMAL_EXAMPLES=1
> make
> ```

The recommended dirty build flags hacking method is at cmake level. 
Doing it at CFLAGS= env level is a unreliable, because cmake only 
references that when creating CMakeCache.txt the first time, it will 
ignore what you have there subsequently, guaranteeing nasty confusion 
down the line.

Doing what you want that way looks like

cmake .. -DCMAKE_C_FLAGS=-DHAVE_EX_DATA 
-DCMAKE_STATIC_LINKER_FLAGS=/usr/lib64/libm.a

followed by your normal lws cmake args.

-Andy


More information about the Libwebsockets mailing list