简体   繁体   English

如何使用cmake正确配置boost测试

[英]How to properly configure boost test with cmake

Boost.Test is a very nice unit-testing library, but allways when I try to configure it in a new project it is a major pain. Boost.Test是一个非常好的单元测试库,但是当我尝试在新项目中配置它时,这是一个很大的痛苦。

How to configure my project, that uses cmake, to use boost with following requirements (this is really a list of things I disliked in most of the recipies I found on the internet): 如何配置我的项目,使用cmake,使用具有以下要求的提升(这实际上是我在互联网上找到的大多数收件人不喜欢的事项列表):

  • I don't want to use single header variant of UTF (that is I don't want to include boost/test/included/unit_test.hpp ). 我不想使用UTF的单头变体(也就是说我不想包含boost/test/included/unit_test.hpp )。 Rationale for this is that it slows down both compilation and IDE. 这样做的理由是它会降低编译和IDE的速度。
  • I don't really want to write a main function -- unless it is a one liner (I didn't find any copy-paste ways to define a main function). 我真的不想写一个main函数 - 除非它是一个单行(我没有找到任何复制粘贴方式来定义主函数)。 So unless you can provide such main snippet this means that Boost.Test is linked statically. 所以除非你能提供这样的主要片段,否则这意味着Boost.Test是静态链接的。
  • I don't want to include everyting statically (via -static gcc switch). 我不想静态地包括每一个(通过-static gcc开关)。 I also dont really want to compile every boost component statically. 我也不想静态编译每个boost组件。
  • I don't want to hardcode any library paths in my cmake config. 我不想在我的cmake配置中硬编码任何库路径。

So here is test.cpp I want to use: 所以这里是test.cpp我想使用:

#define BOOST_TEST_MODULE ExampleTest
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(ExampleSuite)

BOOST_AUTO_TEST_CASE( my_test )
{
    BOOST_CHECK(true);
}

BOOST_AUTO_TEST_SUITE_END()

And here is simple (not working!) CMakeLists.txt : 这里很简单(不工作!) CMakeLists.txt

cmake_minimum_required(VERSION 2.8.11)
project(example)
add_executable(simple-test tests.cpp)

Actually I didn't found any concrete solution that fulfills my need, but here are two partial solutions that are mostly OK. 实际上我没有找到满足我需要的任何具体解决方案,但这里有两个部分解决方案,大部分都可以。

Explicitly link statically to libboost_unit_test_framework.a : 明确地与libboost_unit_test_framework.a链接:

In this case CMakeLists.txt looks like that: 在这种情况下, CMakeLists.txt看起来像这样:

cmake_minimum_required(VERSION 2.8.11)
project(example)
ADD_LIBRARY(boost_unit_test_framework STATIC IMPORTED)
SET_TARGET_PROPERTIES(boost_unit_test_framework PROPERTIES
    IMPORTED_LOCATION /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.a)
add_executable(simple-test tests.cpp)
target_link_libraries(simple-test boost_unit_test_framework)

Drawbacks are that I explicitly link to a /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.a which can change (I guess). 缺点是我明确地链接到/usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.a ,它可以改变(我猜)。

Use find_packages 使用find_packages

cmake_minimum_required(VERSION 2.8.11)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(simple-test tests.cpp)
target_link_libraries(simple-test ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})

Drawbaks for this is that (as far as I understand set(Boost_USE_STATIC_LIBS ON) forces all boost components to be linked statically, which might be undesirable. 对此的Drawbaks是(据我所知set(Boost_USE_STATIC_LIBS ON)强制所有boost组件静态链接,这可能是不合需要的。

You can build your own shared version of boost UTF in your project (static would also work). 您可以在项目中构建自己的boost UTF共享版本(静态也可以)。

add_library(my_boost_test SHARED boost_test.cpp)

With this boost_test.cpp : 使用此boost_test.cpp

#include <boost/test/included/unit_test.hpp>

I use this: 我用这个:

cmake_minimum_required(VERSION 2.8.11)

# disable auto link
add_definitions(-DBOOST_ALL_NO_LIB)

# link against dynamic libraries
add_definitions(-DBOOST_ALL_DYN_LINK)
find_package(Boost COMPONENTS system unit_test_framework)

as a preamble before loading the boost.test library, and it works as expected. 作为加载boost.test库之前的序言,它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM