尝试使用ostream <<运算符对象时,与{fmt} 6.2.1-3 + clang 10.0.0的链接问题

问题描述

在std :: thread的get_id()有问题并通过fmt打印后(即使我包含了fmt / ostream.h),我发现我将这个简单的fmt_test.cpp文件放在一起(基于{{3 }}):

#include <fmt/core.h>
#include <fmt/ostream.h>
#include <iostream>

class date {
   int  m_year,m_month,m_day;
public:
   date( int year,int month,int day ):
      m_year  (  year ),m_month ( month ),m_day   (   day )
   {}

   friend std::ostream & operator<<( std::ostream &out,date const &d ) {
      return out << d.m_year << '-' << d.m_month << '-' << d.m_day;
   }
};

int main() {
   auto s = fmt::format( "The date is {}",date(2012,12,9) ); // s == "The date is 2012-12-9"
   fmt::print( "{}",s );
   return 0;
}

pacman中可用的fmt版本是6.2.1-3,这是我正在使用的版本。我正在使用带有以下参数的clang 10.0.0进行编译:

clang++ fmt_test.cpp -std=c++20 -stdlib=libc++ -O0 -g -o testbin -lfmt -v

我得到的输出是:

clang version 10.0.0 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /sbin
Found candidate GCC installation: /sbin/../lib/gcc/x86_64-pc-linux-gnu/10.1.0
Found candidate GCC installation: /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-pc-linux-gnu/10.1.0
Found candidate GCC installation: /usr/lib64/gcc/x86_64-pc-linux-gnu/10.1.0
Selected GCC installation: /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
 "/usr/bin/clang-10" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name fmt_test.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mthread-model posix -mframe-pointer=all -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -resource-dir /usr/lib/clang/10.0.0 -internal-isystem /usr/bin/../include/c++/v1 -internal-isystem /usr/local/include -internal-isystem /usr/lib/clang/10.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -std=c++20 -fdeprecated-macro -fdebug-compilation-dir /home/falk/Code/SandBox -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -faddrsig -o /tmp/fmt_test-befb6c.o -x c++ fmt_test.cpp
clang -cc1 version 10.0.0 based upon LLVM 10.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/bin/../include/c++/v1
 /usr/local/include
 /usr/lib/clang/10.0.0/include
 /usr/include
End of search list.
 "/sbin/ld" -pie --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o testbin /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64/Scrt1.o /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64/crti.o /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/crtbeginS.o -L/sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0 -L/sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64 -L/usr/bin/../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../.. -L/usr/bin/../lib -L/lib -L/usr/lib /tmp/fmt_test-befb6c.o -lfmt -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/crtendS.o /sbin/../lib64/gcc/x86_64-pc-linux-gnu/10.1.0/../../../../lib64/crtn.o
/sbin/ld: /tmp/fmt_test-befb6c.o: in function `std::__1::basic_string<char,std::__1::char_traits<char>,std::__1::allocator<char> > fmt::v6::internal::grouping<char>(fmt::v6::internal::locale_ref)':
/usr/include/fmt/format.h:855: undefined reference to `std::__1::basic_string<char,std::__1::allocator<char> > fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)'
/sbin/ld: /tmp/fmt_test-befb6c+.o: in function `void fmt::v6::internal::format_value<char,date>(fmt::v6::internal::buffer<char>&,date const&,fmt::v6::internal::locale_ref)':
/usr/include/fmt/ostream.h:102: undefined reference to `std::__1::locale fmt::v6::internal::locale_ref::get<std::__1::locale>() const'
clang-10: error: linker command Failed with exit code 1 (use -v to see invocation)

有什么主意吗?我试过使用其他参数,例如std = c ++ 17,并在-lfmt的参数位置移动,但无济于事。 :(

一个示例“ fmt_test2.cpp”:

#include <fmt/core.h>
#include <fmt/ostream.h>
#include <iostream>
#include <thread>

void foo() { return; }

int main()
{
    std::thread t1( foo );
    std::thread t2( foo );

    // these two lines work without problems:
    // std::cout << "t1's id: " << t1.get_id() << '\n'
    //           << "t2's id: " << t2.get_id() << '\n';

    // these two lines result in linking errors:
    fmt::print( "t1's id: {}\n",t1.get_id() );
    fmt::print( "t2's id: {}\n",t2.get_id() );

    t1.join();
    t2.join();
}

clang++ -pthread -std=c++20 -stdlib=libc++ -lfmt -O0 -g fmt_test2.cpp -o testbin的构建输出为:

/sbin/ld: /tmp/fmt_test-d2bca4.o: in function `void fmt::v6::internal::format_value<char,std::__1::__thread_id>(fmt::v6::internal::buffer<char>&,std::__1::__thread_id const&,fmt::v6::internal::locale_ref)':
/usr/include/fmt/ostream.h:102: undefined reference to `std::__1::locale fmt::v6::internal::locale_ref::get<std::__1::locale>() const'
/sbin/ld: /tmp/fmt_test-d2bca4.o: in function `std::__1::basic_string<char,std::__1::allocator<char> > fmt::v6::internal::grouping_impl<char>(fmt::v6::internal::locale_ref)'
clang-10: error: linker command Failed with exit code 1 (use -v to see invocation)

解决方法

这似乎是pacman软件包的问题,​​因为您的示例适用于{fmt} 6.2.1:https://godbolt.org/z/14dEfx的常规版本。我建议检查pacman版本的libfmt导出的符号。