1. 安装OpenSSL  version > 1.1.0
    参考  Ubuntu下升级openssl-1.1.1a
  2. 添加环境变量
    export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/openssl-1.1.1a/lib/pkgconfig
  3. Install libcoap
git clone https://github.com/obgm/libcoap.git
# To switch the libcoap version
# git checkout b425b150c2fdf0d24810bb6af55db4f6d4fb65b8
cd libcoap
autogen.sh
./configure --disable-documentation
# If has openssl problem, try this one
# ./configure --disable-documentation
make
sudo make install
# the detail libcoap version will be printed, please check it.

4. Check installed libcoap

ls /usr/local/include/
ls /usr/local/lib/

5. Execute ldconfig to active new library

sudo ldconfig

6. 运行examples

cd examples
./coap-server &
./coap-client -m get coap://127.0.0.1:5683/

使用Docker镜像环境编译libcoap

docker pull obgm/libcoap:build-env

这个镜像600多M,下了大半天才下完。

启动这个镜像,进入命令行

cd /opt
git clone https://github.com/obgm/libcoap.git
cd libcoap
sh autogen.sh
./configure  --disable-documentation

这里会显示当前libcoap版本的情况, API version在引用coap库时是要注意的。

libcoap configuration summary:
      libcoap package version : "4.2.0rc3"
      libcoap library version : "1.0.1"
      libcoap API version     : "2"
      libcoap DTLS lib extn   : "-gnutls"
      host system             : "x86_64-pc-linux-gnu"
      build DTLS support      : "yes"
          -->  GnuTLS around  : "yes" (found GnuTLS 3.5.19)
               GNUTLS_CFLAGS  : "-I/usr/include/p11-kit-1"
               GNUTLS_LIBS    : "-lgnutls"
      build doxygen pages     : "no"
      build man pages         : "no"
      build unit test binary  : "no"
      build examples          : "yes"
      build with gcov support : "no"

继续编译

# make
# make install

执行编译好的例子试试

# cd examples
# ./coap-server &
# ./coap-client -m get coap://127.0.0.1:5683/
This is a test server made with libcoap (see https://libcoap.net)
Copyright (C) 2010--2018 Olaf Bergmann <bergmann@tzi.org>
# ./coap-client -m get coap://127.0.0.1:5683/time
Dec 29 02:07:56