我刚刚下载了cxxtest的4.4版,并将zip文件解压缩到以下路径:C:/cxxtest-4.4。
现在,紧接着,无需执行任何其他步骤,我打开了CLion,并尝试将CxxTest添加到我的项目中,如下所示:
set(CXXTEST_PYTHON_TESTGEN_EXECUTABLE C:/cxxtest-4.4/python/cxxtest/cxxtestgen.py)
find_package(CxxTest REQUIRED)
if(CXXTEST_FOUND)
set(SOURCE_FILES main.cpp Calculator.h TestCalculator.h Calculator.cpp TestCalculator.h)
add_executable(Calculator ${SOURCE_FILES})
endif()
我必须手动设置两个变量原因,如果没有得到以下错误:
"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Admin\CLionProjects\Calculator
CMake Error at C:/Program Files (x86)/JetBrains/CLion 2017.1/bin/cmake/share/cmake-3.7/Modules/FindPackageHandleStandardArgs.cmake:138 (message):
Could NOT find CxxTest (missing: CXXTEST_PYTHON_TESTGEN_EXECUTABLE)
Call Stack (most recent call first):
C:/Program Files (x86)/JetBrains/CLion 2017.1/bin/cmake/share/cmake- 3.7/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files (x86)/JetBrains/CLion 2017.1/bin/cmake/share/cmake- 3.7/Modules/FindCxxTest.cmake:221 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:9 (find_package)
-- Configuring incomplete, errors occurred!
See also "C:/Users/Admin/CLionProjects/Calculator/cmake-build- debug/CMakeFiles/CMakeOutput.log".
现在一切似乎都可以正常工作,这是我的Cmake调试:
"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" - DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Admin\CLionProjects\Calculator
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/Admin/CLionProjects/Calculator/cmake-build-debug
现在,当我尝试在头文件中使用cxxtest / TestSuite.h时,它说找不到它。请让我发疯,请帮我!
提前致谢!
编辑:我设法链接它,现在我可以找到头文件,并将以下行添加到CMake文件:
include_directories(C:/cxxtest-4.4)
现在,当我构建时,得到以下输出:
"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" -- build C:\Users\Admin\CLionProjects\Calculator\cmake-build-debug --target Calculator -- -j 8
[ 33%] Linking CXX executable Calculator.exe
CMakeFiles\Calculator.dir/objects.a(main.cpp.obj): In function ` ZN7CxxTest7trackerEv':
C:/cxxtest-4.4/cxxtest/TestTracker.h:130: undefined reference to `CxxTest::TestTracker::tracker()'
CMakeFiles\Calculator.dir/objects.a(main.cpp.obj): In function `ZN7CxxTest14numberToStringIlEEPcT_S1_S2_jj':
C:/cxxtest-4.4/cxxtest/ValueTraits.h:183: undefined reference to `CxxTest::digitToChar(unsigned int)'
CMakeFiles\Calculator.dir/objects.a(main.cpp.obj): In function `ZN7CxxTest14doAssertEqualsIiiEEvPKciS2_T_S2_T0_S2_':
C:/cxxtest-4.4/cxxtest/TestSuite.h:146: undefined reference to `CxxTest::doAbortTest()'
CMakeFiles\Calculator.dir/objects.a(main.cpp.obj):main.cpp:(.rdata$_ZTV14TestCalculator[__ZTV14TestCalculator]+0x14): undefined reference to `CxxTest::TestSuite::tearDown()'
CMakeFiles\Calculator.dir/objects.a(main.cpp.obj): In function `ZN14TestCalculatorD1Ev':
C:/Users/Admin/CLionProjects/Calculator/TestCalculator.h:8: undefined reference to `CxxTest::TestSuite::~TestSuite()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\Calculator.dir\build.make:123: recipe for target 'Calculator.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Calculator.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Calculator.dir/rule' failed
mingw32-make.exe[3]: *** [Calculator.exe] Error 1
Makefile:117: recipe for target 'Calculator' failed
mingw32-make.exe[2]: *** [CMakeFiles/Calculator.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Calculator.dir/rule] Error 2
mingw32-make.exe: *** [Calculator] Error 2
最佳答案
不知道这是否仍然是一个问题,请查看CxxTest的CMake文档CMake文档。
从您的示例CMakeLists文件中,您似乎对常规项目文件进行了剪切。您不得包含main.cpp文件CxxTest根据您的测试定义文件生成一个名为Runner.cpp的文件。
因此,CMakeFiles.txt中的CxxTest部分应如下所示:
set(CMAKE_PREFIX_PATH ${CMAKE_SOURCE_DIR}/deps/cxxtest/bin)
find_package(CxxTest REQUIRED)
if(CXXTEST_FOUND)
include_directories(${CXXTEST_INCLUDE_DIR})
enable_testing()
CXXTEST_ADD_TEST(tests
runner.cpp # THIS IS GENERATED BUT YOU NEED TO ADD IT HERE
c:/libraries/boost_1_63_0/boost/filesystem.hpp # THIS SHOULD NOT BE HARD CODED
${CMAKE_SOURCE_DIR}/source/string_tokenizer.cpp # YOUR PROJECT FILES THAT WILL BE USED FOR YOUR TEST
${CMAKE_CURRENT_SOURCE_DIR}/tests/test_stringtokenizer.h # YOUR TEST DESCRIPTION FILE
)
target_link_libraries(tests ${Boost_LIBRARIES}) # LINK BOOST / OTHER THIRD PARTY LIBS TO YOUR TEST
endif()
希望这可以帮助。