std::thread の get_id() で問題が発生し、fmt 経由で印刷した後 (fmt/ostream.h を含めても)、この単純な fmt_test.cpp ファイルをまとめることにしました (これに基づいています) :
#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::char_traits<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::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::char_traits<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)