《C++程序設計實踐與技巧:測試驅動開發》 環境搭建遇到的坑
在環境搭建時,遇到了一些資源的版本已經更新的問題,主要是 Google Mock,所以整理了搭建環境時與書本不同的修改。我使用的環境是 Ubuntu 16。
Google Mock
Google Mock 項目已經切換到 Github,網址爲:https://github.com/google/googletest。
安裝過程
下載下來後文件解壓路徑到: ~/Desktop/projects/googletest-release-1.8.0
cd ~/Desktop/projects/googletest-release-1.8.0
mkdir mybuild && cd mybuild
cmake ..
make
書的源碼中需要用到 GoogleTest 的頭文件和庫,進入源碼的code/c2/ 目錄,CMakeLists.txt 的內容爲:
project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6)
include_directories($ENV{GMOCK_HOME}/include $ENV{GMOCK_HOME}/gtest/include)
link_directories($ENV{GMOCK_HOME}/mybuild $ENV{GMOCK_HOME}/gtest/mybuild)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")
set(sources
main.cpp
SoundexTest.cpp)
add_executable(test ${sources})
target_link_libraries(test pthread)
target_link_libraries(test gmock)
target_link_libraries(test gtest)
由於 mybuild 路徑與 CMakeLists.txt 裏的需求不同,於是需要對編譯出來的 GoogleTest 重定向。執行如下。
cd ~/Desktop/projects/
mkdir googlemock && cd googlemock
ln -sf ~/Desktop/projects/googletest-release-1.8.0/googlemock/include/ include
ln -sf ~/Desktop/projects/googletest-release-1.8.0/mybuild/googlemock/ mybuild
mkdir gtest && cd gtest
ln -sf ~/Desktop/projects/googletest/include/ include
ln -sf ~/Desktop/project/googletest-release-1.8.0/mybuild/googlemock/gtest/ mybuild
libcurl
安裝 libcul 的時候可能會出現未找到 OPENSSL 的提示,可以執行如下操作。
sudo apt-get install libssl-dev
執行示例代碼
由於代碼裏提供的示例很多不全,暫時發現 code/c2/40 裏的項目可行的,進入到 code/c2/40 執行如下操作,就能看到結果。
mkdir build && cd build
cmake ..
make
./test