1

I have written a library in c++. I am using CMAKE to build the library. The library gets built but the header files are not getting installed. Below is how my CMakeLists.txt file looks like.

cmake_minimum_required(VERSION 3.14)
project(Strand)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_BUILD_TYPE Release)

set(Headers [all the header files])
set(Sources [all the source files])

add_library(LibraryName STATIC ${Sources} ${Headers})
install(TARGETS LibraryName DESTINATION /usr/lib)

In addition, the header files are quite a number. So I want all the header files to be installed into a directory, e.g. usr/local/include/LibraryName/[all header files].

How can I achieve of fix this?

user2987773
  • 407
  • 5
  • 18
  • I went with this option `install(FILES ${HEADERS} DESTINATION include/libraryname)`. @squareskittles @Zaffy However, the source and header files are organized into sub-directories, as a result when I try to use the library in a test application. The location of the header files cannot be resolved. – user2987773 Nov 18 '19 at 15:14
  • So you want to have different structure in project and after install? – Zaffy Nov 18 '19 at 15:20
  • @Zaffy Yes, I have used sub-directories within the `src` folder to organize related header and sources files. – user2987773 Nov 18 '19 at 15:23
  • It's a bit hacky but you could try to use `foreach` and call `install` on each header individually – Zaffy Nov 18 '19 at 15:33
  • I solved it using ``` install(DIRECTORY ${CMAKE_SOURCE_DIR}/src/ DESTINATION include/library FILES_MATCHING PATTERN "*.h" ) ``` using the suggestion from @squareskittles with a little tweak to the directory. – user2987773 Nov 18 '19 at 16:00
  • great you solved your issue, now you can post your solution as an answer :) – Zaffy Nov 18 '19 at 16:27
  • @user2987773 Yes, If you have have solved your issue, please post an answer showing what you did to resolve it, and mark it as accepted. – Kevin Nov 18 '19 at 16:45

1 Answers1

1

I solved it using install(DIRECTORY ${CMAKE_SOURCE_DIR}/src/ DESTINATION include/library FILES_MATCHING PATTERN "*.h" ) using the suggestion from @squareskittles with a little tweak to the directory

user2987773
  • 407
  • 5
  • 18