编译英特尔 TBB 库的问题

Problems with compiling Intel TBB library

编译项目时,我遇到大约 20 个错误,这些错误都与 TBB 库有关。当我用 VS2015 编译项目时,我没有得到任何错误。关于 Emsctipten,它看起来像这样。这会是什么?非常感谢您的帮助。

// WITH EMCC

// C:/Users/Admin/Documents/EMC/OpenFace>emcc FaceLandmarkVid.cpp -o OpenFace.js

// C:/Users/Admin/Documents/EMC/OpenFace/3rdParty/tbb/include/tbb/tbb_machine.h:332:6: // error: Unsupported
//      machine word size.
//    #error Unsupported machine word size.

// Code from tbb_machine.h Line: 328 - 333 //

// #define __TBB_CompareAndSwapW(P,V,C)    __TBB_machine_cmpswp8(P,V,C)
// #define __TBB_FetchAndAddW(P,V)         __TBB_machine_fetchadd8(P,V)
// #define __TBB_FetchAndStoreW(P,V)       __TBB_machine_fetchstore8(P,V)
//#else /* __TBB_WORDSIZE != 8 */
//    #error Unsupported machine word size.
//#endif /* __TBB_WORDSIZE */
    
//   20 errors generated.



// WITH CLANG //

// C:/Users/Admin/Documents/EMC/OpenFace>clang++ FaceLandmarkVid.cpp -o OpenFace.js

// C:/Users/Admin/Documents/EMC/OpenFace/3rdParty/tbb/include/tbb/internal
//   /_tbb_windef.h:32:2: error: TBB
//      requires linkage with multithreaded C/C++ runtime library. Choose multithreaded // DLL runtime in project
//      settings, or use /MD[d] compiler switch.
// #error TBB requires linkage with multithreaded C/C++ runtime library. \

//   28 warnings and 1 error generated.

您好,还没有在我的 machine 上测试过这个,但您需要查看更广泛的上下文(查看来自 https://github.com/jckarter/tbb/blob/master/include/tbb/tbb_machine.h#L285 的 TTB 代码):

#if __TBB_WORDSIZE==4
    #define __TBB_CompareAndSwapW(P,V,C)    __TBB_machine_cmpswp4(P,V,C)
    #define __TBB_FetchAndAddW(P,V)         __TBB_machine_fetchadd4(P,V)
    #define __TBB_FetchAndStoreW(P,V)       __TBB_machine_fetchstore4(P,V)
#elif  __TBB_WORDSIZE==8
    #if __TBB_USE_GENERIC_DWORD_LOAD_STORE || __TBB_USE_GENERIC_DWORD_FETCH_ADD || __TBB_USE_GENERIC_DWORD_FETCH_STORE
        #error These macros should only be used on 32-bit platforms.
    #endif

    #define __TBB_CompareAndSwapW(P,V,C)    __TBB_machine_cmpswp8(P,V,C)
    #define __TBB_FetchAndAddW(P,V)         __TBB_machine_fetchadd8(P,V)
    #define __TBB_FetchAndStoreW(P,V)       __TBB_machine_fetchstore8(P,V)
#else /* __TBB_WORDSIZE != 8 */
    #error Unsupported machine word size.
#endif /* __TBB_WORDSIZE */

在 TTB github 目录中快速搜索显示此文件 (https://github.com/jckarter/tbb/blob/0343100743d23f707a9001bc331988a31778c9f4/include/tbb/tbb_machine.h#L78):

Prerequisites for each architecture port
    ----------------------------------------
    The following functions and macros have no generic implementation. Therefore they must be
    implemented in each machine architecture specific header either as a conventional
    function or as a functional macro.
    __TBB_WORDSIZE
        This is the size of machine word in bytes, i.e. for 32 bit systems it
        should be defined to 4.

所以我的猜测是,在不投入太多时间的情况下,TTB 中的某处有一个特定于平台的代码,它设置了适合该值的 __TBB_WORDSIZE平台...

因此,对于 Emscripten,您可以在代码中添加以下代码段:

#ifdef __EMSCRIPTEN__
    #define __TBB_WORDSIZE 4
#endif

这应该可以帮助您解决下一个编译错误。祝你好运!

编辑 1:

如何解决以下错误?

您需要了解的是,Cpp 大部分是可移植的,但任何处理底层细节的代码都需要 platform-specific 实现细节(这可能是显而易见的,但它也可能对未来的读者有所帮助)。理想情况下,platform-specific 代码保持在最低限度,并且全部放在一个地方。

Pre-processor 变量用于检测 architecture/compiler 用于编译代码并定义一些 platform-specific 细节。在 TTB 中,典型的平台检测 pre-processor 变量是 #if __MINGW64__ || __MINGW32__#elif _XBOX#elif __linux__ || __FreeBSD__ || __NetBSD__,一些样本 platform-specific 的细节是 TBB_USE_GCC_BUILTINS__TBB_WORDSIZE, __TBB_Yield()...

回到 TTB,快速搜索显示平台抽象层在 tbb_machine.h 中,您应该阅读文件顶部的评论,可在此处获取: https://github.com/jckarter/tbb/blob/0343100743d23f707a9001bc331988a31778c9f4/include/tbb/tbb_machine.h#L32

确保您理解每个体系结构端口的先决条件部分。在本节中,您将了解到需要定义几个 macross,列表中的第一个是 __TBB_WORDSIZE__TBB_Yield()__TBB_full_memory_fence() 等等。

最后,文件夹 tbb/include/tbb/machine/ 包含几个 platform-specific 头文件(gcc、mac、xbox...)。您很可能也需要为 emscripten 创建一个。执行此操作时,请记住它是使用 Clang(gcc-compatible)编译并使用 32 位架构。

您如何才能更轻松地获得帮助、帮助世界其他地方、获得良好的人缘并炫耀您不断增长的 C++/Emscripten 专业知识?

Fork Github 上的存储库。创建一个 Emscripten 分支并使其工作(或者至少尽可能地工作)。使用 Emscripten 构建的确切说明更新自述文件。

当你遇到困难时,请在 SO 上再问一个问题。但是这一次,link 到您的 github 存储库。如果我需要 2 分钟才能在 machine 上重现错误,我更有可能尝试并为您修复一些错误。

即使您永远无法完全移植它,您的努力也会公开,其他人可能会在此基础上积累。

如果你能完成它,那就太好了!您现在是 open-source 贡献者。
您的下一步是打开拉取请求以将其合并回主 TTB 存储库。然后,您的补丁将由 Intel 的一些经验丰富的工程师审查,他们会指出您 code/approach 中的潜在问题,您将通过这样做学到很多东西。

顺便说一句,将此贡献回主库对您的雇主也有好处。一旦它被合并到主存储库中,他们将随着库的发展维护该端口。其他人更有可能使用它并改进它。

最后,几年后你可能想换工作并接受一些面试。当被问及您的经验和能力时,以下内容应该可以帮助您脱颖而出:

3 years ago I ported Intel TTB's library to Emscripten. This was harder than I originally anticipated but I eventually made it work. The patch was then reviewed by 2 senior engineers at Intel and incorporated into the library after the code review. The feedback was humbling but it helped me internalise best practices that I still use today and help increasing the quality of my work.

希望编辑对您有所帮助。其中一些对您来说可能微不足道,但希望它也能对未来的读者有所帮助。如果它说服一个人开始回馈 open-source,那将让我开心 :)