paho.mqtt.c是eclipse开发的C语言跨平台mqtt client 开源库,paho.mqtt.cpp是对应的C++ Client,paho.mqtt
支持CMAKE编译,本文以脚本形式提供了使用CMAKE基于VS2015/Windows以及GCC/MinGW的编译过程。
cmake 这是必须的编译工具
Visual Studio 2015 OR MinGW for windows
gcc for linux
msvc_build.bat
要求paho.mqtt.c
,paho.mqtt.cpp
的源码文件夹与msvc_build.bat
脚本在同一级文件夹
执行以下脚本会编译出paho.mqtt.c
静态库,包括Debug和Release版本并安装到
release\paho_mqtt_c_windows_vc_x86_64
文件夹下,
如果指定了/CPP
参数,则会编译paho.mqtt.cpp
安装到release\paho_mqtt_cpp_windows_vc_x86_64
如果指定了/S
参数还会编译对应的动态库
:: 基于 Windows Visual Studio 2015 的 paho mqtt c/cpp 编译脚本
:: 可选参数:
:: /CPP - 编译paho.mqtt.cpp
:: /C - 编译paho.mqtt.c
:: /T - 编译运行单元测试
:: /E - 编译例子程序
:: /S - 编译动态库
:: /MT - 指定/MT编译选项,默认使用/MD
@ECHO OFF
SETLOCAL
ECHO make paho mqtt VS2015 project
IF NOT DEFINED VS140COMNTOOLS (
ECHO vs2015 NOT FOUND.
EXIT /B -1
)
ECHO vs2015 found.
WHERE cmake
IF ERRORLEVEL 1 (
ECHO cmake NOT FOUND.
EXIT /B -1
)
ECHO cmake found
SET sh_folder=%~dp0
:: 删除最后的 '\'
SET sh_folder=%sh_folder:~0,-1%
SET PROJECT_TYPE=c
SET BUILD_SAMPLES=FALSE
SET ENABLE_TEST=OFF
SET rel_suffix=_windows_vc_x86-64
SET with_static_crt=OFF
SET BUILD_SHARED=OFF
:: parse command arguments
:loop
IF x%1 == x GOTO :pare_end
IF /I "%1" == "/CPP" SET PROJECT_TYPE=cpp
IF /I "%1" == "/C" SET PROJECT_TYPE=c
IF /I "%1" == "/T" SET ENABLE_TEST=TRUE
IF /I "%1" == "/E" SET BUILD_SAMPLES=TRUE
IF /I "%1" == "/MT" SET with_static_crt=ON && SET rel_suffix=_windows_vc_mt_x86-64
SHIFT
GOTO :loop
:pare_end
SET src_folder=paho.mqtt.%PROJECT_TYPE%
SET release_prefix=paho_mqtt_%PROJECT_TYPE%
SET cmake_prefix_path=
IF "%PROJECT_TYPE%" == "cpp" (
SET cmake_prefix_path=-DCMAKE_PREFIX_PATH=%sh_folder%\release\paho_mqtt_c%rel_suffix%
)
SET _build_test=-DPAHO_ENABLE_TESTING=%ENABLE_TEST%
IF "%PROJECT_TYPE%" == "cpp" (
SET _build_test=-DPAHO_BUILD_TESTS=%ENABLE_TEST%
)
SET project_folder=%sh_folder%\build\%release_prefix%.vs2015
SET outdir=%sh_folder%\release\%release_prefix%%rel_suffix%
IF "%OUTDIR%x" == "x" SET OUTDIR=%outdir%
ECHO project_folder=%project_folder%
ECHO OUTDIR=%OUTDIR%
:: 生成项目工程文件
IF EXIST %project_folder% RMDIR %project_folder% /s/q
MKDIR %project_folder%
PUSHD %project_folder% || EXIT /B
IF NOT DEFINED VisualStudioVersion (
ECHO make MSVC environment ...
CALL "%VS140COMNTOOLS%..\..\vc/vcvarsall" x86_amd64
)
ECHO creating x86_64 Project for Visual Studio 2015 ...
CMAKE %sh_folder%\%src_folder% -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=%OUTDIR% ^
-DCMAKE_USER_MAKE_RULES_OVERRIDE=%sh_folder%\cmake\compiler_flag_overrides.cmake ^
-DPAHO_BUILD_SAMPLES=%BUILD_SAMPLES% ^
%_build_test% ^
-DCMAKE_DEBUG_POSTFIX=_d ^
-DWITH_STATIC_CRT=%with_static_crt% ^
%cmake_prefix_path% ^
-DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=%BUILD_SHARED% -DPAHO_WITH_SSL=FALSE || EXIT /B
cmake --build . --config Release --target install -- /maxcpucount || EXIT /B
cmake --build . --config Debug --target install -- /maxcpucount || EXIT /B
POPD
ENDLOCAL
gnu_build.sh
要求paho.mqtt.c
,paho.mqtt.cpp
的源码文件夹与msvc_build.bat
脚本在同一级文件夹
执行以下脚本同会编译出paho.mqtt.c
静态库,并安装到release/paho_mqtt_c-$machine
文件夹下
如果未指定环境变量BUILD_TYPE
为DEBUG
,默认编译Release
版本
如果指定了CPP
参数,则会编译paho.mqtt.cpp
安装到release\paho_mqtt_cpp-$machine
文件夹下
如果指定了BUILD_SHARED
参数还会编译对应的动态库
#!/bin/bash
# paho.mqtt C/C++ 编译脚本(支持交叉编译)
# Optional Command Arguments:
# ONLYBUILD 不执行 cmake 创建工程只编译版本
# C build paho.mqtt.c
# CPP build paho.mqtt.cpp
# BUILD_SAMPLES build samples
# BUILD_TEST build test
# BUILD_SHARED build dynamic library
# Optional Environment Variables:
# TOOLCHAIN_FILE 指定交叉编译的工具链文件
# MACHINE 目标平台, such as x86_64-linux-gnu,默认使用当前系统平台
# PREFIX 安装路径
# PROJECT_FOLDER cmake 生成的工程文件(Makefile)文件夹
# BUILD_TYPE 编译版本类型(DEBUG|RELEASE),默认 RELEASE
#字符串转大写
#参数:1字符串
function string_toupper()
{
echo $1 | tr '[a-z]' '[A-Z]'
}
sh_folder=$(cd "$(dirname $0)"; pwd -P)
folder_name=$(basename $sh_folder)
# 定义编译的版本类型(DEBUG|RELEASE)
build_type=RELEASE
[[ "$(string_toupper $BUILD_TYPE)" =~ DEBUG|RELEASE ]] && build_type=$(string_toupper ${BUILD_TYPE})
echo build_type=$build_type
PROJECT_TYPE=c
# 执行 cmake 创建工程
create_prj=ON
build_examples=OFF
enable_test=OFF
build_shared=OFF
while [[ $# -gt 0 ]]
do
# 转大写
key=$(string_toupper ${1})
case $key in
C)
PROJECT_TYPE=c;shift;;
CPP)
PROJECT_TYPE=cpp;shift;;
ONLYBUILD)
create_prj=OFF;shift;;
BUILD_SAMPLES)
build_examples=ON;shift;;
BUILD_TEST)
enable_test=ON;shift;;
BUILD_SHARED)
build_shared=ON;shift;;
*) echo INVALID ARGUMENT $key;exit 255;shift;;
esac
done
src_folder=paho.mqtt.$PROJECT_TYPE
release_prefix=paho_mqtt_$PROJECT_TYPE
machine=$(g++ -dumpmachine)
[ -n "$MACHINE" ] && machine=$MACHINE
cmake_prefix_path=
[ "$PROJECT_TYPE" = "cpp" ] && cmake_prefix_path=-DCMAKE_PREFIX_PATH=$sh_folder/release/paho_mqtt_c-$machine
_build_test="-DPAHO_ENABLE_TESTING=$ENABLE_TEST"
[ "$PROJECT_TYPE" = "cpp" ] && _build_test="-DPAHO_BUILD_TESTS=$ENABLE_TEST"
# 工具链文件
toolchain=
[ -n "$TOOLCHAIN_FILE" ] && toolchain="-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE"
# 安装路径
[ -n "$PREFIX" ] && install_prefix="$PREFIX"
[ -z "$PREFIX" ] && install_prefix=$sh_folder/release/$release_prefix-$machine
# 编译路径
[ -n "$PROJECT_FOLDER" ] && prj_folder="$PROJECT_FOLDER"
[ -z "$PROJECT_FOLDER" ] && prj_folder=$sh_folder/build/$release_prefix-$machine
echo =========================================================================
echo Configuration: $src_folder $machine:$build_type
echo Build Directory: $prj_folder
echo Install Directory: $install_prefix
echo =========================================================================
if [[ "${create_prj}" = ON ]]
then
[ -d $prj_folder ] && rm -fr $prj_folder
mkdir -p $prj_folder || exit
pushd $prj_folder || exit
echo "creating Project for $machine ..."
cmake "$sh_folder/$src_folder" -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=$build_type \
-DCMAKE_INSTALL_PREFIX=$install_prefix \
-DPAHO_BUILD_SAMPLES=$build_examples \
$_build_test \
-DCMAKE_DEBUG_POSTFIX=_d \
-DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=$build_shared -DPAHO_WITH_SSL=FALSE \
$cmake_prefix_path $toolchain || exit
else
pushd $prj_folder || exit
fi
cmake --build . --target install/strip -- -j8 || exit
popd
以上完整脚本参见码云仓库 https://gitee.com/l0km/paho.mqtt.build.git
如果你只想编译paho.mqtt C/C++
,可以如下直接克隆 https://gitee.com/l0km/paho.mqtt.build.git 这个项目到本地(一定要加--recursive
参数)
git clone --recursive https://gitee.com/l0km/paho.mqtt.build.git
然后执行其中的msvc_build.bat
或gnu_build.sh
就可以编译出你想要的paho.mqtt C/C++
库