跳到主要內容

Getting Started with Google Test and CMake on Windows

Getting Started with Google Test and CMake on Windows

Versions

Google Test

Assume we placed unzipped gtest source in %GTEST_ROOT%. To get gtest-1.7.0 compiled with MSVC11, edit %GTEST_ROOT%\cmake\internal_utils.cmake.

macro(fix_default_compiler_settings_)$
   if (MSVC)$
     add_definitions( -D_VARIADIC_MAX=10 ) # Add this line
     # ...
   endif()
endmacro()

Note: You won’t need the workaround with MSVC12, or VS2013 (I envy you).

Open up “Developer Command Prompt for VS2012” (yeah, I LOVE cmdline) and type:

# Compile
cd %GTEST_ROOT%
mkdir build
cd build
cmake .. -G "NMake Makefiles"

# Install with prefix "c:\opt" (change it to whatever you like)
mkdir c:\opt\lib
mkdir c:\opt\include
xcopy *.lib c:\opt\lib
cd ..
xcopy include\gtest c:\opt\include\

Note: I actually did files/dirs copying in cygwin and welcome feedback for correction.

CMake

Let’s write a CMakeLists.txt for a gtest sample. I picked the sample2 in gtests source repo. You can find it in the %GTEST_ROOT%/samples or here.

The sample 2 involves files:

sample2.cc  
sample2.h  
sample2_unittest.cc

Now we add a CMakeLists.txt file of following content in the same folder.

cmake_minimum_required(VERSION 3.0)
project(gtestexp)

add_definitions( -D_VARIADIC_MAX=10 )

if(MSVC)
# We statically link to reduce dependancies
foreach(flag_var CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
    CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    if(${flag_var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
    endif(${flag_var} MATCHES "/MD")
    if(${flag_var} MATCHES "/MDd")
        string(REGEX REPLACE "/MDd" "/MTd" ${flag_var} "${${flag_var}}")
    endif(${flag_var} MATCHES "/MDd")
endforeach(flag_var)
endif(MSVC)


include_directories( c:/opt/include )
link_directories ( c:/opt/lib )

add_executable(unit sample2_test.cc sample2.cc)
target_link_libraries(unit gtest gtest_main)

The if(MSVC) ... endif(MSVC) block in above script forces static linkage of our executable (unit). Otherwise the default dynamic linkage is applied and would causes link error since gtest is built as static libs.

After all, again with “Developer Command Prompt for VS2012”.

mkdir build
cmake .. -G "NMake Makefiles"
nmake
unit.exe

Then you’re done with following test results.

[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from MyString
[ RUN      ] MyString.DefaultConstructor
[       OK ] MyString.DefaultConstructor (0 ms)
[ RUN      ] MyString.ConstructorFromCString
[       OK ] MyString.ConstructorFromCString (0 ms)
[ RUN      ] MyString.CopyConstructor
[       OK ] MyString.CopyConstructor (0 ms)
[ RUN      ] MyString.Set
[       OK ] MyString.Set (0 ms)
[----------] 4 tests from MyString (15 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (15 ms total)
[  PASSED  ] 4 tests.

You can also generate xUnit compatible XML report with --gtest_output option. i.e.

unit.exe --gtest_output=xml:unit.xml

Report content:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="4" failures="0" disabled="0" errors="0" timestamp="2014-12-17T01:21:55" time="0.016" name="AllTests">
  <testsuite name="MyString" tests="4" failures="0" disabled="0" errors="0" time="0.016">
    <testcase name="DefaultConstructor" status="run" time="0" classname="MyString" />
    <testcase name="ConstructorFromCString" status="run" time="0" classname="MyString" />
    <testcase name="CopyConstructor" status="run" time="0" classname="MyString" />
    <testcase name="Set" status="run" time="0" classname="MyString" />
  </testsuite>
</testsuites>

Written with StackEdit.

留言

這個網誌中的熱門文章

得利油漆色卡編碼方式

得利油漆色卡編碼方式 類似 Munsell 色彩系統 ,編碼方式為 HUE LRV/CHROMA 例如 10GY 61/449 ( 色卡 ) 編碼數值 描述 10GY hue ,色輪上從 Y(ellow) 到 G(reen) 區分為 0 ~ 99 ,數值越小越靠近 Y,越大越靠近 G 61 LRV (Light Reflectance Value) 塗料反射光源的比率,數值從 0% ~ 100% ,越高越亮,反之越暗,也可理解為明度 449 chroma 可理解為彩度,數值沒有上限,越高顏色純度 (濃度) 越高 取決於測量儀器,對應至 RGB 並不保證視覺感受相同。 參考資料: 色卡對照網站 e-paint.co.uk Written with StackEdit .

C++17 新功能 try_emplace

C++17 新功能 try_emplace 回顧 emplace 大家的好朋友 Standard Template Library (STL) 容器提供如 push_back , insert 等介面,讓我們塞東西進去; C++11 之後,新增了 emplace 系列的介面,如 std::vector::emplace_back , std::map::emplace 等,差異在於 emplace 是在容器內 in-place 直接建構新元素,而不像 push_back 在傳遞參數前建構,下面用實例來說明: struct Value { // ctor1 Value ( int size ) : array ( new char [ size ] ) , size ( size ) { printf ( "ctor1: %d\n" , size ) ; } // ctor2 Value ( const Value & v ) : array ( new char [ v . size ] ) , size ( v . size ) { printf ( "ctor2: %d\n" , size ) ; memcpy ( array . get ( ) , v . array . get ( ) , size ) ; } private : std :: unique_ptr < char [ ] > array ; int size = 0 ; } ; struct Value 定義了自訂建構子 (ctor1),以指定大小 size 配置陣列,複製建構子 (ctor2) 則會配置與來源相同大小及內容的陣列,為了方便觀察加了一些 printf 。當我們如下使用 std::vector::push_back 時 std :: vector < Value > v ; v . push_back ( Value ( 2048 ) ) ; 首先 Value 會先呼叫 ctor1,傳給 push_ba...

Notes for C/C++ Programming

Notes for C/C++ Programming Notes for C/C++ Programming Off by One Error Prone # define LEN 128 char buf [ LEN ] = { 0 } ; // -1 for prevent missing NULL terminator // since strncpy doesn't preserve it for us. strncpy ( buf , something , sizeof ( buf ) - 1 ) ; // snprintf does preserve NULL terminator for us // i.e. actual size for placing characters is sizeof(buf) - 1 // but it takes extra time to parsing format snprintf ( buf , sizeof ( buf ) , something ) ; Bad things often happen when we mess with -1 or +1 to pointers point to continuous elements. Absorber # define LEN 128 char buf [ LEN + 1 ] = { 0 } ; // ^^^^ Always preserve 1 byte strncpy ( buf , somethin , sizeof ( buf ) ) ; Constant Correctness (C/C++) Bad void do_something ( char * dest , char * src ) ; struct MyList { void do_something ( std :: string & name ) ; } ; Reason : Will the name be modified by the lookup method? Will stat...