Home > Net >  Linking to TBB libraries with CMake
Linking to TBB libraries with CMake

Time:09-23

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!

CodePudding user response:

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://stackoverflow.com/a/36782794/2137996

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 && :

CodePudding user response:

This post helped me solved the problem:

https://stackoverflow.com/a/41909627/3405291

The errors got resolved by:

include_directories("deps/tbb/include")

# https://stackoverflow.com/a/41909627/3405291
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}
)

  • Related