0

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?

Unapiedra
  • 15,037
  • 12
  • 64
  • 93
user8469759
  • 2,522
  • 6
  • 26
  • 50
  • Please consider using `target_include_directories` instead of the global `include_directories`. – Carl Aug 26 '18 at 22:59

1 Answers1

1

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.
Community
  • 1
  • 1
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
  • Not my best answer. Anyway, I think you are more or less fine by moving to an INTERFACE lib. – Unapiedra Aug 26 '18 at 22:48
  • Can you please post the full cmakelists.txt? Including your suggestions, just for future references. – user8469759 Aug 26 '18 at 23:25
  • Deleted my answer. Probably more informed answers can be had – natersoz Aug 27 '18 at 05:02
  • Also, despite I'm going through this : https://stackoverflow.com/questions/26243169/cmake-target-include-directories-meaning-of-scope, I still don't get the meaning of `INTERFACE, PUBLIC, PRIVATE`. – user8469759 Aug 27 '18 at 09:56