Linking to TBB libraries with CMake
Asked Answered
S

3

7

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!

Saberhagen answered 22/9, 2021 at 10:24 Comment(0)
S
7

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
    )
Saberhagen answered 22/2, 2022 at 9:52 Comment(0)
N
15

TBB has native CMake support. On my system with the Intel oneAPI installed, the config package is installed here:

/opt/intel/oneapi/tbb/latest/lib/cmake/tbb/TBBConfig.cmake

Therefore, I just need to add /opt/intel/oneapi/tbb/latest to my CMAKE_PREFIX_PATH. In my CMakeLists.txt, I wrote this:

cmake_minimum_required(VERSION 3.21)
project(test-tbb)

find_package(TBB REQUIRED)

add_library(main main.cpp)
target_link_libraries(main PRIVATE TBB::tbb)
target_compile_features(main PRIVATE cxx_std_11)

TBB provides the IMPORTED target TBB::tbb, which is what you should link to.

main.cpp is just the source from here: https://mcmap.net/q/569686/-simplest-tbb-example

Build like so:

$ cmake -G Ninja -S . -B build -DCMAKE_PREFIX_PATH=/opt/intel/oneapi/tbb/latest
$ cmake --build build --verbose
[1/2] /usr/bin/c++  -isystem /opt/intel/oneapi/tbb/2021.3.0/include  -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/alex/test2/main.cpp
[2/2] : && /usr/bin/cmake -E rm -f libmain.a && /usr/bin/ar qc libmain.a  CMakeFiles/main.dir/main.cpp.o && /usr/bin/ranlib libmain.a && :
Nepos answered 22/9, 2021 at 20:16 Comment(0)
S
7

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
    )
Saberhagen answered 22/2, 2022 at 9:52 Comment(0)
S
-1

This post helped me solved the problem:

https://mcmap.net/q/135489/-cmake-link-to-external-library

The errors got resolved by:

include_directories("deps/tbb/include")

# https://mcmap.net/q/135489/-cmake-link-to-external-library
find_library(LIB_TBB                NAMES tbb               HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBbind            NAMES tbbbind           HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBmalloc_proxy    NAMES tbbmalloc_proxy   HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBmalloc          NAMES tbbmalloc         HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBB_preview        NAMES tbb_preview       HINTS "deps/tbb/lib/intel64/gcc4.8")

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

target_link_libraries(${PROJECT_NAME} PUBLIC
${LIB_TBB}
${LIB_TBBbind}
${LIB_TBBmalloc_proxy}
${LIB_TBBmalloc}
${LIB_TBB_preview}
)

Saberhagen answered 22/9, 2021 at 11:5 Comment(2)
No. Do not do this. Use find_package and imported targets.Nepos
@AlexReinking Alright =) Thanks!Saberhagen

© 2022 - 2024 — McMap. All rights reserved.