使用 CMake 链接到 TBB 库

Linking to TBB libraries with CMake

我已经 tbb 下载并放置在我的存储库目录中:

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

在我的 CMakeLists.txt 中,我试过这个:

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})

但是使用 cmake、linker 构建会抛出此错误:

/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

我不知道缺少了什么。谢谢。

更新

这次提交解决了之前的错误:

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

但是,抛出了新的错误:

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

更新

除了 find_library,TBB 共享库 link 还可以使用哪些 CMake 工具?

我尝试了一些 CMake 工具,但我不知道如何正确地 link 到 TBB *.so 文件!

这个post帮我解决了问题:

错误已解决:

include_directories("deps/tbb/include")

# 
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}
)

TBB 具有原生 CMake 支持。在我安装了 Intel oneAPI 的系统上,配置包安装在这里:

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

因此,我只需要将 /opt/intel/oneapi/tbb/latest 添加到我的 CMAKE_PREFIX_PATH。在我的 CMakeLists.txt 中,我这样写:

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 提供了 IMPORTED 目标 TBB::tbb,这是您应该 link 的目标。

main.cpp 只是来自这里的来源:

像这样构建:

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

受@AlexReinking 回答的启发,这里是最终实现:

    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
    )