2015-09-09 9 views
5

मैं निम्न उदाहरण के लिए समस्या नीचे उबला हुआ को जोड़ने जा नहीं:बजना प्रकट होता है एक पुस्तकालय

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 लाइन:

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

ऐसा लगता है कि यह लाइब्रेरी से लिंक नहीं है। क्या किसी को पता है कि कमांड लाइन स्विच कौन सा और क्या होगा? मैंने पहले क्लैंग का उपयोग नहीं किया है। यह जी ++ के तहत काम करता है।

यह -v स्विच के साथ उत्पादन होता है:

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

के रूप में आंद्रे Kostur नोट:

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

आपने क्लैंग और libC++ को कैसे इंस्टॉल किया? क्या आप जानते हैं कि एबीआई लाइब्रेरी क्लैंग किसका उपयोग कर रही है? क्या आप उदा। जोड़ने का प्रयास कर सकते हैं '-lC++ abi'? –

+1

@ जोचिमपिलबोर्ग, मैंने इंस्टॉल नहीं किया था। मुझे SO से ऑनलाइन कंपाइलर का लिंक मिला। ऐसा लगता है कि मैं '-stdlib = libC++' को हटा देता हूं, यह काम करता है। Hmmmm। वह क्यों होगा? – Adrian

+0

आपके द्वारा निर्दिष्ट स्विच जोड़ना या तो काम नहीं करता था। त्रुटि: '/ usr/bin/ld: नहीं मिल सकता -lC++ abi' – Adrian

उत्तर

6

ऐसा लगता है कि आप main.cpp (see it live) के बाद -lsupc++ जोड़ने की जरूरत है लग रहा है libc++ documentation निम्नलिखित की सिफारिश करता है, हालांकि मुझे यह कॉलिरु पर काम करने के लिए प्रतीत नहीं होता है:

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

यह संबंधित लग रहा है मुद्दों के लिए नीचे दिए गए चयनात्मक उद्धरण के साथ इस सूत्र Making libc++ on Linux user-friendly में चर्चा की जा रही,:

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.

और:

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

इसके अलावा Linux equivalent of Windows DLL forwarders or MacOS reexport_library देखते हैं।

+1

दिलचस्प है कि क्लैंग लोग (http: // libcxx पर .llvm.org /) एक अलग कमांड लाइन का उपयोग करके वकील: 'clang ++ -stdlib = libC++ helloworld.cpp -nodefaultlibs -lC++ -lcxxrt -lm -lc -lgcc_s -lgcc' वैकल्पिक रूप से, आप बस अपने पुस्तकालयों में libcxxrt जोड़ सकते हैं सूची, जो ज्यादातर स्थितियों में एक ही परिणाम देगी: 'clang ++ -stdlib = libC++ helloworld.cpp -lcxxrt' –

+0

@AndreKostur मैंने अपने उत्तर में यह नोट जोड़ा, लेकिन यह कॉलिरू के लिए काम नहीं करता है, मुझे स्पष्ट नहीं है । –

संबंधित मुद्दे