Search code examples
c++cmakelinkerldtbb

Linking to TBB libraries with CMake


I have tbb downloaded and placed in my repository directory:

> tree deps/tbb/ -d 
deps/tbb/
├── bin
├── cmake
│   └── templates
├── include
│   ├── serial
│   │   └── tbb
│   └── tbb
│       ├── compat
│       ├── internal
│       └── machine
└── lib
    ├── ia32
    │   └── gcc4.8
    └── intel64
        └── gcc4.8

In my CMakeLists.txt I have tried this:

include_directories("deps/tbb/include")


find_library(TBB_LIB
    NAMES
    tbbbind_debug
    tbbbind
    tbb_debug
    tbbmalloc_debug
    tbbmalloc_proxy_debug
    tbbmalloc_proxy
    tbbmalloc
    tbb_preview_debug
    tbb_preview
    tbb
    HINTS "${CMAKE_PREFIX_PATH}/deps/tbb/lib/intel64/gcc4.8"
)

add_executable(${PROJECT_NAME}
src/main.cpp
)

target_link_libraries(${PROJECT_NAME} PUBLIC ${TBB_LIB})

But building with cmake, linker throws this error:

/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lTBB_LIB-NOTFOUND

collect2: error: ld returned 1 exit status

I couldn't figure out what is missing. Thanks.

Update

This commit resolves the previous error:

-    HINTS "${CMAKE_PREFIX_PATH}/deps/tbb/lib/intel64/gcc4.8"
+    HINTS "deps/tbb/lib/intel64/gcc4.8"

But, new errors are thrown:

undefined reference to `tbb::interface7::internal::task_arena_base::internal_current_slot()'

Update

Other than find_library, what CMake tools are available to link to TBB shared libraries?

I have tried some CMake tools, but I cannot figure out how to link to TBB *.so files correctly!


Solution

  • Inspired by @AlexReinking answer, here is the final implementation:

        project(my-cpp-service VERSION 0.1.0)
    
        # Equivalent to command-line option of `-DCMAKE_PREFIX_PATH=...`
        list(APPEND CMAKE_MODULE_PATH "deps/tbb/cmake/")
    
        find_package(TBB REQUIRED)
    
        add_executable(${PROJECT_NAME}
            src/main.cpp
        )
    
        target_link_libraries(${PROJECT_NAME} PUBLIC
            TBB::tbb
        )