How to write the better Makefile for cpputest


最近給其他幾個團隊培訓UT工具cpputest。自己沒有寫PPT,而是從網上找了一份比較不錯的CppUTest課件,這份課件來自於一家叫Odd-e的敏捷諮詢公司。

裏面的內容比較基礎,也很清晰,非常適合幫助初學者較快的入門。

由於我之前一直是在Windows下用VS使用這款UT工具,Linux下的實踐僅僅限於在學習cpputest源碼中的過程中,利用工具附帶的example工程上增加修改一些單元測試。

當有同學按照PPT上的簡單實例,在Linux下嘗試時,發現PPT上的Makefile根本就編譯不過。我嘗試之後確實。

其實原因也很清楚,因爲編譯需要包含的cpputest的引用關係比較複雜,並不是在PPT中那麼簡單,下面貼出PPT中的Quick Start代碼:

單元測試代碼如下:










按照上述的Makefile是不行的,如果自己編寫一個可行的Makefile對於一個Makefile功力不夠的初學者是很難的。不過,幸好cpputest工具的發佈者隨代碼提供了一份可用的Makefile。

如果你使用這個工具之前先研究過附帶的examples例子的話,會發現這個例子的Makefile是引用了build目錄下的MakefileWorker.mk來完成整個編譯、鏈接和執行的。

下面貼出這份MakefileWorker的開頭註釋部分:

# Include this helper file in your makefile
# It makes
#    A static library
#    A test executable
#
# See this example for parameter settings 
#    examples/Makefile
#
#----------
# Inputs - these variables describe what to build
#
#   INCLUDE_DIRS - Directories used to search for include files.
#                   This generates a -I for each directory
# SRC_DIRS - Directories containing source file to built into the library
#   SRC_FILES - Specific source files to build into library. Helpful when not all code 
# in a directory can be built for test (hopefully a temporary situation)
# TEST_SRC_DIRS - Directories containing unit test code build into the unit test runner
# These do not go in a library. They are explicitly included in the test runner
# TEST_SRC_FILES - Specific source files to build into the unit test runner
# These do not go in a library. They are explicitly included in the test runner
# MOCKS_SRC_DIRS - Directories containing mock source files to build into the test runner
# These do not go in a library. They are explicitly included in the test runner
#----------


MakefileWorker.mk對外提供了很好的接口,只要你自己編寫一個簡單的Makefile把需要傳入的參數定義並傳入就OK了。


同樣是上面的那個例子,重新編寫Makefile如下:
我的UT代碼所在的文件夾Ut_Test和cpputest的根目錄同級。UT代碼同PPT中的代碼。

#--- Inputs ----#
COMPONENT_NAME = Ut_Test
CPPUTEST_HOME = ../cpputest-3.4

CPPUTEST_USE_EXTENSIONS = Y
CPP_PLATFORM = Gcc

SRC_DIRS = 

TEST_SRC_DIRS = .

INCLUDE_DIRS =\
  .\
  $(CPPUTEST_HOME)/include\

include $(CPPUTEST_HOME)/build/MakefileWorker.mk

運行結果:




如果你不希望看到如此多的編譯細節,可以在Makefile中增加以下定義:
#Set this to @ to keep the makefile quiet
ifndef SILENCE
SILENCE = @
endif

運行結果:



至此,相信你應該可以很easy的編寫cpputest的Makefile了吧!






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章