Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install the C++20 range library from github

I would like to use the range-v3 library in my project, but i don't understand how. The installation description says the following:

This library is header-only. You can get the source code from the range-v3 repository on github. To compile with Range-v3, just #include the individual headers you want.

Does that mean I can copy and paste the needed header files and add the filepath to my CMake file? I am a bit confused, because I never included third party library.

like image 872
M.Mac Avatar asked May 15 '26 16:05

M.Mac


2 Answers

Note: please see hythis' answer for a better solution.


Does that mean I can copy and paste the needed header files and add the filepath to my CMake file?

Basically, yes. First git clone to <path_to_range_v3>. Then include these lines into CMakeLists.txt:

add_library(range_v3 INTERFACE IMPORTED)
set_target_properties(range_v3 PROPERTIES 
    INTERFACE_INCLUDE_DIRECTORIES <path_to_range_v3>/include)

target_link_libraries(your_target PUBLIC range_v3)
like image 75
Evg Avatar answered May 18 '26 05:05

Evg


I'm not sure why Evg suggested what they did, I don't even think in 2019 you were forced to create your own CMake interface with ranges-v3 (see here, the file existed way before hand). Regardless, don't use Evg's solution, ranges-v3 is a good header only library, and in order for a header only library to be good it must provide CMake integration.

Header only libraries do not mean the authors do not provide CMake support, or even avoid CMake themselves. Bad authors do this, as you've probably experienced by having to ask this question in the first place.

To properly integrate with Ranges V3, use a packagemanager (though some package managers screw the process up depending on how their custom CMake files are configured) such as Conan or vcpkg and integrate with their respective CMake solutions, or add the project as a git submodule (though you could git clone it as well) then in your CMakeLists.txt:

add_subdirectory([path to submodules]/range-v3)
...
target_link_libraries(my_target [SCOPE] range-v3::range-v3)

It can be hard to figure out actual targets for libraries if the authors don't spell out how to use their projects in a CMake project, and I don't blame any one making an SO post about it, it's a pain.

Generally if the project has a CMakeLists.txt file, it probably has static-library/sub_directory cmake integration, but some libraries only have install targets, thus are unusable when baked into your source code with out editing their CMakeLists.txt

To check if this is the case, or if you can actually use the targets, if you can't find any CMake documentation about how to use the library (which I couldn't) do the following:

  • Look inside the CMakeLists.txt file
  • Find a project alias usually in the form of project_name::project_name. This is how I found range-v3's project alias.
  • If you can't find alias (via searching for ::), find the actual target name for the library (and try to use this directly), sometimes this isn't exported though (hidden in a sub directory from the top cmake file). OpenCV doesn't use :: for example, and instead each component target is marked with opencv_[component name] but still exported.

If the project has not been configured to be properly used as a submodule and is otherwise meant to be used as a static library, submit an issue or PR to the given repository, this is a bug.

like image 36
Krupip Avatar answered May 18 '26 05:05

Krupip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!