2016-07-18 36 views
2

私は通常プロジェクト用のmakefileを使用しますが、私はCMakeを学び始めたいと思います。 私はプロジェクトをビルドするだけでなく、プロジェクトをテストするためにmakefileを使用します。 非常に便利です。 CMakeでどうすればいいですか? exempleについてはCMakeを追加してテストプログラムコマンドを追加

このメイクファイル:

pathword=words.txt 
flags=-std=c++11 -Wall -Wextra -g -Og 
#flags=-std=c++11 -O3 -DNDEBUG -s 

default: TextMiningCompiler TextMiningApp 

TextMiningCompiler: TextMiningCompiler.cpp trie.cpp 
    g++ $(flags) TextMiningCompiler.cpp trie.cpp -o TextMiningCompiler 

TextMiningApp: TextMiningApp.cpp 
    g++ $(flags) TextMiningApp.cpp -o TextMiningApp 

run: TextMiningCompiler TextMiningApp 
    ./TextMiningCompiler $(pathword) totoro.txt 
    cat test.txt | time ./TextMiningApp totoro.txt 

clean: 
    trash TextMiningCompiler TextMiningApp 

私はこのCMakefileを作った:

cmake_minimum_required(VERSION 2.8.9) 
project (TextMining) 
add_executable(TextMiningApp TextMiningApp.cpp) 
add_executable(TextMiningCompiler TextMiningCompiler.cpp trie.cpp read_words_file.cpp) 
set_property(TARGET TextMiningApp PROPERTY CXX_STANDARD 11) 
set_property(TARGET TextMiningCompiler PROPERTY CXX_STANDARD 11) 

は、どのように私はメイクの実行機能を持つことができますか?または他のカスタム関数?

答えて

2

コマンドadd_custom_target

は、与えられたコマンドを実行し、指定された名前のターゲットを追加します。

使い方は簡単です:

add_custom_target(run 
    COMMAND ./TextMiningCompiler <pathword> totoro.txt 
    COMMAND cat test.txt | time ./TextMiningApp totoro.txt 
) 
add_dependencies(run TextMiningCompiler TextMiningApp) 
+0

多分ちょうど/ペーストをコピーし、それは 'add_custom_target()'の代わりに 'add_custom_command(のではありません) 'のコード例でも同様ですか? – Florian

+0

@Florian:はい、それは 'add_custom_target'でなければなりません。 – Tsyvarev

6

それはCMakeの中での試験を取得する私はadd_test()を使用することを好みます。それは、テストを実行するためにmake testのようなものを呼び出すことのほかに、 (CMakeと一緒に配布)経由でテストレポートを入手してください。 add_test()で「コマンド」として、実行可能ファイルのcmakeのターゲットの名前を使用して

直接excutableのパスに置き換え:

cmake_minimum_required(VERSION 2.8.9) 
project (TextMining) 

enable_testing() 

set(CMAKE_CXX_STANDARD 11) 
set(pathword "${CMAKE_SOURCE_DIR}/words.txt") 

add_executable(TextMiningCompiler TextMiningCompiler.cpp trie.cpp read_words_file.cpp) 
add_test(
    NAME TestTextMiningCompiler 
    COMMAND TextMiningCompiler "${pathword}" "totoro.txt" 
) 

add_executable(TextMiningApp TextMiningApp.cpp) 
add_test(
    NAME TestTextMiningApp 
    COMMAND sh -c "cat ${CMAKE_SOURCE_DIR}/test.txt | time $<TARGET_FILE:TextMiningApp> totoro.txt" 
) 
set_tests_properties(TestTextMiningApp PROPERTIES DEPENDS TestTextMiningCompiler) 

ご希望場合は、さらにshのようなシェルに依存性を排除することができます入力としてtest.txtを渡すTextMiningAppにコマンドラインパラメータを追加します。

add_test(
    NAME TestTextMiningApp 
    COMMAND TextMiningApp -i "${CMAKE_SOURCE_DIR}/test.txt" "totoro.txt" 
) 

make test(btw)でテストを実行すると、合計実行時間が自動的に測定されるため、timeコールを追加する必要はありません。 )ctestを呼び出すことに相当:

$ make test 
Running tests... 
Test project [... path to project's binary dir ...] 
    Start 1: TestTextMiningCompiler 
1/2 Test #1: TestTextMiningCompiler ........... Passed 0.11 sec 
    Start 2: TestTextMiningApp 
2/2 Test #2: TestTextMiningApp ................ Passed 0.05 sec 

100% tests passed, 0 tests failed out of 2 

Total Test time (real) = 0.19 sec 

リファレンス

関連する問題