【问题标题】:newly-installed Clang can't find or isn't looking for default libraries?新安装的 Clang 找不到或不寻找默认库?
【发布时间】:2020-08-25 05:00:28
【问题描述】:

谷歌搜索发现有几个页面显示了基于 yum 的旧版本 clang 的说明。我是一位经验丰富的程序员,但对管理几乎一无所知,例如不知道 yum 并且命令似乎无法如图所示工作。我确实使用了 dnf,以下导致 clang 可运行,但显然要么找不到或不知道链接到标准 C++ 库、数学库等。与 g++ 相同的命令可以正常工作。

[root@localhost Linux5.3.7-301.fc31.x86_64]# dnf provides clang
Last metadata expiration check: 2:44:22 ago on Sat May  9 10:25:22 2020.
clang-9.0.0-1.fc31.i686 : A C language family front-end for LLVM
Repo        : fedora
Matched from:
Provide    : clang = 9.0.0-1.fc31

clang-9.0.0-1.fc31.x86_64 : A C language family front-end for LLVM
Repo        : fedora
Matched from:
Provide    : clang = 9.0.0-1.fc31

clang-9.0.1-2.fc31.i686 : A C language family front-end for LLVM
Repo        : updates
Matched from:
Provide    : clang = 9.0.1-2.fc31

clang-9.0.1-2.fc31.x86_64 : A C language family front-end for LLVM
Repo        : updates
Matched from:
Provide    : clang = 9.0.1-2.fc31

[root@localhost Linux5.3.7-301.fc31.x86_64]# dnf install clang-9.0.1-2.fc31.x86_64
Last metadata expiration check: 2:44:43 ago on Sat May  9 10:25:22 2020.
Dependencies resolved.
========================================================================================================================================================
 Package                               Architecture                     Version                                 Repository                         Size
========================================================================================================================================================
Installing:
 clang                                 x86_64                           9.0.1-2.fc31                            updates                           379 k
Installing dependencies:
 clang-libs                            x86_64                           9.0.1-2.fc31                            updates                            32 M
Installing weak dependencies:
 compiler-rt                           x86_64                           9.0.1-1.fc31                            updates                           1.9 M
 libomp                                x86_64                           9.0.1-1.fc31                            updates                           350 k

Transaction Summary
========================================================================================================================================================
Install  4 Packages

Total download size: 34 M
Installed size: 164 M
Is this ok [y/N]: Y
Downloading Packages:
(1/4): clang-9.0.1-2.fc31.x86_64.rpm                                                                                     33 kB/s | 379 kB     00:11
(2/4): libomp-9.0.1-1.fc31.x86_64.rpm                                                                                    90 kB/s | 350 kB     00:03
(3/4): compiler-rt-9.0.1-1.fc31.x86_64.rpm                                                                              117 kB/s | 1.9 MB     00:16
(4/4): clang-libs-9.0.1-2.fc31.x86_64.rpm                                                                               228 kB/s |  32 MB     02:22
--------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                   245 kB/s |  34 MB     02:23
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                1/1
  Installing       : libomp-9.0.1-1.fc31.x86_64                                                                                                     1/4
  Installing       : compiler-rt-9.0.1-1.fc31.x86_64                                                                                                2/4
  Installing       : clang-libs-9.0.1-2.fc31.x86_64                                                                                                 3/4
  Installing       : clang-9.0.1-2.fc31.x86_64                                                                                                      4/4
  Running scriptlet: clang-9.0.1-2.fc31.x86_64                                                                                                      4/4
  Verifying        : clang-9.0.1-2.fc31.x86_64                                                                                                      1/4
  Verifying        : clang-libs-9.0.1-2.fc31.x86_64                                                                                                 2/4
  Verifying        : compiler-rt-9.0.1-1.fc31.x86_64                                                                                                3/4
  Verifying        : libomp-9.0.1-1.fc31.x86_64                                                                                                     4/4

Installed:
  clang-9.0.1-2.fc31.x86_64         clang-libs-9.0.1-2.fc31.x86_64         compiler-rt-9.0.1-1.fc31.x86_64         libomp-9.0.1-1.fc31.x86_64

Complete!

然后,天真地可能只是简单地将 g++ 更改为 clang,会产生明显良好的编译,但链接器没有找到,或者甚至没有寻找标准库等:

> clang -DLinux -fPIC -pthread tsfoo.o -Wall -Wno-parentheses -Werror -L../../release/Linux5.3.7-301.fc31.x86_64/lib -L/usr/X11/lib -L/X/lib -L/X/lib -latomic -lFooDebug -lFooTopDebug -o tsfoo

/usr/bin/ld: tsfoo.o: in function `main':
/t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:161: undefined reference to `std::chrono::_V2::system_clock::now()'
/usr/bin/ld: /t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:165: undefined reference to `std::chrono::_V2::system_clock::now()'
/usr/bin/ld: /t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:184: undefined reference to `std::chrono::_V2::system_clock::now()'
/usr/bin/ld: /t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:188: undefined reference to `std::chrono::_V2::system_clock::now()'
/usr/bin/ld: /t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:192: undefined reference to `std::chrono::_V2::system_clock::now()'
/usr/bin/ld: tsfoo.o:/t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:196: more undefined references to `std::chrono::_V2::system_clock::now()' follow
/usr/bin/ld: tsfoo.o: in function `main':
/t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/test/tsfoo.cxx:246: undefined reference to `std::thread::join()'
/usr/bin/ld: tsfoo.o: in function `__static_initialization_and_destruction_0(int, int)':
/usr/include/c++/9/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: /usr/include/c++/9/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: tsfoo.o: in function `std::thread::~thread()':
/usr/include/c++/9/thread:139: undefined reference to `std::terminate()'
/usr/bin/ld: tsfoo.o: in function `void std::vector<std::thread, std::allocator<std::thread> >::_M_realloc_insert<void (&)(int), int&>(__gnu_cxx::__normal_iterator<std::thread*, std::vector<std::thread, std::allocator<std::thread> > >, void (&)(int), int&)':
/usr/include/c++/9/bits/vector.tcc:485: undefined reference to `__cxa_begin_catch'
/usr/bin/ld: /usr/include/c++/9/bits/vector.tcc:493: undefined reference to `__cxa_rethrow'
/usr/bin/ld: /usr/include/c++/9/bits/vector.tcc:485: undefined reference to `__cxa_end_catch'
/usr/bin/ld: tsfoo.o: in function `std::vector<std::thread, std::allocator<std::thread> >::_M_check_len(unsigned long, char const*) const':
/usr/include/c++/9/bits/stl_vector.h:1756: undefined reference to `std::__throw_length_error(char const*)'
/usr/bin/ld: tsfoo.o: in function `__gnu_cxx::new_allocator<std::thread>::deallocate(std::thread*, unsigned long)':
/usr/include/c++/9/ext/new_allocator.h:128: undefined reference to `operator delete(void*)'
/usr/bin/ld: tsfoo.o: in function `std::thread::thread<void (&)(int), int&, void>(void (&)(int), int&)':
/usr/include/c++/9/thread:130: undefined reference to `std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)())'
/usr/bin/ld: tsfoo.o: in function `std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> > std::thread::_S_make_state<std::thread::_Invoker<std::tuple<void (*)(int), int> > >(std::thread::_Invoker<std::tuple<void (*)(int), int> >&&)':
/usr/include/c++/9/thread:206: undefined reference to `operator new(unsigned long)'
/usr/bin/ld: tsfoo.o: in function `__gnu_cxx::new_allocator<std::thread>::allocate(unsigned long, void const*)':
/usr/include/c++/9/ext/new_allocator.h:105: undefined reference to `std::__throw_bad_alloc()'
/usr/bin/ld: /usr/include/c++/9/ext/new_allocator.h:114: undefined reference to `operator new(unsigned long)'
/usr/bin/ld: tsfoo.o: in function `std::thread::_State::_State()':
/usr/include/c++/9/thread:67: undefined reference to `vtable for std::thread::_State'
/usr/bin/ld: tsfoo.o: in function `std::thread* std::__uninitialized_copy<false>::__uninit_copy<std::move_iterator<std::thread*>, std::thread*>(std::move_iterator<std::thread*>, std::move_iterator<std::thread*>, std::thread*)':
/usr/include/c++/9/bits/stl_uninitialized.h:86: undefined reference to `__cxa_begin_catch'
/usr/bin/ld: /usr/include/c++/9/bits/stl_uninitialized.h:89: undefined reference to `__cxa_rethrow'
/usr/bin/ld: /usr/include/c++/9/bits/stl_uninitialized.h:86: undefined reference to `__cxa_end_catch'
/usr/bin/ld: tsfoo.o: in function `std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(int), int> > >::~_State_impl()':
/usr/include/c++/9/thread:187: undefined reference to `std::thread::_State::~_State()'
/usr/bin/ld: tsfoo.o: in function `std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(int), int> > >::~_State_impl()':
/usr/include/c++/9/thread:187: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: tsfoo.o:(.data.rel.ro._ZTINSt6thread11_State_implINS_8_InvokerISt5tupleIJPFviEiEEEEEE[_ZTINSt6thread11_State_implINS_8_InvokerISt5tupleIJPFviEiEEEEEE]+0x0): undefined reference to `vtable for __cxxabiv1::__si_class_type_info'
/usr/bin/ld: tsfoo.o:(.data.rel.ro._ZTINSt6thread11_State_implINS_8_InvokerISt5tupleIJPFviEiEEEEEE[_ZTINSt6thread11_State_implINS_8_InvokerISt5tupleIJPFviEiEEEEEE]+0x10): undefined reference to `typeinfo for std::thread::_State'
/usr/bin/ld: tsfoo.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
/usr/bin/ld: ../../release/Linux5.3.7-301.fc31.x86_64/lib/libFooDebug.a(akstdc.o): in function `AKFPDFromDbl':
/t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/libDebug/akstdc.c:159: undefined reference to `log10'
/usr/bin/ld: /t/proj/Foo/SC_2_3_1/Linux5.3.7-301.fc31.x86_64/libDebug/akstdc.c:161: undefined reference to `pow'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)

【问题讨论】:

  • clang 是一个c 编译器。你为什么要链接到c++ 库? c++ 不是 c 它是一种不同的语言,
  • clang 手册的第一行:clang 是一个 C、C++ 和 Objective-C 编译器。
  • Wiki 页面第一行:Clang /ˈklæŋ/[5] 是 C、C++、Objective-C 和 Objective-C++ 编程语言的编译器前端。
  • Clang 主页第一行:Clang: a C language family frontend for LLVM
  • 他的意思是,虽然 Clang 可以编译多种语言,但 clang 可执行文件用于编译 C。使用 clang++

标签: c++ clang fedora


【解决方案1】:

项目/工具链 Clang 确实可以编译的不仅仅是 C 程序。它还可以处理 C++、Objective-C 和 Objective-C++。

但是,你必须告诉它这样做。

项目的二进制文件clang 处理 C,因此不会(默认情况下)链接到 C++ 标准库中。

The project's binary clang++ will do so.

为 C++ 编写 clang++

【讨论】:

  • 感谢小行星。我假设它会以文件名作为提示。现在它工作正常。
  • @SwissFrank 文件名为tsfoo.o。这是一个已经编译好的目标文件。扩展 .o 提示什么语言?
  • 好点!最初的问题是编译和链接调用,所以没有意识到我的基本假设被我上传的实际示例证明是不可能的。我只是再次确认 Clang 手册页没有提及 clang++,即使作为 SEE ALSO,尽管详细讨论了 C++。现在我想知道,当项目没有提及时,您是如何第一次知道 clang++ 是命令?
  • @SwissFrank 虽然文档确实可以改进,但可以通过谷歌搜索“如何在 clang 中构建 C++”找到解决方案,然后阅读前几个结果。需要一分钟。
  • (我现在记得,我当时想知道clang 将如何检测到对 C++ 标准库的需求,但立即有了一些可能的解释:可能是目标文件中包含未解析的 C++ 标准库符号这将是一个线索!或者他们只是将 C++ 标准库扔到每个有效的链接行中,在那里它可用于 C++,而对于 C/ObjC 则被忽略。或者存在损坏的 C++ 符号,不可能在 C 中生成, 会触发 C++ 标准库。确实考虑到可能的启发式方法,人们想知道为什么它不是自动的。)
猜你喜欢
  • 2022-12-10
  • 2013-02-15
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多