使用llvm-nm查看双架构Mach-O通用二进制库报错的调试方法咨询
nm "Invalid argument" Error for Multi-Arch Mach-O Static Library Let’s walk through actionable steps to troubleshoot this frustrating issue—especially since others can run the exact same command without problems. Here’s how to narrow down the root cause:
First, verify your file’s integrity
Runfile ./my_library.ato confirm the system recognizes it as a valid Mach-O universal archive. You should see output like:./my_library.a: Mach-O universal binary with 2 architectures: [x86_64: current ar archive] [arm64]
If the output doesn’t match, your local file copy is likely corrupted. Try re-copying it from the source others are using, or compare its hash with a known good version usingshasumto rule out partial downloads.Test by targeting architectures explicitly
Instead of lettingnmauto-detect the architectures, specify each one separately to isolate which slice might be causing the problem:nm -arch x86_64 ./my_library.a nm -arch arm64 ./my_library.aIf one command works and the other fails, you’ll know exactly which architecture slice is problematic. If both fail, the issue is likely with the archive wrapper itself, not the individual object files.
Inspect the archive structure with
ar
Use theartool to list the archive’s contents and check for anomalies:ar -t ./my_library.aLook for unexpected files or malformed entries. You can also extract individual object files and validate each one:
ar -x ./my_library.a file *.oIf any
.ofile isn’t recognized as a valid Mach-O object, that’s almost certainly the culprit.Check for environment variable conflicts
Hidden environment variables can sometimes alternm’s behavior. Run this to spot any relevant overrides:env | grep -E '(NM|LLVM|TOOLCHAIN)'To rule out environment interference, run
nmin a clean environment:env -i PATH="$PATH" nm ./my_library.aThis resets all variables except
PATH, ensuring no hidden configurations are messing with the tool.Test with a different toolchain instance
Even if you’re on the same Xcode version, your local toolchain might be corrupted. Try switching to the standalone command-line tools:xcode-select -s /Library/Developer/CommandLineTools nm ./my_library.aIf this works, reset your Xcode toolchain or reinstall the command-line tools:
xcode-select -s /Applications/Xcode.app/Contents/Developer # Or reinstall tools with: xcode-select --installUse
otoolfor more detailed diagnosticsotoolcan often provide more specific error messages about Mach-O file issues. Run:otool -l ./my_library.aIf
otoolfails with a precise error, it can help you pinpoint what’s broken in the file structure thatnmisn’t reporting clearly.
内容的提问来源于stack exchange,提问作者Alonzo Altamirano

