新安装的 Clang 找不到或不在寻找默认库?

newly-installed Clang can't find or isn't looking for default libraries?

谷歌搜索发现几个页面显示了基于 yum 的指令,用于旧的 clang 变体。我是一名经验丰富的程序员,但对管理几乎一无所知,例如不知道 yum 并且命令似乎无法正常工作。我确实使用 dnf,以下结果导致 clang 可以运行,但显然没有找到或不知道 link 到标准 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,结果编译结果看起来不错,但是 linker 没有找到,或者甚至没有寻找标准库等:

> 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,project/toolchain,确实可以编译的不仅仅是C程序。它还可以处理 C++、Objective-C 和 Objective-C++。

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

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

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

为 C++ 编写 clang++