update sub project example to use include instead of inc

This commit is contained in:
Thom Troy
2017-07-02 21:37:01 +01:00
parent 420969b4d9
commit 6c3842465f
8 changed files with 30 additions and 20 deletions

View File

@@ -25,24 +25,40 @@ $ tree
│   └── main.cpp │   └── main.cpp
├── sublibrary1 ├── sublibrary1
│   ├── CMakeLists.txt │   ├── CMakeLists.txt
│   ├── inc │   ├── include
│   │   └── sublib1.h │   │   └── sublib1
│   │   └── sublib1.h
│   └── src │   └── src
│   └── sublib1.cpp │   └── sublib1.cpp
└── sublibrary2 └── sublibrary2
├── CMakeLists.txt ├── CMakeLists.txt
└── inc └── include
└── sublib2.h └── sublib2
└── sublib2.h
``` ```
* link:CMakeLists.txt[] - Top level CMakeLists.txt * link:CMakeLists.txt[] - Top level CMakeLists.txt
* link:subbinary/CMakeLists.txt[] - to make the executable * link:subbinary/CMakeLists.txt[] - to make the executable
* link:subbinary/main.cpp[] - source for the executable * link:subbinary/main.cpp[] - source for the executable
* link:sublibrary1/CMakeLists.txt[] - to make a static library * link:sublibrary1/CMakeLists.txt[] - to make a static library
* link:sublibrary1/inc/sublib1.h[] * link:sublibrary1/include/sublib1/sublib1.h[]
* link:sublibrary1/src/sublib2.cpp[] * link:sublibrary1/src/sublib2.cpp[]
* link:sublibrary2/CMakeLists.txt[] - to setup header only library * link:sublibrary2/CMakeLists.txt[] - to setup header only library
* link:sublibrary2/inc/sublib2.h[] * link:sublibrary2/include/sublib2/sublib2.h[]
[TIP]
====
In this example I have moved the header files to a subfolder under each projects +include+
directory, while leaving the target include as the root +include+ folder. This is a good idea to prevent
filename clashes because you have to include a file like below:
[source,cpp]
----
#include "sublib1/sublib1.h"
----
This also means that if you install your library for other users the default install location would be
+/usr/local/include/sublib1/sublib1.h+.
====
# Concepts # Concepts
@@ -114,7 +130,7 @@ that link this target but not in the complation of the target itself.
---- ----
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}
INTERFACE INTERFACE
${PROJECT_SOURCE_DIR}/inc ${PROJECT_SOURCE_DIR}/include
) )
---- ----

View File

@@ -9,14 +9,8 @@ add_executable(${PROJECT_NAME} ${SOURCES})
# Link the static library from subproject1 using it's alias sub::lib1 # Link the static library from subproject1 using it's alias sub::lib1
# Link the header only library from subproject2 using it's alias sub::lib2 # Link the header only library from subproject2 using it's alias sub::lib2
# This will cause the include directories for that target to be added to this project
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
sub::lib1 sub::lib1
sub::lib2 sub::lib2
) )
# Include the inc directories from the sub projects
include_directories( ${PROJECT_NAME}
PRIVATE
${sublibrary1_SOURCE_DIR}/inc
${sublibrary2_SOURCE_DIR}/inc
)

View File

@@ -1,5 +1,5 @@
#include "sublib1.h" #include "sublib1/sublib1.h"
#include "sublib2.h" #include "sublib2/sublib2.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View File

@@ -10,6 +10,6 @@ set(SOURCES
add_library(${PROJECT_NAME} ${SOURCES}) add_library(${PROJECT_NAME} ${SOURCES})
add_library(sub::lib1 ALIAS ${PROJECT_NAME}) add_library(sub::lib1 ALIAS ${PROJECT_NAME})
include_directories( ${PROJECT_NAME} target_include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}/inc PUBLIC ${PROJECT_SOURCE_DIR}/include
) )

View File

@@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include "sublib1.h" #include "sublib1/sublib1.h"
void sublib1::print() void sublib1::print()
{ {

View File

@@ -6,5 +6,5 @@ add_library(sub::lib2 ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} target_include_directories(${PROJECT_NAME}
INTERFACE INTERFACE
${PROJECT_SOURCE_DIR}/inc ${PROJECT_SOURCE_DIR}/include
) )