clang似乎没有链接到库

时间:2015-09-09 00:48:14

标签: c++ c++11 clang++

我将问题归结为以下示例:

int main()
{
    try {
        throw false;
    } catch (bool x)
    {
        if (x)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }
}

Coliru上生成以下错误:

/tmp/main-c8b47a.o: In function `main':
main.cpp:(.text+0xf): undefined reference to `typeinfo for bool'
/tmp/main-c8b47a.o: In function `GCC_except_table0':
main.cpp:(.gcc_except_table+0x30): undefined reference to `typeinfo for bool'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Cmd line:

  

clang ++ -std = c ++ 11 -stdlib = libc ++ -O2 -Wall -pedantic -pthread main.cpp&& ./a.out

这听起来好像没有链接到图书馆。有谁知道命令行切换的内容和内容是什么?我之前没有使用过clang。这适用于g ++。

这是带-v开关的输出:

clang version 3.6.0 (tags/RELEASE_360/final 235480)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.1.0
Found candidate GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0
Selected GCC installation: /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/usr/local/bin/clang" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model static -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.22 -momit-leaf-frame-pointer -v -dwarf-column-info -resource-dir /usr/local/bin/../lib/clang/3.6.0 -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /usr/local/bin/../lib/clang/3.6.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wall -pedantic -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /tmp/1441759762.34715 -ferror-limit 19 -fmessage-length 0 -pthread -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o /tmp/main-47c098.o -x c++ main.cpp
clang -cc1 version 3.6.0 based upon LLVM 3.6.0 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/v1
 /usr/local/include
 /usr/local/bin/../lib/clang/3.6.0/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/crtbegin.o -L/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0 -L/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../../../lib64 -L/usr/local/bin/../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/../../.. -L/usr/local/bin/../lib -L/lib -L/usr/lib /tmp/main-47c098.o -lc++ -lm -lgcc_s -lgcc -lpthread -lc -lgcc_s -lgcc /usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o
/tmp/main-47c098.o: In function `main':
main.cpp:(.text+0xf): undefined reference to `typeinfo for bool'
/tmp/main-47c098.o: In function `GCC_except_table0':
main.cpp:(.gcc_except_table+0x30): undefined reference to `typeinfo for bool'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:6)

看起来您需要在main.cpp( see it live )之后添加-lsupc++

clang++ -std=c++11  -stdlib=libc++ -O2 -Wall -pedantic -pthread main.cpp -lsupc++
                                                                         ^^^^^^^^

正如安德烈·科斯特尔所指出的那样libc++ documentation推荐以下内容,尽管我似乎无法在Coliru上使用它:

  

不幸的是,你不能简单地用&#34; -stdlib = libc ++&#34;在这   因为clang被设置为链接libc ++链接到libsupc ++。至   解决这个问题,您必须自己设置链接器(或补丁   铛)。例如,

     
      
  • clang ++ -stdlib = libc ++ helloworld.cpp -nodefaultlibs -lc ++ -lcxxrt -lm -lc -lgcc_s -lgcc
  •   
     

或者,您可以将libcxxrt添加到库列表中   在大多数情况下会得到相同的结果:

     
      
  • clang ++ -stdlib = libc ++ helloworld.cpp -lcxxrt
  •   

这看起来与此主题Making libc++ on Linux user-friendly中讨论的问题有关,下面有选择性引用:

  

问题在于:在构建libc ++时,链接器会找到各种各样的内容   ABI在libstdc ++中起作用,并且很高兴他们在那里。   但是,当Clang为实际程序调用链接器时,它并没有   传递libstdc ++的链接标志,仅用于libc ++。因此,链接   失败。

  

这可以通过显式指定与源库的链接来解决,这里-lsupc ++可以工作。

另见Linux equivalent of Windows DLL forwarders or MacOS reexport_library