仅CMake标头依赖

时间:2019-01-15 04:48:48

标签: c++ build cmake header-files catch-unit-test

使用CMake在catch2上运行简单测试时遇到麻烦。由于catch只不过是标头,所以我觉得它很容易放入任何项目中,因此我认为我应该像普通的标头文件一样包含它。

项目结构:

-build
-external
    -Catch2
        catch2.hpp
 CMakeLists.txt
 tester.cpp

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(Test VERSION 1.0.0)

include_directories(external)

enable_testing()
add_executable(tester tester.cpp)
add_test(Tester tester)

tester.cpp:

#define CATCH_CONFIG_MAIN
#include "Catch2\catch.hpp"

TEST_CASE( "1 is 1" ) {
    REQUIRE( 1 == 1 );
}

输出:

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.03 sec

The following tests FAILED:
          1 - Tester (Exit code 0xc0000139
)
Errors while running CTest

显然,测试应该通过,但是没有通过。因为我是CMake和catch2领域的初学者,所以我很难找出问题所在。我可以肯定地说的是找到了catch.hpp并且没有链接器错误,它只是返回了一些错误代码。

我环顾四周,发现了这一点

CMake Header from library not found when implementing in catch test case

但是它没有答案,而且作者似乎也没有同样的问题。

这是我构建和运行测试的方法(位于构建目录中):

cmake .. -G "MinGW Makefiles" && mingw32-make && ctest

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

好的,我删除了catch并只是与mingw一起玩,显然我只是通过使用gcc -std=c11 -g -Wall -pedantic init.c -o init init.c: In function ‘main’: init.c:55:14: warning: implicit declaration of function ‘fdopen’; did you mean ‘fopen’? [-Wimplicit-function-declaration] if ((file = fdopen(pipeToParent[0], "r")) == NULL) ^~~~~~ fopen init.c:55:12: warning: assignment makes pointer from integer without a cast [-Wint-conversion] if ((file = fdopen(pipeToParent[0], "r")) == NULL) ^ 而得到了相同的错误。有人说这与丢失的DLL文件有关。我在可执行文件上运行了依赖行者,确实丢失了一堆DLL。我现在不知道该怎么做或从哪里得到这些,所以我放弃了mingw并尝试使用Cygwin方法。

但是将cmake与cygwin一起使用时,我没有发现适用于我的开发环境(Windows)的任何兼容生成器。

然后我转而生成一个Visual Studio项目(我从一开始就避免这样做,因为我不想在IDE中进行开发)。但是我发现我可以使用msbuild从生成的Visual Studio项目中构建可执行文件,并且它的工作原理就像一个带有catch的超级按钮。