Conan2: Building for multiple configurations: Release, Debug, Static and Shared

Command Lines

> conan profile detect 
> conan config home
> conan profile show
> conan profile list

> conan install . -pr=debug --output-folder=build --build=missing
> cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
> cmake --build . --config Debug

> conan install . --output-folder=build --build=missing --options=zlib/1.2.11:shared=True
> cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
> cmake --build . --config Release
> conanrun.bat
>



Preparation

> git clone https://github.com/conan-io/examples2.git
> cd examples2/tutorial/consuming_packages/different_configurations

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations>tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 XXXX-XXXX
E:.
|   CMakeLists.txt
|   conanfile.txt
|   run_example.py
|
\---src
        main.c

When you call a Conan command setting the –profile(or -pr) argument, Conan will take all the information from the profile and apply it to the packages you want to build or install. If you don’t specify that argument it’s equivalent to call it with --profile=default. You can store different profiles and use them to build for different settings.

Debug

Use Debug configuration for the application and its dependencies
One can easily get a debug profile from the default profile:
First, copy default profile

.conan2\profiles>copy default debug

Second, modify the debug profile:

debug

[settings]
arch=x86_64
build_type=Debug
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=193
os=Windows

Using profiles is not the only way to set the configuration you want to use. You can also override the profile settings in the Conan command using the --settings argument.

> conan install . --output-folder=build --build=missing --settings=build_type=Debug

As we explained above, this is the equivalent of having debug profile and running these command using the –profile=debug argument instead of the –settings=build_type=Debug argument.

This conan install command will check if we already installed the required libraries (Zlib) in Debug configuration and install them otherwise. It will also set the build configuration in the conan_toolchain.cmake toolchain that the CMakeToolchain generator creates so that when we build the application it’s built in Debug configuration.

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 "
                            "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());

    #ifdef NDEBUG
    printf("Release configuration!\n");
    #else
    printf("Debug configuration!\n");
    #endif

    return EXIT_SUCCESS;
}

build & run

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations>conan install . -pr=debug --output-folder=build --build=missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
build_type=Debug
...
...
conanfile.txt: Generating aggregated env files
conanfile.txt: Generated aggregated env files: ['conanbuild.bat', 'conanrun.bat']
Install finished successfully
E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations>cd build

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
-- Using Conan toolchain: E:\XXXXX\examples2\tutorial\consuming_packages/different_configurations/build/conan_toolchain.cmake
...
...
-- Build files have been written to: E:\XXXXX\examples2/tutorial/consuming_packages/different_configurations/build

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>cmake --build . --config Debug
MSBuild version 17.6.3+07e294721 for .NET Framework
...
...
E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>cd Debug

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build\Debug>compressor.exe
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11
Debug configuration!

Shared

Modifying options: linking the application dependencies as shared libraries
In the Zlib’s Conan package there’s an attribute set to build in that mode by default, so that Zlib is statically linked to this application in default case.
We can change from static to shared linking by setting the shared option to True using the --options argument:

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations>conan install . --output-folder=build --build=missing --options=zlib/1.2.11:shared=True

Doing this, Conan will install the Zlib shared libraries, generate the files to build with them and, also the necessary files to locate those dynamic libraries when running the application.

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations>cd build

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>cmake --build . --config Release

Shared libraries (.dll in windows, .dylib in OSX and .so in Linux), are loaded at runtime. That means
that the application executable needs to know where are the required shared libraries when it runs.

On Windows, the dynamic linker will search in the same directory then in the PATH directories.

On OSX, it will search in the directories declared in DYLD_LIBRARY_PATH.

On Linux will use the LD_LIBRARY_PATH.

Conan provides a mechanism to define those variables and make it possible, for executables, to find and load these shared libraries. This mechanism is the VirtualRunEnv generator.

If you check the output folder you will see that Conan generated a new file called conanrun.sh/bat. This is the result of automatically invoking that VirtualRunEnv generator when we activated the shared option when doing the conan install.

This generated script will set the PATH, LD_LIBRARY_PATH, DYLD_LIBRARY_PATH and DYLD_FRAMEWORK_PATH environment variables so that executables can find the shared libraries.

Activate the virtual environment, and run the executables again:

E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>conanrun.bat
E:\XXXXX\examples2\tutorial\consuming_packages\different_configurations\build>Release\compressor.exe
Uncompressed size is: 233
Compressed size is: 147
ZLIB VERSION: 1.2.11
Release configuration!


  • 21
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值