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:
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
|
||||
|
||||
Reference in New Issue
Block a user