Directory structure for CMake library projects
I have an application project Demo
that depends on my shared library Hello
. The library Hello
must be redistributable. So I need to distribute Hello headers and also use it in my demo projects. What directory structure and CMake configuration should I use? I used to have a flat structure and Hello
only a header subdirectory without CMakeLists.txt. I am now planning this directory structure.
Demo
main.cpp
app.h
app.cpp
CMakeLists.txt
Hello
includes
matrix.hpp // header only
diagonal.hpp
point.hpp
store.h
analyzer.h
sources
store.cpp
alanyzer.cpp
CMakeLists.txt
Previously, the parser was just a title, which is now broken down into title and source. I only mix titles. Is this a good structure? But I prefer to use nice #include <Hallo/matrix.hpp>
not ugly #include "Hallo/includes/matrix.hpp"
or simple #include "matrix.hpp"
. I expect this library to Hello
be used for other applications and libraries as well.
Demo/CMakeList.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(Demo)
ADD_SUBDIRECTORY(Demo)
SET(Demo_HEADERS
app.h
)
SET(Demo_SOURCES
app.cpp
main.cpp
)
INCLUDE_DIRECTORIES(${Hello_INCLUDE_DIRS})
# ^^^^ Is this how I need to access the headers ? or just ADD_SUBDIRECTORY() will work
ADD_EXECUTABLE(Demo ${Demo_SOURCES} ${Demo_HEADERS})
TARGET_LINK_LIBRARIES(Demo Hello)
Hello / CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(Hello)
FIND_PACKAGE(Boost COMPONENTS filesystem program_options thread system serialization date_time chrono REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
SET(Hello_SOURCES
store.cpp
analyzer.cpp
)
INCLUDE_DIRECTORIES((${HELLO_SOURCE_DIR}/includes)
# ^^^^ Is this how I need to access the headers ?
ADD_LIBRARY(Hello ${Hello_SOURCES})
TARGET_LINK_LIBRARIES(Hello ${Boost_LIBRARIES})
I'm a little confused with all of this since there is no specific way to do all of this.
--- EDIT ---
Should I have this structure? Or do some other libraries usually use this structure? or what structures are commonly practiced?
hallo
includes
hallo
sources
source to share
If you want to access your headers like this:
#include <matrix.hpp>
then you need to include directories. I suggest you the following:
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/Hello/includes)
Your mistake is that you never define the Hello_INCLUDE_DIRS variable in the global scope, only in the local one. So your inclusions don't do anything. CMAKE_SOURCE_DIR is always defined and safe to use. So you either need to define Hello_INCLUDE_DIRS in the global scope or use INCLUDE_DIRECTORIES ($ {CMAKE_SOURCE_DIR} / Hello / includes).
Usage is, as always, up to the developer. I prefer the structure:
+ Project Root
+-> Build
+-> Documentation
+-> Include
+-> Source
+-> Test
+ CMakeLists.txt
Then I can set the following useful variables in the root of CMakeLists.txt:
SET(Project_Include_Dir "${CMAKE_SOURCE_DIR}/Include")
SET(Project_Source_Dir "${CMAKE_SOURCE_DIR}/Source")
source to share