cmake:VS2015和GCC编译cpp_redis

Posted 10km

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cmake:VS2015和GCC编译cpp_redis相关的知识,希望对你有一定的参考价值。

cpp_redis是一个基于C++11编写的支持跨平台的redis客户端。使用起来很方便,cpp_redis支持CMAKE编译,编译也很简单,本文以脚本形式提供了使用CMAKE基于VS2015/Windows以及GCC/MinGW的编译过程。

环境要求

cmake 这是必须的编译工具

Visual Studio 2015 OR MinGW for windows

gcc for linux

Visual Studio 2015 编译脚本

msvc_build.bat
要求cpp_redis源码文件夹与msvc_build.bat脚本在同一级文件夹
执行以下脚本会编译cpp_redis 并安装到
release\\cpp_redis_windows_vc_mt_x86_64文件夹下(包含Debug和Release版本).
如果指定了/MD选项会安装到release\\cpp_redis_windows_vc_md_x86_64 文件夹下
如果未指定/S 参数,默认编译的是静态库

:: 基于 Windows Visual Studio 2015 的 cpp_redis 编译脚本
:: 可选参数:
::   /T              - 编译单元测试
::   /E              - 编译例子程序
::   /S              - 编译动态库,默认编译静态库
::   /MD             - 指定/MD编译选项,默认使用/MT

@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 build_test=OFF
SET build_examples=OFF
SET build_shared=OFF
SET msvcrt_config=/MT
SET rel_suffix=_windows_vc_mt_x86-64
:: parse command arguments
:loop
IF x%1 == x GOTO :pare_end
IF /I "%1" == "/T"      SET build_test=ON
IF /I "%1" == "/E"      SET build_examples=ON
IF /I "%1" == "/S"      SET build_shared=ON
IF /I "%1" == "/MD"     SET msvcrt_config=/MD && SET rel_suffix=_windows_vc_md_x86-64

SHIFT
GOTO :loop
:pare_end

SET src_folder=cpp_redis
SET release_prefix=cpp_redis

SET project_folder=%sh_folder%\\build\\%release_prefix%.vs2015
IF "%OUTDIR%x" == "x" SET OUTDIR=%sh_folder%\\release\\%release_prefix%%rel_suffix%

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% ^
	-DBUILD_SHARED_LIBS=%build_shared% ^
	-DBUILD_TESTS=%build_test% ^
	-DBUILD_EXAMPLES=%build_examples% ^
	-DMSVC_RUNTIME_LIBRARY_CONFIG=%msvcrt_config% || EXIT /B

cmake --build . --config Release --target install -- /maxcpucount || EXIT /B
cmake --build . --config Debug   --target install -- /maxcpucount || EXIT /B
POPD
ENDLOCAL

GNU C编译脚本

gnu_build.sh
要求cpp_redis源码文件夹与gnu_build.sh脚本在同一级文件夹
执行以下脚本会编译cpp_redis安装到release/cJSON-$machine 文件夹下
如果未指定环境变量BUILD_TYPEDEBUG,默认编译Release版本
如果未指定BUILD_SHARD 参数,默认编译的是静态库

#!/bin/bash
# cpp_redis 编译脚本(支持交叉编译)
# Optional Command Arguments:
#    ONLYBUILD      不执行 cmake 创建工程只编译版本
#	 BUILD_TEST     build test
#    BUILD_EXAMPLES build examples
#    BUILD_SHARD    build shared library,default static
# 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

# 执行 cmake 创建工程
create_prj=ON
build_examples=OFF
build_test=OFF
build_shared=OFF
while [[ $# -gt 0 ]]
do
# 转大写
key=$(string_toupper ${1})
case $key in
ONLYBUILD)
	create_prj=OFF;shift;;
BUILD_EXAMPLES)
	build_examples=ON;;
BUILD_TEST)
	build_test=ON;;
BUILD_SHARD)
	build_shared=ON;;
*) shift;;
esac
done

machine=$(g++ -dumpmachine)

[ -n "$MACHINE" ] && machine=$MACHINE

# 工具链文件
toolchain=
[ -n "$TOOLCHAIN_FILE" ] && toolchain="-DCMAKE_TOOLCHAIN_FILE=$TOOLCHAIN_FILE"

# 安装路径
[ -n "$PREFIX" ] && install_prefix="$PREFIX"
[ -z "$PREFIX" ] && install_prefix=$sh_folder/release/cpp_redis-$machine

# 编译路径
[ -n "$PROJECT_FOLDER" ] && prj_folder="$PROJECT_FOLDER"
[ -z "$PROJECT_FOLDER" ] && prj_folder=$sh_folder/build/cpp_redis-$machine

echo =========================================================================
echo       Configuration: cpp_redis $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/cpp_redis" -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_BUILD_TYPE=$build_type \\
		-DCMAKE_INSTALL_PREFIX=$install_prefix \\
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON \\
		-DBUILD_SHARED_LIBS=$build_shared \\
		-DBUILD_TESTS=$build_test \\
		-DBUILD_EXAMPLES=$build_examples \\
		-DCMAKE_DEBUG_POSTFIX=_d \\
		$toolchain || exit

else
	pushd $prj_folder || exit
fi

cmake --build . --target install/strip -- -j8 || exit 


popd


cpp_redis.build

以上完整脚本参见码云仓库 https://gitee.com/l0km/cpp_redis.build.git

如果你只想编译cpp_redis,可以如下直接克隆 https://gitee.com/l0km/cpp_redis.build.git 这个项目到本地(一定要加--recursive参数)

git clone --recursive https://gitee.com/l0km/cpp_redis.build.git

然后执行其中的msvc_build.batgnu_build.sh就可以编译出你想要的cpp_redis

以上是关于cmake:VS2015和GCC编译cpp_redis的主要内容,如果未能解决你的问题,请参考以下文章

cmake:VS2015和GCC编译paho.mqtt C/C++ client

cmake:解决MSVC下/MT 编译paho.mqtt.c的问题

zliblibzip libzippp 库编译(windows + cmake + vs2013)

[zlib] CMake + VS2015 下编译zlib,及使用小例

opencv配置过程 (cmake,vs2013,qt 5.4)

2015-08-26: GCC编译选项(转载)