kutschkem
kutschkem

Reputation: 8163

Cmake does not build static library on Windows

I have an odd problem where CMake on Windows run without trouble but then the compiler does not actually create the library

project(core)

find_package(spdlog REQUIRED)

set(core_gen_hdr "include/chrono.h" "include/monitoring.h" "${SPDLOG_HEADER_FILES}")

add_library(${PROJECT_NAME} STATIC "${core_gen_hdr}")

target_include_directories (${PROJECT_NAME} PUBLIC "include/" "${SPDLOG_INCLUDE_DIRS}")

source_group("Header Files" FILES "${core_gen_hdr}")

set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER ${MODULE_NAME})
set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX)

I wonder if this is because the only source files are header files? When compiling, no library gets created, which creates problem with dependent libraries that want to link against the non-existing file.

CMake version is 3.5.0 and I build with Visual Studio 2015.

Upvotes: 1

Views: 864

Answers (1)

Steve Lorimer
Steve Lorimer

Reputation: 28689

You need to create an interface library

In fact, spdlog is also header-only, and does just this (see here)

For your project, you just need to add your project's include directories, and then link to spdlog and cmake will sort out the transitive dependencies

# create your library, specifying it is an interface-library
add_library(${PROJECT_NAME} INTERFACE)

# add your project's directories
target_include_directories(
    ${PROJECT_NAME}
    INTERFACE
    ${CMAKE_CURRENT_SOURCE_DIR}/include)

# link against spdlog, which is also just an interface-library
target_link_libraries(${PROJECT_NAME} spdlog)

Upvotes: 0

Related Questions