user8469759
user8469759

Reputation: 2722

Using CMake with templates

I've a small git project for basic algorithms and data structure, and specifically I'm planning to change this bit with templates, at the moment everything is with integers. The CMakeList.txt is this:

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ElementaryDataStructures)

set( CMAKE_CXX_STANDARD 11 )

include_directories(./)

set( HEADER_FILES list.h bstree.h rbtree.h)
set( SRC_FILES list.cpp bstree.cpp rbtree.cpp)

if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")
endif()

add_library(elementary-data-structures SHARED ${SRC_FILES} ${HEADER_FILES})
set_target_properties(elementary-data-structures PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
#set_target_properties(elemetary-data-structures PROPERTIES LINKER_LANGUAGE CXX)

While the main CMakeLists.txt is

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(CPP_Algorithms_and_Data_Structures)

set( CMAKE_CXX_STANDARD 11 )

add_subdirectory(./ElementaryAlgorithms)
add_subdirectory(./ElementaryDataStructures)
add_subdirectory(./AdvancedDataStructures)
add_subdirectory(./GraphAlgorithms)

set(INCLUDE_FOLDERS 
    ./ElementaryAlgorithms 
    ./ElementaryDataStructures 
    ./AdvancedDataStructures 
    ./GraphAlgorithms)

include_directories(${INCLUDE_FOLDERS})

set(SRC_FILES main.cpp)

add_executable(alg-and-ds ${SRC_FILES})
target_link_libraries(alg-and-ds 
    elementary-algorithms
    elementary-data-structures
    advanced-data-structures)

So basically I'd like to replace, for example, the definition of List in list.h with a template class and instead of having list.cpp I'd like to have a list.tpp for the implementation. How should my CMakeLists.txts be changed to support template?

Is it just including folders? or is there something subtle I should keep in mind?

Update

I did change one of my CMakeLists.txt as follows:

cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(ElementaryDataStructures)

set( CMAKE_CXX_STANDARD 11 )

if(WIN32)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")
endif()

add_library(elementary-data-structures SHARED "")
target_include_directories(elementary-data-structures PUBLIC ./)
target_sources(elementary-data-structures PUBLIC 
    "${CMAKE_CURRENT_LIST_DIR}/list.h"
    "${CMAKE_CURRENT_LIST_DIR}/list.tcc")
set_target_properties(elementary-data-structures PROPERTIES     RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

And I'm getting this error:

-- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.17134.
-- Configuring done
CMake Error: CMake can not determine linker language for target: elementary-data                    -structures
-- Generating done
-- Build files have been written to: C:/Users/Lukkio/Project/CPP-AlgAndDS/build

What am I doing wrong?

Upvotes: 0

Views: 5142

Answers (1)

Unapiedra
Unapiedra

Reputation: 16197

How should my CMakeLists.txts be changed to support template?

Is it just including folders? or is there something subtle I should keep in mind?

By going to templates you are also moving to a header-only library. Once you have replaced all the implementations, you will be left without .cpp files.

You don't need to do anything special. However, googling for "CMake Header-only library", you see the recommendation to mark your lib as "INTERFACE". 1, 2

  • target_sources can be used to tell CMake the input files. Alternatively, you can implicitly export them through target_include_directories. However, that way they would not get installed, if you create a package (debian, etc) out of the library.

Recommendation

  • Get rid of the directory-scoped include_directories by replacing it with target_include_directories(elementary-data-structures PUBLIC .). include_directories is old-style CMake and should be avoided.

Upvotes: 1

Related Questions