CMake测试库,标题位置问题 [英] CMake testing a library, header location issue

查看:63
本文介绍了CMake测试库,标题位置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在将一个项目作为一个库的一部分,该库具有一些作为库接口的标头,其余标头则是库本身的私有库。因此对于我的库,CMAKE部分如下所示:

So, I'm making part of a project a library with some headers that are the interface to the library, and the remaining are private to the library itself. So for my library the CMAKE part looks like:

add_library(${PROJECT_NAME} ${PROJECT_SOURCES} "${PROJECT_BINARY_DIR}/libversion.h")
add_library(my::lib ALIAS ${PROJECT_NAME})

target_include_directories(${PROJECT_NAME} 
    PRIVATE ${Boost_INCLUDE_DIRS}
    PRIVATE ${PROJECT_BINARY_DIR} #to locate libversion.h
    INTERFACE ${PUBLIC_INCLUDE_HEADERS}
    )

然后是我的测试目标:

add_executable(${TEST_NAME} ${TEST_SOURCES})
add_test(NAME LibTest COMMAND ${TEST_NAME})

target_link_libraries(${TEST_NAME} 
    PRIVATE ${Boost_LIBRARIES}
    PRIVATE my::lib
    )

但这仅允许我测试公共接口。如果要对库进行单元测试,该如何声明对项目 lib 中其余标头的访问?我的看法是添加一个全新的目标 my :: lib :: testing ,该目标将接口声明为当前源目录(当前所有标头都位于该目录中,将公共标题与私有标题分开是我尚未解决的另一个问题)。像这样:

But this only allows me to test my public interface. If I want to unit test my library, how would I go about declaring the access to the remaining headers in project lib? The way I see it would be to add an entire new target my::lib::testing which declares the interface as the current source directory (where all headers currently are located, seperating public from private headers is another issue I've yet to handle). So something like this:

add_library(${PROJECT_NAME}_TESTING ${PROJECT_SOURCES} "${PROJECT_BINARY_DIR}/libversion.h")
add_library(my::lib::testing ALIAS ${PROJECT_NAME}_TESTING)

target_include_directories(${PROJECT_NAME}_TESTING
    PRIVATE ${Boost_INCLUDE_DIRS}
    PRIVATE ${PROJECT_BINARY_DIR} #to locate libversion.h
    INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
    )

但这需要根据用途来构建两个不同的目标。一个用于我的应用程序,链接到别名 my :: lib ,另一个用于单元测试,链接别名 my :: lib :: testing

But this requires two different targets to be build depending on the usage. One for my application linking to alias my::lib and one for unit testing, linking alias my::lib::testing.

所以我的问题是,如何干净单独的标头,这样我只能拥有 INTERFACE 标头显示的目标,但仍按我的测试目标访问其余标头吗?

So my question is, how do I cleanly separate headers so I can have only my INTERFACE headers shown by targets, but still access the remaining headers by my test target?

推荐答案

PRIVATE和PUBLIC项均填充目标的INCLUDE_DIRECTORIES属性,因此您可以尝试在目标项目的target_include_directories中使用它。

Both PRIVATE and PUBLIC items populate the INCLUDE_DIRECTORIES property of an target, so you can try to use it in target_include_directories for the test project.

add_executable(${TEST_NAME} ${TEST_SOURCES})
add_test(NAME LibTest COMMAND ${TEST_NAME})

target_link_libraries(${TEST_NAME} 
    PRIVATE ${Boost_LIBRARIES}
    PRIVATE my::lib
    )

target_include_directories( ${TEST_NAME} PRIVATE $<TARGET_PROPERTY:my::lib,INCLUDE_DIRECTORIES>)

这篇关于CMake测试库,标题位置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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