add imported target example and cmake 3.5.1 docker

This commit is contained in:
Thom Troy
2016-08-19 22:28:42 +01:00
parent f9f1a5bbb0
commit bb61b1683d
9 changed files with 185 additions and 2 deletions

View File

@@ -6,8 +6,10 @@ compiler:
- gcc - gcc
before_install: before_install:
- docker pull matrim/cmake-examples:3.4.3 - docker pull matrim/cmake-examples:3.4.3
- docker pull matrim/cmake-examples:3.5.1
script: script:
- docker run --rm -v $PWD:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh - docker run --rm -v $PWD:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh
- docker run --rm -v $PWD:/data/code -it matrim/cmake-examples:3.5.1 /data/code/test.sh
branches: branches:
except: except:
- gh-pages - gh-pages

View File

@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (imported_targets)
# find a boost install with the libraries filesystem and system
find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)
# check if boost was found
if(Boost_FOUND)
message ("boost found")
else()
message (FATAL_ERROR "Cannot find Boost")
endif()
# Add an executable
add_executable(imported_targets main.cpp)
# link against the boost libraries
target_link_libraries( imported_targets
PRIVATE
Boost::filesystem
)

View File

@@ -0,0 +1,92 @@
= Imported Targets
:toc:
:toc-placement!:
toc::[]
# Introduction
As previously mentioned in the link:../H-third-party-library[third party library], newer
versions of CMake alow you to link third party libraries using link:https://cmake.org/cmake/help/v3.6/prop_tgt/IMPORTED.html#prop_tgt:IMPORTED[imported] +ALIAS+ targets.
The files in this tutorial are below:
```
$ tree
.
├── CMakeLists.txt
├── main.cpp
```
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:main.cpp[] - The source file with main
# Requirements
This example requires the boost libraries to be installed in a default system location.
# Concepts
## Imported Target
Imported targets are read-only targets that are exported by FindXXX modules. The benefit of imported
targets are that they can also populate include directories and linked libraries.
To include boost filesystem you can do the following:
[source,cmake]
----
target_link_libraries( imported_targets
PRIVATE
Boost::filesystem
)
----
This will automtaically link the Boost::filesystem and Boost::system libraries while also including the
Boost include directories.
# Building the Example
[source,bash]
----
$ mkdir build
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- filesystem
-- system
boost found
-- Configuring done
-- Generating done
-- Build files have been written to: /data/code/01-basic/K-imported-targets/build
$ make
Scanning dependencies of target imported_targets
[ 50%] Building CXX object CMakeFiles/imported_targets.dir/main.cpp.o
[100%] Linking CXX executable imported_targets
[100%] Built target imported_targets
$ ./imported_targets
Hello Third Party Include!
Path is not relative
----

View File

@@ -0,0 +1,24 @@
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>
int main(int argc, char *argv[])
{
std::cout << "Hello Third Party Include!" << std::endl;
// use a shared ptr
boost::shared_ptr<int> isp(new int(4));
// trivial use of boost filesystem
boost::filesystem::path path = "/usr/share/cmake/modules";
if(path.is_relative())
{
std::cout << "Path is relative" << std::endl;
}
else
{
std::cout << "Path is not relative" << std::endl;
}
return 0;
}

View File

@@ -0,0 +1,12 @@
#!/bin/bash
# Make sure we have the minimum cmake version
cmake_version=`cmake --version | grep version | cut -d" " -f3`
[[ "$cmake_version" =~ ([3-9][.][5-9.][.][0-9]) ]] || exit 0
echo "correct version of cmake"
mkdir -p build && cd build && cmake .. && make
if [ $? -ne 0 ]; then
echo "Error running example"
exit 1
fi

View File

@@ -15,3 +15,4 @@ The examples included are
- link:H-third-party-library[third-party-library]. Shows an example of how to link third party libraries. - link:H-third-party-library[third-party-library]. Shows an example of how to link third party libraries.
- link:I-compiling-with-clang[compiling-with-clang]. An example of invoking the clang compiler. - link:I-compiling-with-clang[compiling-with-clang]. An example of invoking the clang compiler.
- link:J-building-with-ninja[building-with-ninja] - Shows how to generate ninja build files - link:J-building-with-ninja[building-with-ninja] - Shows how to generate ninja build files
- link:K-imported-targets[imported-targets] - Shows how to link boost using the new imported targets

View File

@@ -22,7 +22,7 @@ For example:
[source,bash] [source,bash]
---- ----
$ docker build --rm -f ubuntu14.04-cmake-3.4.3 t matrim/cmake-examples:3.4.3 . $ docker build --rm -f ubuntu14.04-cmake-3.4.3 -t matrim/cmake-examples:3.4.3 .
---- ----
In this example the tag is created as follows In this example the tag is created as follows
@@ -45,6 +45,10 @@ The images available include the following versions of cmake:
$ docker pull docker pull matrim/cmake-examples:3.4.3 $ docker pull docker pull matrim/cmake-examples:3.4.3
* Ubuntu 16.04 with CMake 3.5.1
$ docker pull docker pull matrim/cmake-examples:3.5.1
# Running # Running
When run the images will automatically create a non root user called devuser, with a default command to launch a bash shell in the users home directory. When run the images will automatically create a non root user called devuser, with a default command to launch a bash shell in the users home directory.
@@ -76,5 +80,5 @@ Below is an example of loading a volume and automatically running all cmake-exam
[source,bash] [source,bash]
---- ----
docker run -e DEV_UID=`id -u` -e DEV_GID=`id -u` -v /checkout/directory:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh docker run --rm -e DEV_UID=`id -u` -e DEV_GID=`id -u` -v /checkout/directory:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh
---- ----

View File

@@ -0,0 +1,23 @@
# Container for building and testing cmake-examples with default cmake v2.8.12.2
FROM ubuntu:16.04
MAINTAINER Thom Troy
RUN apt-get update && apt-get install -y build-essential \
sudo \
cmake \
libboost-all-dev \
libprotobuf-dev \
protobuf-compiler \
cppcheck \
clang-3.6 \
ninja-build \
wget \
git \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ADD setup.sh /setup.sh
RUN chmod +x /setup.sh
CMD ["/bin/bash"]
ENTRYPOINT ["/setup.sh"]

View File

@@ -21,6 +21,7 @@ dirs=(./01-basic/A-hello-cmake \
./01-basic/H-third-party-library \ ./01-basic/H-third-party-library \
./01-basic/I-compiling-with-clang \ ./01-basic/I-compiling-with-clang \
./01-basic/J-building-with-ninja \ ./01-basic/J-building-with-ninja \
./01-basic/K-imported-targets \
./02-sub-projects/A-basic \ ./02-sub-projects/A-basic \
./03-code-generation/protobuf \ ./03-code-generation/protobuf \
./03-code-generation/configure-files \ ./03-code-generation/configure-files \