Produce .lib of header-only library that depends on external resources

时间:2018-12-03 13:19:36

标签: cmake subdirectory

EDIT: I've read up and understood the initial issue was caused by scanning-header-only not having cpp files and thus a lib file not being generated. Edited the question to reflect that extra understanding:

My current project folder structure and relevant CMakeLists content:

leveling
├── CMakeLists.txt: add_subdirectory(deps) 
└── deps
    ├── CMakeLists.txt: add_subdirectory(scanning-header-only)
    └── scanning
        ├── CMakeLists.txt: add_subdirectory(deps)
        │                   add_library(scanning-header-only file.h)
        │                   target_include_directories(scanning-header-only PUBLIC ${CMAKE_CURRENT_LIST_DIR}/deps/tinyxml2)
        │                   target_link_libraries(scanning-header-only PUBLIC tinyxml2)
        └── deps
            ├── CMakeLists.txt: add_subdirectory(tinyxml2)
            └── tinyxml2

But a scanning-header-only library file is not being generated, and thus the root project can't target_link_libraries(leveling scanning-header-only) and has had to target_include_directories(leveling ${CMAKE_CURRENT_LIST_DIR}/deps/scanning-header-only/deps/tinyxml2)

Is it possible to target_link_library a header-only library that depends on external resources?

I see that a header-only library without external resource dependency could be add_library(.. INTERFACE), but I'm failing to do so with the dependency on tinyxml2

A dirty workaround is adding and empty cpp file to scanning-header-only so a lib file is generated, but is there a correct way to do this?



Here is minimal example v1: https://www.dropbox.com/s/r1lbajz3xoat1bg/leveling-header-only-test%20v1.zip?dl=0

leveling CMakeLists.txt: cmake_minimum_required(VERSION 3.8)

set(LEVELING_NAME leveling)

project(${LEVELING_NAME})

#
# To put tinyxml.dll next to the executable, to workaround having to make tinyxml2.dll reachable in PATH
#
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8")
set(platform_dir bin/${CMAKE_SYSTEM_NAME}-${platform_bits})

foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
    foreach(var
            CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config}
            CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config}
            CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config}
            )
        set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}/${config}")
        string(TOLOWER "${${var}}" ${var})
    endforeach()
endforeach()
#
# ----------------------------------------------------------------------
#

add_subdirectory(deps)

add_executable(${LEVELING_NAME} main.cpp)

target_include_directories(${LEVELING_NAME} PUBLIC
    ${CMAKE_CURRENT_LIST_DIR}/deps/scanning 
)

target_link_libraries(${LEVELING_NAME} 
    xml-reading
)

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${LEVELING_NAME}) # Set Startup Project in VS. Implemented in CMake v3.6
set_target_properties(${LEVELING_NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") # Set Working Directory of project in VS. Implemented in CMake v3.8

scanning CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

set(XML_NAME xml-reading)

project(${XML_NAME})

#
# To put tinyxml.dll next to the executable, to workaround having to make tinyxml2.dll reachable in PATH
#
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

math(EXPR platform_bits "${CMAKE_SIZEOF_VOID_P} * 8")
set(platform_dir bin/${CMAKE_SYSTEM_NAME}-${platform_bits})

foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
    foreach(var
            CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${config}
            CMAKE_LIBRARY_OUTPUT_DIRECTORY_${config}
            CMAKE_RUNTIME_OUTPUT_DIRECTORY_${config}
            )
        set(${var} "${CMAKE_BINARY_DIR}/${platform_dir}/${config}")
        string(TOLOWER "${${var}}" ${var})
    endforeach()
endforeach()
#
# ----------------------------------------------------------------------
#

add_subdirectory(deps)

add_library(${XML_NAME} INTERFACE CamerasXML.h)
target_include_directories(${XML_NAME} INTERFACE
    ${CMAKE_CURRENT_LIST_DIR}/deps/tinyxml2
)
target_link_libraries(${XML_NAME} 
    INTERFACE tinyxml2
)

which yields

CMake Error at deps/scanning/CMakeLists.txt:33 (add_library): 
  add_library INTERFACE library requires no source arguments. 

2 个答案:

答案 0 :(得分:0)

.lib是在Windows上创建STATIC(.lib)或SHARED(.lib.dll)库时的状态。您需要的是一个INTERFACE库,它不生成任何文件。 http://mariobadr.com/creating-a-header-only-library-with-cmake.html有一个例子。然后,您可以使用此处列出的以下命令https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries来填充接口。请注意,它使用的是INTERFACE而不是PUBLIC。

target_link_libraries(INTERFACE),
target_link_options(INTERFACE),
target_include_directories(INTERFACE),
target_compile_options(INTERFACE),
target_compile_definitions(INTERFACE), and
target_sources(INTERFACE),

我从没真正使用过它,但是我认为它可以按文档所述工作。

答案 1 :(得分:0)

一个简单的add_library(${XML_NAME} INTERFACE)(不指定任何源文件),同时具有target_include_directories(${XML_NAME} INTERFACE ${CMAKE_CURRENT_LIST_DIR}/deps/tinyxml2)target_link_libraries(${XML_NAME} INTERFACE tinyxml2)可以解决问题。

tinyxml2包含项可用于父项目,并且tinyxml2库链接在父项目中。