4

I'm considering to start using Conan package manager for managing third party dependencies of my C++ projects, but I cannot find how to link only to some of the libraries in Conan package. I'm using CMake as build system and I'm using Conan multi config CMake generator: cmake_multi.

Following the example in the documentation I have this code:

project(FoundationTimer)
cmake_minimum_required(VERSION 2.8.12)

set(TARGET_NAME timer)

include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
conan_basic_setup(TARGETS)

add_executable(${TARGET_NAME} timer.cpp)
target_link_libraries(${TARGET_NAME} CONAN_PKG::Poco)

in my CMakeLists.txt.

After generating solution with CMake:

conan install -g cmake_multi -s build_type=Debug -s compiler.runtime=MDd ..\mytimer\
conan install -g cmake_multi -s build_type=Release -s compiler.runtime=MD ..\mytimer\
cmake ..\mytimer\ -G "Visual Studio 14 2015 Win64"

this generates dependencies to all libraries in the Conan package and to all libraries in other Conan packages to which Poco Conan package depends like OpenSSL and zlib.

Additional dependencies in Visual Studio project options for debug configuration is set to:

C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoUtilmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoMongoDBmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoNetmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoNetSSLWinmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoCryptomdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoDatamdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoDataSQLitemdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoZipmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoXMLmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoJSONmdd.lib
C:\Users\ivan.bobev\.conan\data\Poco\1.7.8p3\pocoproject\stable\package\67348df82fcd362bbf088991f95bb229be582635\lib\PocoFoundationmdd.lib
C:\Users\ivan.bobev\.conan\data\OpenSSL\1.0.2l\conan\stable\package\b17b520b4b55729a7391c6b2d20631fec4cf1564\lib\ssleay32.lib
C:\Users\ivan.bobev\.conan\data\OpenSSL\1.0.2l\conan\stable\package\b17b520b4b55729a7391c6b2d20631fec4cf1564\lib\libeay32.lib
C:\Users\ivan.bobev\.conan\data\zlib\1.2.11\conan\stable\package\c32596dcd26b8c708dc3d19cb73738d2b48f12a8\lib\zlibd.lib

Is it possible to link only to specific libraries in Poco package?

I tried explicitly to list only the libraries to which I want to link the following way:

target_link_libraries(${TARGET_NAME}
  debug PocoFoundationmdd optimized PocoFoundationmd
  debug PocoUtilmdd optimized PocoUtilmd)

But after this the path to the lib files is not set properly, nor include directories for Poco package.

I'm using latest Conan version 0.25.1.

bobeff
  • 2,800
  • 1
  • 26
  • 51
  • 1
    One question: are you having a linker performance problem? I guess you know that the linker will not use unnecessary libraries, so unless your linkage step is becoming very slow and you need to really optimize this, you are good leaving the default libraries. – drodri Aug 23 '17 at 16:58

1 Answers1

3

I think the best could be to filter or define the necessary libraries, before calling the setup step. Something like:

include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
# Just the libraries you want
set(CONAN_LIBS_POCO PocoUtilmd PocoMongoDBmd PocoFoundationmd ws2_32 Iphlpapi.lib)
conan_basic_setup(TARGETS)

Note that there are some other system libraries and that the library names may vary in different OSs and for different configurations. So probably it is better to filter out (something like this, not tested):

include(${CMAKE_BINARY_DIR}/conanbuildinfo_multi.cmake)
set(_my_poco_libs)
foreach(_library IN ${CONAN_LIBS_POCO})
   if(NOT ${_library} MATCHES "yourRegexToDiscardUnwantedLibs")
       list(APPEND _my_poco_libs ${_library})
   endif()
enforeach()
set(CONAN_LIBS_POCO ${_my_poco_libs})
conan_basic_setup(TARGETS)

Note that the include() of the generated conanbuildinfo.cmake file, is "passive", it shouldn't do anything besides declaring CONAN_XXX variables. So you can manipulate them anyway you want before actually setting the build, which is what conan_basic_setup() does, it translate those variables to cmake.

In any case, unless you are having a linking performance issue, you are probably fine leaving all the Poco libs there, they will not be linked unless they are really necessary for the final executable.

drodri
  • 3,796
  • 10
  • 15
  • Is this solution still applicable to Conan 1.27.1? I'm working with a wt/4.3.1 package that contains several libraries, some of which conflict with each other, namely: `wthttp` and `wttest`. I would like to exclude `wttest` from `${CONAN_LIBS}`. How could I achieve that? – IvanR Jul 16 '20 at 14:44
  • 1
    Conan recipes have learned [components](https://docs.conan.io/en/latest/creating_packages/package_information.html#using-components) in the latest Conan 1.26 (cmake_find_package generator) and Conan 1.27 (cmake_find_package_multi generator). It might take a few weeks until ConanCenter recipes adopt them, but as soon as they are do, it will be possible to link directly with targets like ``Poco::crypto``. – drodri Jul 17 '20 at 22:06
  • Is there anything users can do to speed up the adoption of components by packages in ConanCenter that they use but don't own? I.e. is it possible to open something like a PR somewhere? (Sorry, new to Conan/JFrog/etc so just testing the ground in hope of speeding things up) – IvanR Jul 18 '20 at 17:30
  • Yes, the https://github.com/conan-io/conan-center-index repo is still in EAP, but any user that applies to the program can contribute to any existing package, submit new versions or completely new packages. All packages in ConanCenter are created automatically from that repo. – drodri Jul 19 '20 at 20:42