Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link only to some of the libraries in Conan package?

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.

like image 824
bobeff Avatar asked Oct 17 '22 07:10

bobeff


1 Answers

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.

like image 137
drodri Avatar answered Nov 15 '22 06:11

drodri