Update to minimum CMake 3.5

And modernise some examples.
This commit is contained in:
Thom Troy
2018-03-18 17:23:57 +00:00
parent 54e75664c1
commit b81da6f68b
52 changed files with 303 additions and 212 deletions

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
project(hello_library)
@@ -6,16 +6,14 @@ project(hello_library)
# Create a library
############################################################
# Source files to be used in the library
set(library_SOURCES
#Generate the static library from the library sources
add_library(hello_library STATIC
src/Hello.cpp
)
#Generate the static library from the library sources
add_library(hello_library STATIC ${library_SOURCES})
target_include_directories(hello_library
PUBLIC ${PROJECT_SOURCE_DIR}/include
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
@@ -23,15 +21,13 @@ target_include_directories(hello_library
# Create an executable
############################################################
# Source fles for the binary
set(binary_SOURCES
# Add an executable with the above sources
add_executable(hello_binary
src/main.cpp
)
# Add an executable with the above sources
add_executable(hello_binary ${binary_SOURCES})
# link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary
PRIVATE hello_library
PRIVATE
hello_library
)

View File

@@ -6,7 +6,9 @@ toc::[]
# Introduction
Shows a hello world example which first creates and links a static library
Shows a hello world example which first creates and links a static library. This is a
simplified example showing the libray and binary in the same folder. Typically
these would be in sub-projects as described in section 02-sub-projects
The files in this tutorial are below:
@@ -15,14 +17,15 @@ $ tree
.
├── CMakeLists.txt
├── include
│   └── Hello.h
│   └── static
│   └── Hello.h
└── src
├── Hello.cpp
└── main.cpp
```
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:include/Hello.h[] - The header file to include
* link:include/static/Hello.h[] - The header file to include
* link:src/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with main
@@ -36,15 +39,19 @@ This is called as follows:
[source,cmake]
----
set(library_SOURCES
add_library(hello_library STATIC
src/Hello.cpp
)
add_library(hello_library STATIC ${library_SOURCES})
----
This will be used to create a static library with the name libhello_library.a with
the sources from the +library_SOURCES+ variable.
the sources in the +add_library+ call.
[NOTE]
====
As mentioned in the prevoius example, we pass the source files directly to the
+add_library+ call, as recommended for modern CMake.
====
## Populating Including Directories
@@ -53,7 +60,8 @@ In this example, we include directories in the library using the +target_include
[source,cmake]
----
target_include_directories(hello_library
PUBLIC ${PROJECT_SOURCE_DIR}/include
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
----
@@ -69,6 +77,24 @@ The meaning of scopes are:
* +PUBLIC+ - As above, it is included int his library and also any targets that link this library.
[TIP]
====
For public headers it is often a good idea to have your include folder be "namespaced"
with sub-directories.
The directory passed to +target_include_directories+ will be the root of your
include directory and your C++ files should include the path from there to your header.
For this example you can see that we do it as follows:
[source,cpp]
----
#include "static/Hello.h"
----
Using this method means that there is less chance of header filename clashes when
you use multiple libraries in your project.
====
## Linking a Library
When creating an executable that will use your library you must tell the compiler
@@ -76,10 +102,13 @@ about the library. This can be done using the +target_link_library()+ function.
[source,cmake]
----
add_executable(hello_binary ${binary_SOURCES})
add_executable(hello_binary
src/main.cpp
)
target_link_libraries( hello_binary
PRIVATE hello_library
PRIVATE
hello_library
)
----

View File

@@ -1,8 +1,8 @@
#include <iostream>
#include "Hello.h"
#include "static/Hello.h"
void Hello::print()
{
std::cout << "Hello Static Library!" << std::endl;
}
}

View File

@@ -1,8 +1,8 @@
#include "Hello.h"
#include "static/Hello.h"
int main(int argc, char *argv[])
{
Hello hi;
hi.print();
return 0;
}
}