Code Blocks+gtest環境配置

本文僅介紹Code::Blocks+gtest環境配置,gtest具體使用方法請參考:
玩轉Google開源C++單元測試框架Google Test系列(gtest)(總)
http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.htmlhtml

環境概要

  • Code::Blocks 13.12
  • Windows 8.1
  • gtest-1.7.0

說明

gtest是一個優秀的開源C++單元測試框架,詳細介紹能夠參考官方網站。因爲某種緣由須要在Windows下使用Code::Blocks進行C++開發,特將配置過程進行記錄以備以後查閱。c++

下載安裝Code::Blocks,官方網址是http://www.codeblocks.org/,我下載的是codeblocks-13.12mingw-setup.exe版本,因爲Code::Blocks的安裝比較簡單,這裏我就很少說了,相信你們看此文章的目的不是查閱Code::Blocks的安裝說明。shell

下載gtest,官方網站是https://code.google.com/p/googletest/,我下的是gtest-1.7.0.zip版本,下載完成以後進行解壓,我解壓的目錄是E: gtest-1.7.0
下載安裝CMake,CMake是跨平臺的構建工具,官方網站是http://www.cmake.org,我下載的是cmake-3.2.1-win32-x86.exe,安裝以後運行cmake-gui,配置源代碼目錄(以前解壓的gtest目錄)和要build到的目標目錄。能夠參考以下截圖,但目錄設置要根據本身的環境進行配置。框架

單擊Configure,出現以下對話框,按照下圖選擇合適選項:工具

單擊Finish,會在目標目錄產生MinGW Makefiles,以後會出現下圖:單元測試

選中gtest_disable_pthreads一項,單擊Generate,即會產生Code::Blocks工程文件(.cbp)。
用Code::Blocks打開該文件,直接進行編譯,會在目標目錄產生兩個庫文件:測試

  • libgtest.a
  • libgtest_main.a

此時gtest的編譯工做就完成了,用Code::Blocks新建工程目錄gtest,在maim.h中寫入以下代碼:網站

#include<cstdio>
#include<gtest/gtest.h>
int add(int a, int b)
{
    return a+b;
}
TEST(addtest, HandleNoneZeroInput)
{
    EXPECT_EQ(14, add(4, 10));
    EXPECT_EQ(-2, add(-3, 1));
}
int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
    return 0;
}

將gtest源代碼目錄中的include中的文件拷貝到工程目錄中的include文件夾中,將以前build生成的兩個文件拷貝到工程目錄中的lib文件夾中。右鍵project,選擇Build options...,在Linker settings > Other linker options下填寫-lgtest。點擊選項卡Search directories,在Compiler子選項卡中Add一項,加入include目錄,在Linker子選項卡中Add一項,加入lib目錄。點擊OKui

此時build工程若是成功會出現如下結果:this

若是工程中想使用C++11的新特徵,得用命令行參數-std=gnu++11,而不是-std=c++11。具體作法是右鍵project,選擇Build options...,清除Compiler settings > Compile Flags下與-std=有關的複選框,點擊選項卡Compiler settings > Other options,在對話框中填寫-std=gnu++11。若是直接使用-std=c++11等選項會出現如下錯誤:

include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::StrCaseCmp(const char*, const char*)':
include/gtest/internal/gtest-port.h:1719:25: error: '_stricmp' was not declared in this scope
include/gtest/internal/gtest-port.h: In function 'char* testing::internal::posix::StrDup(const char*)':
include/gtest/internal/gtest-port.h:1721:58: error: '_strdup' was not declared in this scope
include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
include/gtest/internal/gtest-port.h:1729:52: error: '_fileno' was not declared in this scope
include/gtest/internal/gtest-port.h: In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':
include/gtest/internal/gtest-port.h:1779:71: error: 'fdopen' was not declared in this scope

另外,若是你的project的build option中已經清除Compiler settings > Compile Flags下與-std=有關的複選框,但仍是出現這個問題,可能緣由是你的global設置有問題,你能夠查看Settings->Complier選項卡是否也設置正確,若是沒有請正確設置。

相關文章
相關標籤/搜索