Merge pull request #27 from ttroy50/add-conan-imported-targets
add conan using imported targets
This commit is contained in:
commit
cd65d74137
@ -149,4 +149,11 @@ default_options:
|
||||
with_fmt_alias: False
|
||||
----
|
||||
|
||||
This shows details about the package including what link:https://docs.conan.io/en/latest/using_packages/conanfile_txt.html#options[options] can be set when including the package. In the case of fmtlib there are 4 options which allow you to specify the type of installion you want.
|
||||
This shows details about the package including what link:https://docs.conan.io/en/latest/using_packages/conanfile_txt.html#options[options] can be set when including the package. In the case of fmtlib there are 4 options which allow you to specify the type of installion you want.
|
||||
|
||||
# Examples
|
||||
|
||||
The examples included are:
|
||||
|
||||
- link:i-basic[Basic]. A basic example linking against one library.
|
||||
- link:ii-basic-targets[Using Targets]. The basic example, except using modern CMake targets.
|
||||
|
@ -0,0 +1,17 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project (conan_third_party_include)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
# Included the conan build information
|
||||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
||||
conan_basic_setup(TARGETS)
|
||||
|
||||
# Add an executable
|
||||
add_executable(third_party_include main.cpp)
|
||||
|
||||
# link against the fmt target supplied by conan
|
||||
target_link_libraries(third_party_include
|
||||
PRIVATE
|
||||
CONAN_PKG::fmt
|
||||
)
|
170
07-package-management/D-conan/ii-basic-targets/README.adoc
Normal file
170
07-package-management/D-conan/ii-basic-targets/README.adoc
Normal file
@ -0,0 +1,170 @@
|
||||
= Conan Basic Example
|
||||
:toc:
|
||||
:toc-placement!:
|
||||
|
||||
toc::[]
|
||||
|
||||
# Introduction
|
||||
|
||||
Modern CMake recommends to use a target based approach to including libraries. This example shows how to support this using
|
||||
conan in an example that is functionally the same as the previous one.
|
||||
|
||||
The files in this tutorial are below:
|
||||
|
||||
```
|
||||
.
|
||||
├── CMakeLists.txt
|
||||
├── conanfile.txt
|
||||
├── main.cpp
|
||||
└── README.adoc
|
||||
```
|
||||
|
||||
* link:CMakeLists.txt[] - Contains the CMake commands to run
|
||||
* link:conanfile.txt[] - Contains the conan dependencies and options
|
||||
* link:main.cpp[] - Source file of the application.
|
||||
|
||||
# Concepts
|
||||
|
||||
## Libraries Targets
|
||||
|
||||
As described in the link:https://github.com/ttroy50/cmake-examples/tree/master/01-basic/H-third-party-library[third party library] example, CMake recommends to use +ALIAS+ targets for third party libraries. An example of
|
||||
adding an imported target is
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
target_link_libraries( third_party_include
|
||||
PRIVATE
|
||||
Boost::filesystem
|
||||
)
|
||||
----
|
||||
|
||||
|
||||
## Conan Imported Targets
|
||||
|
||||
To configure conan to use imported targets add the argument +TARGETS+ to the +conan_basic_setup+ function. This will cause conan to create a `CONAN_PKG::pkg_name` target that can be imported as a library.
|
||||
|
||||
An example of this using the `fmt` library is below:
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
conan_basic_setup(TARGETS)
|
||||
|
||||
add_executable(third_party_include)
|
||||
|
||||
target_link_libraries(third_party_include
|
||||
PRIVATE
|
||||
CONAN_PKG::fmt
|
||||
)
|
||||
----
|
||||
|
||||
# Building the Example
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
$ mkdir build
|
||||
|
||||
$ cd build/
|
||||
|
||||
$ conan install ..
|
||||
Configuration:
|
||||
[settings]
|
||||
arch=x86_64
|
||||
arch_build=x86_64
|
||||
build_type=Release
|
||||
compiler=gcc
|
||||
compiler.libcxx=libstdc++11
|
||||
compiler.version=5
|
||||
os=Linux
|
||||
os_build=Linux
|
||||
[options]
|
||||
[build_requires]
|
||||
[env]
|
||||
|
||||
fmt/5.3.0@bincrafters/stable: Not found in local cache, looking in remotes...
|
||||
fmt/5.3.0@bincrafters/stable: Trying with 'conan-center'...
|
||||
Downloading conanmanifest.txt
|
||||
[==================================================] 166B/166B
|
||||
Downloading conanfile.py
|
||||
[==================================================] 2.9KB/2.9KB
|
||||
Downloading conan_export.tgz
|
||||
[==================================================] 758B/758B
|
||||
Decompressing conan_export.tgz: 1.98kB [00:00, 504kB/s]
|
||||
fmt/5.3.0@bincrafters/stable: Downloaded recipe revision 0
|
||||
conanfile.txt: Installing package
|
||||
Requirements
|
||||
fmt/5.3.0@bincrafters/stable from 'conan-center' - Downloaded
|
||||
Packages
|
||||
fmt/5.3.0@bincrafters/stable:4d887c1c2779c63d2cdd81580698d2e22cb35b29 - Download
|
||||
|
||||
fmt/5.3.0@bincrafters/stable: Retrieving package 4d887c1c2779c63d2cdd81580698d2e22cb35b29 from remote 'conan-center'
|
||||
Downloading conanmanifest.txt
|
||||
[==================================================] 1.1KB/1.1KB
|
||||
Downloading conaninfo.txt
|
||||
[==================================================] 550B/550B
|
||||
Downloading conan_package.tgz
|
||||
[==================================================] 156.2KB/156.2KB
|
||||
Decompressing conan_package.tgz: 161kB [00:00, 13.8MB/s]
|
||||
fmt/5.3.0@bincrafters/stable: Package installed 4d887c1c2779c63d2cdd81580698d2e22cb35b29
|
||||
fmt/5.3.0@bincrafters/stable: Downloaded package revision 0
|
||||
conanfile.txt: Generator cmake created conanbuildinfo.cmake
|
||||
conanfile.txt: Generator txt created conanbuildinfo.txt
|
||||
conanfile.txt: Generated conaninfo.txt
|
||||
conanfile.txt: Generated graphinfo
|
||||
|
||||
$ 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
|
||||
-- Conan: Adjusting output directories
|
||||
-- Conan: Using cmake targets configuration
|
||||
-- Library fmt found /home/devuser/.conan/data/fmt/5.3.0/bincrafters/stable/package/4d887c1c2779c63d2cdd81580698d2e22cb35b29/lib/libfmt.a
|
||||
-- Conan: Adjusting default RPATHs Conan policies
|
||||
-- Conan: Adjusting language standard
|
||||
-- Current conanbuildinfo.cmake directory: /data/code/07-package-management/D-conan/ii-basic-targets/build
|
||||
-- Conan: Compiler GCC>=5, checking major version 5
|
||||
-- Conan: Checking correct version: 5
|
||||
-- Configuring done
|
||||
-- Generating done
|
||||
-- Build files have been written to: /data/code/07-package-management/D-conan/ii-basic-targets/build
|
||||
|
||||
|
||||
$ make VERBOSE=1
|
||||
/usr/local/bin/cmake -H/data/code/07-package-management/D-conan/ii-basic-targets -B/data/code/07-package-management/D-conan/ii-basic-targets/build --check-build-system CMakeFiles/Makefile.cmake 0
|
||||
/usr/local/bin/cmake -E cmake_progress_start /data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles /data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles/progress.marks
|
||||
make -f CMakeFiles/Makefile2 all
|
||||
make[1]: Entering directory '/data/code/07-package-management/D-conan/ii-basic-targets/build'
|
||||
make -f CMakeFiles/third_party_include.dir/build.make CMakeFiles/third_party_include.dir/depend
|
||||
make[2]: Entering directory '/data/code/07-package-management/D-conan/ii-basic-targets/build'
|
||||
cd /data/code/07-package-management/D-conan/ii-basic-targets/build && /usr/local/bin/cmake -E cmake_depends "Unix Makefiles" /data/code/07-package-management/D-conan/ii-basic-targets /data/code/07-package-management/D-conan/ii-basic-targets /data/code/07-package-management/D-conan/ii-basic-targets/build /data/code/07-package-management/D-conan/ii-basic-targets/build /data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles/third_party_include.dir/DependInfo.cmake --color=
|
||||
Dependee "/data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles/third_party_include.dir/DependInfo.cmake" is newer than depender "/data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles/third_party_include.dir/depend.internal".
|
||||
Dependee "/data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles/third_party_include.dir/depend.internal".
|
||||
Scanning dependencies of target third_party_include
|
||||
make[2]: Leaving directory '/data/code/07-package-management/D-conan/ii-basic-targets/build'
|
||||
make -f CMakeFiles/third_party_include.dir/build.make CMakeFiles/third_party_include.dir/build
|
||||
make[2]: Entering directory '/data/code/07-package-management/D-conan/ii-basic-targets/build'
|
||||
[ 50%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.o
|
||||
/usr/bin/c++ -isystem /home/devuser/.conan/data/fmt/5.3.0/bincrafters/stable/package/4d887c1c2779c63d2cdd81580698d2e22cb35b29/include -std=gnu++11 -o CMakeFiles/third_party_include.dir/main.cpp.o -c /data/code/07-package-management/D-conan/ii-basic-targets/main.cpp
|
||||
[100%] Linking CXX executable bin/third_party_include
|
||||
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/third_party_include.dir/link.txt --verbose=1
|
||||
/usr/bin/c++ CMakeFiles/third_party_include.dir/main.cpp.o -o bin/third_party_include /home/devuser/.conan/data/fmt/5.3.0/bincrafters/stable/package/4d887c1c2779c63d2cdd81580698d2e22cb35b29/lib/libfmt.a
|
||||
make[2]: Leaving directory '/data/code/07-package-management/D-conan/ii-basic-targets/build'
|
||||
[100%] Built target third_party_include
|
||||
make[1]: Leaving directory '/data/code/07-package-management/D-conan/ii-basic-targets/build'
|
||||
/usr/local/bin/cmake -E cmake_progress_start /data/code/07-package-management/D-conan/ii-basic-targets/build/CMakeFiles 0
|
||||
|
||||
/usr/bin/cmake -E cmake_progress_start /home/devuser/ws/build/CMakeFiles 0
|
||||
|
||||
$ ./bin/third_party_include
|
||||
Hello, conan. This is fmtlib!
|
||||
----
|
@ -0,0 +1,5 @@
|
||||
[requires]
|
||||
fmt/5.3.0@bincrafters/stable
|
||||
|
||||
[generators]
|
||||
cmake
|
8
07-package-management/D-conan/ii-basic-targets/main.cpp
Normal file
8
07-package-management/D-conan/ii-basic-targets/main.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <fmt/format.h>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
fmt::print("Hello, {}. This is {}!\n", "conan", "fmtlib");
|
||||
return 0;
|
||||
}
|
19
07-package-management/D-conan/ii-basic-targets/run_test.sh
Executable file
19
07-package-management/D-conan/ii-basic-targets/run_test.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
conan_bin=`which conan`
|
||||
|
||||
if [ -z $conan_bin ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
conan profile show default || {
|
||||
conan profile new default --detect
|
||||
}
|
||||
conan profile update settings.compiler.libcxx=libstdc++11 default
|
||||
|
||||
echo "correct version of cmake"
|
||||
mkdir -p build && cd build && conan install .. && cmake .. && make
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error running example"
|
||||
exit 1
|
||||
fi
|
3
test.sh
3
test.sh
@ -36,7 +36,8 @@ dirs=(./01-basic/A-hello-cmake \
|
||||
./05-unit-testing/google-test-download \
|
||||
./05-unit-testing/catch2-vendored \
|
||||
./06-installer/deb \
|
||||
./07-package-management/D-conan/basic \
|
||||
./07-package-management/D-conan/i-basic \
|
||||
./07-package-management/D-conan/ii-basic-targets \
|
||||
)
|
||||
|
||||
ROOT_DIR=`pwd`
|
||||
|
Loading…
Reference in New Issue
Block a user