mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 12:14:36 +03:00
add imported target example and cmake 3.5.1 docker
This commit is contained in:
@@ -6,8 +6,10 @@ compiler:
|
||||
- gcc
|
||||
before_install:
|
||||
- docker pull matrim/cmake-examples:3.4.3
|
||||
- docker pull matrim/cmake-examples:3.5.1
|
||||
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.5.1 /data/code/test.sh
|
||||
branches:
|
||||
except:
|
||||
- gh-pages
|
||||
|
||||
24
01-basic/K-imported-targets/CMakeLists.txt
Normal file
24
01-basic/K-imported-targets/CMakeLists.txt
Normal 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
|
||||
)
|
||||
92
01-basic/K-imported-targets/README.adoc
Normal file
92
01-basic/K-imported-targets/README.adoc
Normal 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
|
||||
|
||||
|
||||
----
|
||||
24
01-basic/K-imported-targets/main.cpp
Normal file
24
01-basic/K-imported-targets/main.cpp
Normal 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;
|
||||
}
|
||||
12
01-basic/K-imported-targets/run_test.sh
Executable file
12
01-basic/K-imported-targets/run_test.sh
Executable 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
|
||||
@@ -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: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:K-imported-targets[imported-targets] - Shows how to link boost using the new imported targets
|
||||
|
||||
@@ -22,7 +22,7 @@ For example:
|
||||
|
||||
[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
|
||||
@@ -45,6 +45,10 @@ The images available include the following versions of cmake:
|
||||
|
||||
$ 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
|
||||
|
||||
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]
|
||||
----
|
||||
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
|
||||
----
|
||||
|
||||
23
dockerfiles/ubuntu16.04-default-cmake-3.5.1
Normal file
23
dockerfiles/ubuntu16.04-default-cmake-3.5.1
Normal 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"]
|
||||
1
test.sh
1
test.sh
@@ -21,6 +21,7 @@ dirs=(./01-basic/A-hello-cmake \
|
||||
./01-basic/H-third-party-library \
|
||||
./01-basic/I-compiling-with-clang \
|
||||
./01-basic/J-building-with-ninja \
|
||||
./01-basic/K-imported-targets \
|
||||
./02-sub-projects/A-basic \
|
||||
./03-code-generation/protobuf \
|
||||
./03-code-generation/configure-files \
|
||||
|
||||
Reference in New Issue
Block a user