Mac OS X上编译mockcpp过程
OS版本:
banxia:mockcpp yangjia$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.14
BuildVersion: 18A391
mockcpp
支持cmake
,编译步骤如下:
cd mockcpp/mockcpp
cmake .
make
第一次编译错误
用上面的方法编译到InterfaceInfo.cpp
时报错:
/Users/yangjia/repos/mockcpp/mockcpp/src/InterfaceInfo.cpp:17:12: error:
unknown type name '__class_type_info'
const __class_type_info* info, unsigned int number);
^
/Users/yangjia/repos/mockcpp/mockcpp/src/InterfaceInfo.cpp:21:43: error:
unknown type name '__base_class_type_info'
getNumberOfVtblsByBaseClassTypeInfo(const __base_class_type_info& info
/Users/yangjia/repos/mockcpp/mockcpp/src/InterfaceInfo.cpp:41:29: error:
unknown type name '__vmi_class_type_info'
getNumberOfVtblsByVmi(const __vmi_class_type_info* vmi, unsigned int number)
难道编译器不支持rtti
?
设置cmake
的选项CMAKE_VERBOSE_MAKEFILE
,让cmake
输出编译的详细信息:
set(CMAKE_VERBOSE_MAKEFILE ON)
注意:需要在mockcpp/mockcpp/CMakeLists.txt
和mockcpp/mockcpp/src/CMakeLists.txt
两个文件都增加这个选项才能生效。
编译输出为:
[ 10%] Building CXX object src/CMakeFiles/mockcpp.dir/InterfaceInfo.cpp.o
cd /Users/yangjia/repos/mockcpp/mockcpp/src && /Library/Developer/CommandLineTools/usr/bin/c++ -DMOCKCPP_ALLOW_MULTI_INHERITANCE=1 -DMOCKCPP_MAX_INHERITANCE=2 -DMOCKCPP_MAX_PARAMETERS=12 -DMOCKCPP_MAX_VTBL_SIZE=20 -DMOCKCPP_NO_NAMESPACE=0 -DPACKAGE=\"mockcpp\" -DPACKAGE_BUGREPORT=\"dev.mockcpp@gmail.com\" -DPACKAGE_NAME=\"mockcpp\" -DPACKAGE_STRING=\"mockcpp\" -DPACKAGE_URL=\"http://code.google.com/p/mockcpp\" -DVERSION=\"2.6\" -I/Users/yangjia/repos/mockcpp/mockcpp/3rdparty -I/Users/yangjia/repos/mockcpp/mockcpp/include -fPIC -o CMakeFiles/mockcpp.dir/InterfaceInfo.cpp.o -c /Users/yangjia/repos/mockcpp/mockcpp/src/InterfaceInfo.cpp
原来用的是xcode
自带的clang
:
$ /Library/Developer/CommandLineTools/usr/bin/c++ -v
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin18.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
果断换成gcc
,设置cmake
的选项CMAKE_CXX_COMPILER
(mockcpp/mockcpp/src/CMakeLists.txt
设置即可):
正好用homebrew
装了一个7.3.0
版本的gcc
,就用这个版本了:
SET(CMAKE_CXX_COMPILER /usr/local/bin/g++-7)
第二次编译错误
编译器换成g++
后又报错了:
[ 10%] Building CXX object src/CMakeFiles/mockcpp.dir/AfterMatcher.cpp.o
cd /Users/yangjia/repos/mockcpp/mockcpp/src && /usr/local/bin/g++-7 -DMOCKCPP_ALLOW_MULTI_INHERITANCE=1 -DMOCKCPP_MAX_INHERITANCE=2 -DMOCKCPP_MAX_PARAMETERS=12 -DMOCKCPP_MAX_VTBL_SIZE=20 -DMOCKCPP_NO_NAMESPACE=0 -DPACKAGE=\"mockcpp\" -DPACKAGE_BUGREPORT=\"dev.mockcpp@gmail.com\" -DPACKAGE_NAME=\"mockcpp\" -DPACKAGE_STRING=\"mockcpp\" -DPACKAGE_URL=\"http://code.google.com/p/mockcpp\" -DVERSION=\"2.6\" -I/Users/yangjia/repos/mockcpp/mockcpp/3rdparty -I/Users/yangjia/repos/mockcpp/mockcpp/include -fPIC -o CMakeFiles/mockcpp.dir/AfterMatcher.cpp.o -c /Users/yangjia/repos/mockcpp/mockcpp/src/AfterMatcher.cpp
In file included from /Users/yangjia/repos/mockcpp/mockcpp/include/mockcpp/AfterMatcher.h:24:0,
from /Users/yangjia/repos/mockcpp/mockcpp/src/AfterMatcher.cpp:19:
/Users/yangjia/repos/mockcpp/mockcpp/include/mockcpp/mockcpp.h:60:8: error: expected identifier before 'static_assert'
struct static_assert
^~~~~~~~~~~~~
对应的代码如下:
// mockcpp.h
template <bool condition>
struct static_assert
{
typedef int static_assert_failure[condition ? 1 : -1];
};
moclkcpp
自己定义了一个static_assert
,但是c++11
已经引入了static_assert
,两者冲突了。一个解决方案是c++11
以后就不要编这段代码了:
#if __cplusplus < 201103L
template <bool condition>
struct static_assert
{
typedef int static_assert_failure[condition ? 1 : -1];
};
#endif
用了__cplusplus
预编译宏,详细说明见这里
修改后,编译终于通过了。
代码已经放到github上了。
网友评论