c++17 parallel algorithms and CMake

Viewed 287

It looks like more and more standard libraries implementations are relying on TBB for their parallel algorithms. This is a bit surprising to me, as I didn't think that standard libraries would have external dependencies (outside of stuff like pthread), but that's a different question I imagine.

My issue is that I need to bake this into my CMakeLists.txt files now.

First bad news: There is no official CMake support for TBB, and TBB itself does not provide any FindTBB.cmake file. You can find it here and there on the web, but if standard libraries start relying on it, it would be nice to have it officially supported by CMake. Is this coming further down the line?

Then, I need to have some slightly convoluted code in my CMakeLists.txt file to find_package(TBB REQUIRED) and link the corresponding targets when required (depending on the standard library, version, etc.). It looks like Conan is already offering a package that hides all that stuff from the user. You just get parallelstl and that's it. Will we have something similar in CMake in the future?

We can already use these parallel algorithms in CMake today, but it would be great to make it easier to create such projects.

1 Answers

I have Ubuntu 21.10 with GCC and TBB (libtbb-dev 2020.3-1) installed. Since TBB uses pkg-config what worked for me is this:

# file CMakeLists.txt

find_package(PkgConfig REQUIRED)
pkg_search_module(TBB REQUIRED tbb)
link_libraries(PkgConfig::TBB)

Unfortunately, when I later installed intel-oneapi 2021.4.0 which includes its own TBB (in /opt/intel/oneapi) this stopped working. This newer version of TBB cannot be used as a backed for GCC's (parallel) STL apparently (generates compilation errors in my system), so I did this in order to pick the system's TBB and not the /opt/intel version.

link_libraries(-ltbb) #(PkgConfig::TBB)

This defeats the purpose of CMake, so I am also looking for a more robust solution. My recommendation at the moment is do not install oneapi's TBB if what you want is to make it work with system's GCC.

Related