Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to include boost sublibs #17711

Open
1 task done
omgronny opened this issue Feb 5, 2025 · 6 comments
Open
1 task done

[question] How to include boost sublibs #17711

omgronny opened this issue Feb 5, 2025 · 6 comments
Assignees
Labels
responded Responded by Conan team type: question

Comments

@omgronny
Copy link

omgronny commented Feb 5, 2025

What is your question?

Hello, I'm trying to migrate my project from conan1.x to conan2

In conan1.x I had in my CMakeLists.txt

set(CONAN_LIBS_BOOST "iostreams;filesystem;program_options;container;atomic;exception")

foreach (BOOST_COMPONENT ${CONAN_LIBS_BOOST})
  MESSAGE("Add boost component ${BOOST_COMPONENT}")
  add_library(CONAN_PKG::boost::${BOOST_COMPONENT} ALIAS boost_boost_${BOOST_COMPONENT})
endforeach()

find_package(Boost REQUIRED)

add_library(CONAN_PKG::boost::container ALIAS boost::boost::container)
add_library(CONAN_PKG::boost::beast ALIAS boost::boost::beast)
add_library(CONAN_PKG::boost::algorithm ALIAS boost::boost::algorithm)

but now I have to replace it with

find_package(Boost REQUIRED)

find_package(Boost REQUIRED)

target_include_directories(Boost::boost INTERFACE "${boost_PACKAGE_FOLDER_RELEASE}/include")

Otherwise it fails with

CMake Error at cmake/conan/conan.cmake:47 (add_library):
  add_library cannot create ALIAS target "CONAN_PKG::boost::iostreams"
  because target "boost_boost_iostreams" does not already exist.
...

There is two questions I have:

  1. Is it possible not to add the whole boost and add only the required sublibs?
  2. Without the last line in the second snippet, my project uses system-installed boost version instead of the one written in my conanfile.py. Is this line required or I', doing something wrong?

Thank you

Have you read the CONTRIBUTING guide?

  • I've read the CONTRIBUTING guide
@memsharded
Copy link
Member

Hi @omgronny

Something like

target_include_directories(Boost::boost INTERFACE "${boost_PACKAGE_FOLDER_RELEASE}/include")

isn't necessary and it is probably incorrect in some cases

Also, as commented in the other thread as add_library(CONAN_PKG::boost::container isn't recmmended, the CONAN_PKG thing was a legacy thing. If you read the output of conan install it will tell you what you need:

find_package(Boost)  # Or something like that

target_link_libraries(... Boost::boost) # or other target name

The generated Boost::boost target already contains everything, including the headers directories, this is why target_include_directories shouldn't be used.

@omgronny
Copy link
Author

omgronny commented Feb 6, 2025

Thank you for your answer, target_link_libraries(... Boost::boost) works

But my problem is actually that when I delete target_include_directories line, my program starts to use boost from /usr/include/boost, and fails due to incorrect system boost version.

When I return this line, it starts to use boost from conan package with proper version

@memsharded
Copy link
Member

If that is the case, it might be even possible that the Boost that is being linked is also the system one.

This is typically the result of resolution priorities. Using the Conan generated conan_toolchain.cmake file (are you using it?) one of the things that it tries to do is to define the right priorities, so the Conan packages are found first and not the system things.

Also, using find_package(Boost CONFIG REQUIRED) might help, as it avoids using legacy FindBoost.cmake modules, and forces to require a modern BoostConfig.cmake config file, which is better prioritized.

@omgronny
Copy link
Author

omgronny commented Feb 7, 2025

Thank you for your answer
I tried to remove some target_link_libraries(... Boost::boost) and found out that my program still compiling (and starts to use system boost I guess)

I've checked the BoostConfig.cmake file:

$ cat BoostConfig.cmake                                                                                                           1 ↵  ✹ ✭evgenyz/conan2
########## MACROS ###########################################################################
#############################################################################################

# Requires CMake > 3.15
if(${CMAKE_VERSION} VERSION_LESS "3.15")
    message(FATAL_ERROR "The 'CMakeDeps' generator only works with CMake >= 3.15")
endif()

if(Boost_FIND_QUIETLY)
    set(Boost_MESSAGE_MODE VERBOSE)
else()
    set(Boost_MESSAGE_MODE STATUS)
endif()

include(${CMAKE_CURRENT_LIST_DIR}/cmakedeps_macros.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/BoostTargets.cmake)
include(CMakeFindDependencyMacro)

check_build_type_defined()

foreach(_DEPENDENCY ${boost_FIND_DEPENDENCY_NAMES} )
    # Check that we have not already called a find_package with the transitive dependency
    if(NOT ${_DEPENDENCY}_FOUND)
        find_dependency(${_DEPENDENCY} REQUIRED ${${_DEPENDENCY}_FIND_MODE})
    endif()
endforeach()

set(Boost_VERSION_STRING "1.83.0")
set(Boost_INCLUDE_DIRS ${boost_INCLUDE_DIRS_RELEASE} )
set(Boost_INCLUDE_DIR ${boost_INCLUDE_DIRS_RELEASE} )
set(Boost_LIBRARIES ${boost_LIBRARIES_RELEASE} )
set(Boost_DEFINITIONS ${boost_DEFINITIONS_RELEASE} )


# Only the last installed configuration BUILD_MODULES are included to avoid the collision
foreach(_BUILD_MODULE ${boost_BUILD_MODULES_PATHS_RELEASE} )
    message(${Boost_MESSAGE_MODE} "Conan: Including build module from '${_BUILD_MODULE}'")
    include(${_BUILD_MODULE})
endforeach()

And noticed one thing: variables Boost_INCLUDE_DIRS and Boost_INCLUDE_DIR is actually not set when running CMakeLists.txt

My conanfile.py:

    def requirements(self):
        ...
        self.requires("boost/1.83.0", override=True, transitive_headers=True)
        self.requires("boost::headers/1.83.0", override=True, transitive_headers=True)
        ...

May it be relative to the problem I have?

I also tried find_package(Boost 1.83.0 CONFIG REQUIRED), but behavior is still the same

It is important for me to make sure that I use proper boost, because I wanna make sure I don't link the whole boost in some targets in order to decrease compilation time

@memsharded
Copy link
Member

memsharded commented Feb 7, 2025

self.requires("boost::headers/1.83.0", override=True, transitive_headers=True)

This is not a thing, the boost::headers package doesn't exist and it will error, as it contains invalid characters, so I am not sure what the issue is.

Also, the override=True trait doesn't really introduce a dependency to Boost, unless there is some other package uptream that depends on boost.

So there seems to be some misconceptions and errors there.

I'd suggest to strip down your case as we did in the other example, to achieve full reproducibility:

  • Start with a conan new cmake_lib -d requires=boost/1.83.0
  • Change the CMakeLists.txt to something like find_package(Boost) and target_link_libraries(...., Boost::boost)
  • Change the #include "boost.h" to an include that makes sense and the call to boost() to something like makes sense.
  • Do conan build . and report.

@memsharded memsharded self-assigned this Feb 7, 2025
@memsharded
Copy link
Member

Hi @omgronny

Any further feedback here? Did you manage to include boost?
If not, please try to make a stripped down report with the steps suggested above, thanks for the feedback.

@memsharded memsharded added the responded Responded by Conan team label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
responded Responded by Conan team type: question
Projects
None yet
Development

No branches or pull requests

2 participants