如何在Catch2中使用CMake? [英] How to use CMake with Catch2?

查看:452
本文介绍了如何在Catch2中使用CMake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Catch2的示例,我尝试了使用cmake运行此示例,其中我的项目结构如下:

From Catch2's example, I tried to run this example with cmake where structure of my project is like this:

/factorial
    +-- CMakeLists.txt
    +-- /bin
    +-- /include
    |     +-- catch.hpp
    |     +-- fact.hpp 
    +-- /src
    |     +-- CMakeLists.txt
    |    +-- fact.cpp
    +-- /test
         +-- CMakeLists.txt
         +-- test_fact.cpp

fact.cpp:

unsigned int factorial( unsigned int number ) {
    return number <= 1 ? number : factorial(number-1)*number;
}

fact.hpp:

#ifndef FACT_H
#define FACT_H

unsigned int factorial(unsigned int);

#endif

test_fact.cpp:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "fact.hpp"

TEST_CASE( "factorials are computed", "[factorial]" ) {
    REQUIRE( factorial(1) == 1 );
    REQUIRE( factorial(2) == 2 );
    REQUIRE( factorial(3) == 6 );
    REQUIRE( factorial(10) == 3628800 );
}

我已经尝试了几种方法来使用cmake构建该项目,但是失败了.有时我会出错:

I tried several ways already to build this project with cmake but it's failed. Sometimes I got an error:

cpp:X:XX: fatal error: 'fact.hpp' file not found
...

有时我得到:

Undefined symbols for architecture x86_64:
  "_main", referenced from:
...

当我运行make时.

如果要将执行文件放在factorial/bin中,应该在factorial/CMakeLists.txtfactorial/src/CMakeLists.txtfactorial/test/CMakeLists.txt中包含什么?

What should I have in factorial/CMakeLists.txt,factorial/src/CMakeLists.txt and factorial/test/CMakeLists.txt, if I want to have my execution files in factorial/bin?

其他: 这是我的CMakeLists.txts(我认为它们是完全错误的).

Additional: This is my CMakeLists.txts (I think they are completely wrong).

factorial/CMakeLists.txt:

project(factorial)
cmake_minimum_required(VERSION 2.8.12)

add_definitions("-std=c++11")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

add_subdirectory(src)
add_subdirectory(test)

factorial/src/CMakeLists.txt:

project(factorial)
cmake_minimum_required(VERSION 2.8.12)

add_executable(fact fact.cpp)

factorial/test/CMakeLists.txt:

project(factorial)
cmake_minimum_required(VERSION 2.8.12)

add_executable(test_fact test_fact.cpp)
target_include_directories(test_fact PRIVATE ${CMAKE_SOURCE_DIR}/include)

推荐答案

这里有很多问题:

add_executable(fact fact.cpp)

该调用应使用add_library(您也可以指定STATICSHARED),因为您仅定义阶乘函数,而不是具有main函数的可执行

The call should be using add_library (You could also specify STATIC or SHARED), since you are only defining a factorial function, not an executable with a main function.

add_executable(fact fact.cpp)

文件应为test_fact.cpp,并且目标应具有不同的名称,以避免与您创建的先前库冲突.另外,您的fact.cpp不包含fact.hpp.最后但并非最不重要的是,只需在顶层CMakeLists.txt中编写以下内容,而不是执行target_include_directories:

The file should be test_fact.cpp and the target should have a different name to avoid conflict with the previous library you created. Also, your fact.cpp doesn't include fact.hpp. Last but not least, instead of doing target_include_directories, just write the following in your top-level CMakeLists.txt:

include_directories(include)

现在,所有子目录都应该能够访问头文件.请注意,这将删除对头文件(PRIVATEPUBLICINTERFACE)的作用域的控制,并允许 all 子目录访问头文件.如果要限制此行为,请对所有目标(库和测试可执行文件)使用target_include_direcories.对于此示例,由于一切都需要访问头文件,因此上面的语句没有问题.

Now, all subdirectories should be able to access the header files. Beware that this removes control of the scoping of the header files (PRIVATE vs PUBLIC vs INTERFACE) and allows all subdirectories to access the header files. If you want to restrict this behavior, then use target_include_direcories for all targets (Your library and the test executable). For this example, since everything needs to access the header files, there is no problem with the statement above.

更多问题:

project(factorial)
cmake_minimum_required(VERSION 2.8.12)

要么切换这些语句的顺序,要么将它们都删除. (您仅在顶级CMake文件中需要它们)

Either switch the order of these statements, or remove both of them. (You only need them in your top level CMake file)

这篇关于如何在Catch2中使用CMake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆