3

I compiled all the information and now iam trying to implement CMake build system for a super project and i really need a guidance here,it would be great to transform all data i acquired by reading about CMake across different sites to practical knowledge

i have a fundamental questions regarding understating CMake Targets

In case of multi directories structure,i want to achieve most portability thus having target for each directory and then use these different targets as link for others in another directories, my goal is to be agile as possible and not be coupled with a certain directory structure, make use of CMake to figure the dependency, where i would be only concerned about targets

my final target is a library of libraries

USE CASE :

First level directory {PROJECT} ( 2 folders ) :
CMakeLists.txt common_env source ​ ​First level CMakeLists.txt :

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(dassys)

# iam using this because target scope change with sub-directory
include(common/CMakeLists.txt) 
include(src/CMakeLists.txt)

#Add Library
add_library(dassys INTERFACE)

#Creating library
target_link_libraries(dassys INTERFACE
  common
  src
  )

Second Level directory {{PROJECT} /common}(2 folders 1 file ): cfgd_*** CMakeLists.txt dstdh header.h

Second Level CMakeLists.txt

#Adding Subdirectory

include(${CMAKE_CURRENT_LIST_DIR}/dstdh/CMakeLists.txt)
include(${CMAKE_CURRENT_LIST_DIR}/cfgd_***/CMakeLists.txt)
#add_subdirectory(dstdh)
#add_subdirectory(cfgd_***)

#Add Library
add_library(common INTERFACE)

#Creating library
target_link_libraries(common INTERFACE
  dstdh
  cfgd_***
  )

----------------------------------------------------------------------------------------------------------------------------------
​Thrid Level directory { {PROJECT} /common/dstdh }(6 files ): 
CMakeLists.txt dassert.h  dfloat.h  dstdbit.h  dstdbool.h  dstdint.h  dstdio.h  dstring.h

Thrid Level CMakeLists.txt 



#Adding Library
add_library(dstdh INTERFACE )


target_sources(dstd INTERFACE
  ${CMAKE_CURRENT_LIST_DIR}/dassert.h
  ${CMAKE_CURRENT_LIST_DIR}/dfloat.h
  ${CMAKE_CURRENT_LIST_DIR}/dstdbit.h
  ${CMAKE_CURRENT_LIST_DIR}/dstdbool.h
  ${CMAKE_CURRENT_LIST_DIR}/dstdint.h
  ${CMAKE_CURRENT_LIST_DIR}/dstdio.h
  ${CMAKE_CURRENT_LIST_DIR}/dstring.h
  )

#Add include for Header only library
target_include_directories(dstdh INTERFACE
  "${CMAKE_CURRENT_LIST_DIR}/dstdh/"
  )

and here is my question, i need to link against dstd library in another directory

how can this be done because as far as i understand

target_link_libraries ( mylib dstd ) should work in different directory because i have target which is dstd INTERFACE library, and i need Cmake to resolve this dependency by finding this target and link against it

i get compilation error as dstdint.h is not found when mylib is being compiled

  • Looks like argument for `target_include_directories` should be `${CMAKE_CURRENT_LIST_DIR}`, not a `${CMAKE_CURRENT_LIST_DIR}/dstdh/`, because current directory is already `/common/dstdh`. BTW, in variable *CMAKE_CURRENT_LIST_DIR* is mostly intended for `include()`-ed scripts; in `CMakeLists.txt` scripts (which are added with `add_subdirectory()`) *CMAKE_CURRENT_SOURCE_DIR* is used usually. – Tsyvarev Nov 13 '17 at 22:06

0 Answers0