“From using libraries already packaged by Conan, to how to package your libraries and store them in a remote server alongside all the precompiled binaries.”
- Clone the sources of this example project from github, and open the simple_cmake_project directory:
> git clone https://github.com/conan-io/examples2.git
> cd examples2/tutorial/consuming_packages/simple_cmake_projectE:\XXXX\examples2\tutorial\consuming_packages\simple_cmake_project> tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 EXXX-XXXX
E:.
| CMakeLists.txt
| conanfile.txt
|
+---build
\---srcmain.c
main.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>#include <zlib.h>int main(void) {char buffer_in [256] = {"Conan is a MIT-licensed, Open Source package manager for C and C++ development, ""allowing development teams to easily and efficiently manage their packages and ""dependencies across platforms and build systems."};char buffer_out [256] = {0};z_stream defstream;defstream.zalloc = Z_NULL;defstream.zfree = Z_NULL;defstream.opaque = Z_NULL;defstream.avail_in = (uInt) strlen(buffer_in);defstream.next_in = (Bytef *) buffer_in;defstream.avail_out = (uInt) sizeof(buffer_out);defstream.next_out = (Bytef *) buffer_out;deflateInit(&defstream, Z_BEST_COMPRESSION);deflate(&defstream, Z_FINISH);deflateEnd(&defstream);printf("Uncompressed size is: %lu\n", strlen(buffer_in));printf("Compressed size is: %lu\n", strlen(buffer_out));printf("ZLIB VERSION: %s\n", zlibVersion());return EXIT_SUCCESS;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(compressor C)find_package(ZLIB REQUIRED)add_executable(${PROJECT_NAME} src/main.c)
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
- conanfile.txt
This application relies on the Zlib library. Conan, by default, tries to install libraries from a remote server called ConanCenter. One can search there for libraries and also check the available versions. In this case, after checking the available versions for Zlib one of the latest versions: zlib/1.2.11 is chosed.
The easiest way to install the Zlib library and find it from this project with Conan is using a conanfile.txt file:
[requires]
zlib/1.2.11[generators]
CMakeDeps
CMakeToolchain
• [requires] section is where we declare the libraries we want to use in the project, in this case, zlib/1.2.11.
• [generators] section tells Conan to generate the files that the compilers or build systems will use to find the dependencies and build the project. In this case, as our project is based in CMake, we will use:
CMakeDeps to generate information about where the Zlib library files are installed and
CMakeToolchain to pass build information to CMake using a CMake toolchain file.
- Conan profile
Besides the conanfile.txt, we need a Conan profile to build our project.
Conan profiles allow users to define a configuration set for things like the compiler, build configuration, architecture, shared or static libraries, etc.
Conan, by default, will not try to detect a profile automatically, so we need to create one.
To let Conan try to guess the profile, based on the current operating system and installed tools, please run:
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project>conan profile detect --force
detect_api: Found msvc 17Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=193
os=WindowsWARN: This profile is a guess of your environment, please check it.
WARN: The output of this command is not guaranteed to be stable and can change in future Conan versions.
WARN: Use your own profile files for stability.
Saving detected profile to C:\Users\XXXXX\.conan2\profiles\default
This will detect the operating system, build architecture and compiler settings based on the environment.
It will also set the build configuration as Release by default.
The generated profile will be stored in the Conan home folder with name default and will be used by Conan in all commands by default unless another profile is specified via the command line.
- conan install
Use Conan to install Zlib and generate the files that CMake needs to find this library and build project. Those files will be generated in the folder build:
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project>conan install . --output-folder=build --build=missing======== Input profiles ========
Profile host:
[settings]
arch=x86_64
...
...
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project> tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 XXXX-XXXX
E:.
| CMakeLists.txt
| CMakeUserPresets.json
| conanfile.txt
|
+---build
| cmakedeps_macros.cmake
| CMakePresets.json
| conanbuild.bat
| conanbuildenv-release-x86_64.bat
| conandeps_legacy.cmake
| conanrun.bat
| conanrunenv-release-x86_64.bat
| conan_toolchain.cmake
| deactivate_conanbuild.bat
| deactivate_conanrun.bat
| FindZLIB.cmake
| module-ZLIB-release-x86_64-data.cmake
| module-ZLIB-Target-release.cmake
| module-ZLIBTargets.cmake
| ZLIB-release-x86_64-data.cmake
| ZLIB-Target-release.cmake
| ZLIBConfig.cmake
| ZLIBConfigVersion.cmake
| ZLIBTargets.cmake
|
\---srcmain.c
• Conan installed the Zlib library from the remote server, which should be the Conan Center server by default if the library is available. This server stores both the Conan recipes, which are the files that define how libraries must be built, and the binaries that can be reused so we don’t have to build from sources every time.
• Conan generated several files under the build folder. Those files were generated by both the CMakeToolchain and CMakeDeps generators we set in the conanfile.txt. CMakeDeps generates files so that CMake finds the Zlib library we have just downloaded. On the other side, CMakeToolchain generates a toolchain file for CMake so that we can transparently build our project with CMake using the same settings that we detected for our default profile.
- build and run compressor app
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project>cd buildE:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build>cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
-- Using Conan toolchain: E:/XXXXX/examples2/tutorial/consuming_packages/simple_cmake_project/build/conan_toolchain.cmake
-- Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.36.32532.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Conan: Target declared 'ZLIB::ZLIB'
-- Configuring done (4.0s)
-- Generating done (0.0s)
-- Build files have been written to: E:/XXXXX/examples2/tutorial/consuming_packages/simple_cmake_project/buildE:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build>cmake --build . --config Release
MSBuild version 17.6.3+07e294721 for .NET FrameworkChecking Build SystemBuilding Custom Rule E:/XXXXX/examples2/tutorial/consuming_packages/simple_cmake_project/CMakeLists.txtmain.cE:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build>cd Release
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build\Release>compressor.exe
Uncompressed size is: 207
Compressed size is: 19
ZLIB VERSION: 1.2.11
References
- Conan Documentation --Release 2.0.17