简体   繁体   English

如何使用系统提供的较新版本的clang链接到线程消毒剂?

[英]How can I link to thread sanitizer with a newer version of clang than the system provides?

I download normally the latest clang version from their website . 我通常从他们的网站上下载最新的clang版本。 This helps me use the latest version of C++ as doing this with gcc is not really possible. 这可以帮助我使用最新版本的C ++,因为用gcc这样做实际上是不可能的。 I just get the binaries for my Ubuntu/Debian and I'm good to go. 我刚刚得到了我的Ubuntu / Debian的二进制文件,我很高兴。

For me, linking with tsan library (thread-sanitizer library) has never been something simple. 对我来说,与tsan库(线程清理程序库)链接从未如此简单。 I use insane measures in cmake to make it work. 我在cmake中使用了疯狂的措施使其工作。 Previously, when I used gcc from the system, this is what I did in cmake to make the link work correctly: 以前,当我从系统中使用gcc时,这就是我在cmake中所做的,以使链接正常工作:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -ltsan")
set(CMAKE_LINK_LIBRARY_FLAG "-ltsan -l")

which basically modifies the linking flags to link every little thing with tsan . 它基本上修改了链接标志,将每个小东西与tsan链接。 This has worked fine for a while, but to make it work, I should use gcc, the system's compiler. 这已经工作了一段时间了,但是要使其正常工作,我应该使用系统的编译器gcc。 If I try to link like this with clang 7, I get a segfault when I run my program. 如果我尝试使用clang 7链接,则在运行程序时会出现段错误。

So I searched for the available tsan libraries that come with clang, and here's what I found: 因此,我搜索了clang附带的可用tsan库,这是我发现的内容:

user@machine:/opt/clang7$ find -iname "*tsan*"
./lib/clang/7.0.0/lib/linux/libclang_rt.tsan_cxx-x86_64.a
./lib/clang/7.0.0/lib/linux/libclang_rt.tsan-x86_64.a
./lib/clang/7.0.0/lib/linux/libclang_rt.tsan_cxx-x86_64.a.syms
./lib/clang/7.0.0/lib/linux/libclang_rt.tsan-x86_64.a.syms
./lib/clang/7.0.0/include/sanitizer/tsan_interface_atomic.h
./lib/clang/7.0.0/include/sanitizer/tsan_interface.h

There seems to be tsan libraries there. 那里似乎有tsan库。 I guess I gotta link to them. 我想我必须链接到他们。 How do I do that? 我怎么做?

This, doesn't seem to work: 这似乎不起作用:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -L/opt/clang7/lib/clang/7.0.0/lib/linux/ -lclang_rt.tsan_cxx-x86_64")
set(CMAKE_LINK_LIBRARY_FLAG "-L/opt/clang7/lib/clang/7.0.0/lib/linux/ -lclang_rt.tsan_cxx-x86_64 -l")

This doesn't work either: 这也不起作用:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread -l:/opt/clang7/lib/clang/7.0.0/lib/linux/libclang_rt.tsan_cxx-x86_64.a")
set(CMAKE_LINK_LIBRARY_FLAG "-l:/opt/clang7/lib/clang/7.0.0/lib/linux/libclang_rt.tsan_cxx-x86_64.a -l")

I've tried a few other combinations. 我尝试了其他几种组合。 But None of them seem to work. 但是它们似乎都不起作用。 The errors I get are either linking errors or undefined references to some tsan components. 我得到的错误是链接错误或对某些tsan组件的未定义引用。

How can I link to tsan from the newest clang's prebuilt binaries? 如何从最新的clang的预编译二进制文件链接到tsan?

Setting the link flag for the compilation is a big no-no: 为编译设置链接标志是一个很大的禁忌:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")

Then you need to do the same for the link flags as well: 然后,您还需要对链接标志执行相同的操作:

SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread") 
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=thread")

You can also change the target properties only: 您还可以仅更改目标属性:

set_target_properties(${TARGET} PROPERTIES
    LINK_FLAGS -fsanitizer=thread
    COMPILE_FLAGS -fsanitizer=thread)

Be aware that this overrides all flags (I don't remember if CMAKE_CXX_FLAGS are also there, maybe not), you may want to retrieve the current ones and append these instead of removing everything. 请注意,这将覆盖所有标志(我不记得CMAKE_CXX_FLAGS是否也存在,也许不存在),您可能想检索当前标志并将其附加,而不是删除所有标志。

clang knows where its support libraries for sanitation are (as you said, these are tagged with a triple-like information and are not in the usual library folders, to avoid any contamination from other installs), and the full fsanitize=tsan flag will make it pick up these versions. clang知道其卫生支持库在哪里(如您所说,这些库带有类似三重的信息标记,并且不在通常的库文件夹中,以避免其他安装造成的污染),并且完整的fsanitize=tsan标志将使它会选择这些版本。 Note that it's not -ltsan , but a full option that will make clang pick up the proper backend in the path where its own sanitizers are. 请注意,它不是-ltsan ,而是一个完整的选项,它将使clang在其自身的消毒剂所在的路径中拾取适当的后端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM