clang似乎没有链接到库 [英] clang appears not to be linking to a library

查看:1176
本文介绍了clang似乎没有链接到库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把问题归结为下面的例子:

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

a href =http://coliru.stacked-crooked.com/a/adf42fbd79482935 =nofollow> Coliru :

  /tmp/main-c8b47a.o:在main函数中:
main.cpp :(。text + 0xf):未定义引用`typeinfo for bool'
/ tmp /main-c8b47a.o:在函数`GCC_except_table0'中:
main.cpp :(。gcc_except_table + 0x30):未定义引用`typeinfo for bool'
clang:error:linker命令失败,退出代码1(使用-v查看调用)

Cmd行:


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


这听起来不是链接到库。有谁知道哪些和什么命令行切换将是?我以前没有使用过。这在g ++下工作。



这是带-v开关的输出:

  clang version 3.6.0(tags / RELEASE_360 / final 235480)
目标:x86_64-unknown-linux-gnu
线程模型:posix
找到的候选GCC安装:/ usr / lib / gcc / x86_64-linux-gnu / 4.6
找到的候选GCC安装:/usr/lib/gcc/x86_64-linux-gnu/4.6.4
找到的候选GCC安装:/ usr / lib / gcc / x86_64-linux-gnu / 4.7
找到的候选GCC安装:/usr/lib/gcc/x86_64-linux-gnu/4.7.3
找到的候选GCC安装:/ usr / lib / gcc / x86_64-linux-gnu / 4.8
找到的候选GCC安装:/usr/lib/gcc/x86_64-linux-gnu/4.8.1
找到的候选GCC安装:/ usr / lib / gcc / x86_64- linux-gnu / 4.9
找到的候选GCC安装:/usr/lib/gcc/x86_64-linux-gnu/4.9.2
找到的候选GCC安装:/usr/local/bin/../lib /gcc/x86_64-unknown-linux-gnu/4.8.2
找到的候选GCC安装:/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.0
找到的候选GCC安装:/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.9.2
找到的候选GCC安装:/ usr / local / bin / .. /lib/gcc/x86_64-unknown-linux-gnu/5.1.0
找到的候选GCC安装:/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0
选择的GCC安装:/usr/local/bin/../lib/gcc/x86_64-unknown-linux-gnu/5.2.0
候选multilib:。; @ m64
选择的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 3.6.0基于LLVM 3.6.0默认目标x86_64-unknown-linux-gnu
忽略不存在的目录/ include
#include...搜索从这里开始:
#include< ...>搜索从这里开始:
/ usr / include / c ++ / v1
/ usr / local / include
/usr/local/bin/../lib/clang/3.6.0/include
/ usr / include / x86_64-linux-gnu
/ usr / include
搜索结束列表。
/ 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:在函数`main'中:
main.cpp :( .text + 0xf):未定义对`bool'的typeinfo的引用
/tmp/main-47c098.o:在函数`GCC_except_table0' :
main.cpp :(。gcc_except_table + 0x30):未定义引用`typeinfo for bool'
clang:error:linker命令失败,退出代码为1(使用-v查看调用)


解决方案

看起来您需要添加 lsupc ++ 在main.cpp之后( 查看实时 ):

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

正如Andre Kostur所说 libc ++文档建议如下,虽然我似乎无法在Coliru上工作:

不幸的是,你不能简单地用-stdlib = libc ++在这个
点运行clang,因为clang被设置为链接libc ++链接到libsupc ++。要
绕过这个你必须自己设置你的链接器(或补丁
clang)。例如,




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



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




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


这看起来与此线程中讨论的问题有关在Linux上方便用户使用libc ++ ,选择性引用如下:


这里的问题:当构建libc ++时,链接器在libstdc ++中找到各种
ABI函数,并且对它们很满意。
当Clang调用实际程序的链接器时,
不会传递libstdc ++的链接标志,只有libc ++。因此,链接
失败。


和:


这也可以通过明确指定与源库的链接来解决,这里-lsupc ++是可行的。


另请参阅 Linux相当于Windows DLL转发器或MacOS reexport_library


I boiled down the problem to the following example:

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

generates the following errors on 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

This sounds like it's not linking to a library. Does anyone know which and what the command line switches would be? I've not used clang before. This works under g++.

This is the output with the -v switch:

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)

解决方案

It looks like you need to add -lsupc++ after main.cpp (see it live):

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

As Andre Kostur notes the libc++ documentation recommends the following, although I can not seem to get this to work on Coliru:

Unfortunately you can't simply run clang with "-stdlib=libc++" at this point, as clang is set up to link for libc++ linked to libsupc++. To get around this you'll have to set up your linker yourself (or patch clang). For example,

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

Alternately, you could just add libcxxrt to your libraries list, which in most situations will give the same result:

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

This looks related to issues being discussed in this thread Making libc++ on Linux user-friendly, with selective quotes below:

Here's the problem: when building libc++, the linker finds the various ABI functions in libstdc++, and is quite happy with them being there. When Clang calls the linker for the actual program, though, it doesn't pass along a link flag for libstdc++, only for libc++. Thus, the links fails.

and:

This again can be worked around by explicitly specifying linking against the source library, and here -lsupc++ works.

Also see Linux equivalent of Windows DLL forwarders or MacOS reexport_library.

这篇关于clang似乎没有链接到库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆