Search code examples
cmakecmake-modulescmake-language

Why don't I need add boost::shared_ptr to target_link_libraries when linking to Boost::filesystem?


Recently, I was learning CMake using ttroy50/cmake-examples. But when I learning H-third-party-library, I meet a problem! This tutorial said we can use find_package function to find third party libraries. We have the following main.cpp:

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>

int main(int argc, char *argv[])
{
    std::cout << "Hello Third Party Include!" << std::endl;

    // use a shared ptr
    boost::shared_ptr<int> isp(new int(4));

    // trivial use of boost filesystem
    boost::filesystem::path path = "/usr/share/cmake/modules";
    if(path.is_relative())
    {
        std::cout << "Path is relative" << std::endl;
    }
    else
    {
        std::cout << "Path is not relative" << std::endl;
    }

   return 0;
}

In this souce file, we using boost/shared_ptr and boost/filesystems. But we have the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

# Set the project name
project (third_party_include)


# find a boost install with the libraries filesystem and system
find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)

# check if boost was found
if(Boost_FOUND)
    message ("boost found")
else()
    message (FATAL_ERROR "Cannot find Boost")
endif()

# Add an executable
add_executable(third_party_include main.cpp)

# link against the boost libraries
target_link_libraries( third_party_include
    PRIVATE
        Boost::filesystem
)

We can correctly build and run! But why don't we need add Boost::shared_ptr to CMakeLists.txt's target_link_libraries? We also use it!!!

And where can I know which alias can I use for a Boost library, such as Boost::filesystem?

I want to know why I don't need add Boost::shared_ptr to target_link_libraries.

Update 2023-4-18

I have another question. In above CMakeLists.txt file I have not include boost header file directories. But it work normally.

Is this because the header file directories path is /usr/include (GCC will default search header file in this directories )? Or because the imported target(Boost::filesystem) will add the header directories Path to my targetthird_party_include in the function target_link_libraries?


Solution

  • boost::shared_ptr is part of the Boost smart_ptr library. So I assume you'd target_link_libraries to Boost::smart_ptr (and not to some Boost::shared_ptr). I'm assuming the Boost CMake library target names are derived (lower-snake-case) from the names of the libraries themselves. See the FindBoost docs' section on the imported targets- in particular, Boost::<component>. I'm guessing the reason why it worked without you doing anything might have to do with the include directories for Boost Filesystem and Boost Smart Ptr being the same in your installation of Boost, and with Boost Smart Ptr being header-only (see also Which Boost libraries are header-only?).