Search code examples
c++gcccmakelinkertbb

Getting tbb linker errors when including the <execution> header


I have been writing a visualisation tool using OpenGL. It has been compiling and linking just fine (using gcc 11.2.0) until I recently installed the Linux dependencies for OpenVDB (listed under Using UNIX apt-get). I am now getting linker errors that I have narrowed down to the inclusion of the <execution> header file:

/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::execution_slot(tbb::detail::d1::execution_data const&)':
main.cpp:(.text._ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE[_ZN3tbb6detail2d114execution_slotERKNS1_14execution_dataE]+0x18): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'
/usr/bin/ld: CMakeFiles/test.dir/main.cpp.o: in function `tbb::detail::d1::current_thread_index()':
main.cpp:(.text._ZN3tbb6detail2d120current_thread_indexEv[_ZN3tbb6detail2d120current_thread_indexEv]+0x12): undefined reference to `tbb::detail::r1::execution_slot(tbb::detail::d1::execution_data const*)'

The above is from the following minimal example.

main.cpp:

#include <execution>
#include <iostream>

int main()
{
   std::cout << "Hello, World!" << std::endl;
   return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)
project(test)

set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)

add_executable(test main.cpp)

I have tried linking TBB with my project like so:

# Find and link TBB.
find_package(TBB REQUIRED)
include_directories(${TBB_INCLUDE_DIRS})
link_directories(${TBB_LIBRARY_DIRS})
add_definitions(${TBB_DEFINITIONS})
if(NOT TBB_FOUND)
    message("Error: TBB not found")
endif(NOT TBB_FOUND)

add_executable(test main.cpp)
target_link_libraries(test ${TBB_LIBRARIES})

...and also adding the -ltbb linker flag (as per the answer to this post) using

set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -ltbb")

However, this does not solve the issue.

What I find particularly strange is that I didn't use to have to link against tbb despite having included the <excution> header all along. Only after installing the OpenVDB dependencies has this become an issue.

Can anyone advise me on how to solve this (either by appropriately linking tbb, or not having to link it at all, as was the case before)? Any form of insight would be much appreciated.


Solution

  • I have managed to successfully link tbb as follows:

    find_package(TBB REQUIRED COMPONENTS tbb)
    
    add_executable(test main.cpp)
    target_link_libraries(test tbb)
    

    Although I am still clueless as to why I suddenly needed to link with tbb, considering I previously didn't need to.