c++ - 在 CMake 中链接 CxxTest,它说它成功但无法构建

标签 c++ cmake cxxtest

我刚刚下载了 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

最佳答案

不确定这是否仍然是个问题,请查看 CMake 文档 CMake CxxTest 的文档。

从您的示例 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()

希望这对您有所帮助。

关于c++ - 在 CMake 中链接 CxxTest,它说它成功但无法构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43832065/

相关文章:

c++ - 文件存在时出现 Visual Studio 外部符号错误

unit-testing - CMake - 在构建过程中运行测试并将标准输出捕获到文件

c++ - 如何使用预定设置运行CMake?

c++ - 动态链接错误cmake

c++ - 具有大型 C 和 C++ 代码库的 Gtest

c++ - 操纵内部函数的返回值进行单元测试?

c++ - constexpr 的模板参数

c++ - 模板化重载运算符的显式调用

c++ - 如何直接在main函数中使用Locks