Appleシリコン(M1,M2)Macのg++のエラー

M2 MacでHomebrewでインストールしたg++を使ったときに謎のエラーで落ちたのでそのときのメモ。別のコードでは落ちてなかったのに突然落ちたのでつらみ。再現コード:

#include <iostream>
#include <string>
#include <vector>

signed main() {
    std::vector<std::string> val;
    std::string s = "hoge";
    val.push_back(s);
}
> g++ simple.cpp
0  0x1025181a0  __assert_rtn + 140
1  0x10239fa8c  mach_o::relocatable::Parser<arm64>::parse(mach_o::relocatable::ParserOptions const&) + 4536
2  0x102371d38  mach_o::relocatable::Parser<arm64>::parse(unsigned char const*, unsigned long long, char const*, long, ld::File::Ordinal, mach_o::relocatable::ParserOptions const&) + 148
3  0x1023da4ac  ld::tool::InputFiles::makeFile(Options::FileInfo const&, bool) + 1468
4  0x1023dd360  ___ZN2ld4tool10InputFilesC2ER7Options_block_invoke + 56
5  0x1bff541f4  _dispatch_client_callout2 + 20
6  0x1bff67954  _dispatch_apply_invoke + 224
7  0x1bff541b4  _dispatch_client_callout + 20
8  0x1bff65a04  _dispatch_root_queue_drain + 680
9  0x1bff66104  _dispatch_worker_thread2 + 164
10  0x1c0114324  _pthread_wqthread + 228
A linker snapshot was created at:
        /tmp/a.out-2023-01-16-225032.ld-snapshot
ld: Assertion failed: (_file->_atomsArrayCount == computedAtomCount && "more atoms allocated than expected"), function parse, file macho_relocatable_file.cpp, line 2061.
collect2: error: ld returned 1 exit status

調べてみると、AppleシリコンMacとXcode 14.0の組合せの問題のようだった(リンク)ので、Xcode(ここではCommand Line Tools)を14.1以上にアップデートする。

> softwareupdate --list
...
* Label: Command Line Tools for Xcode-14.2
	Title: Command Line Tools for Xcode, Version: 14.2, Size: 687573KiB, Recommended: YES,
...
> softwareupdate -i "Command Line Tools for Xcode-14.2"
Software Update Tool

Finding available software

Downloaded Command Line Tools for Xcode
Installing Command Line Tools for Xcode
Done with Command Line Tools for Xcode
Done.

直った。

> g++ simple.cpp