Xcode 链接器错误: 文件对于体系结构 x86_64来说太小

我在 Xcode 开发一个应用程序。

当我尝试构建时,会出现这个错误:

ld: in /Users/theodore/Library/Developer/Xcode/DerivedData/Tower-bkpdifuqssebjdgurzmtirbxejnn/Build/Intermediates/Tower.build/Debug/Tower.build/Objects-normal/x86_64/TWRAppDelegate.o, file too small for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人知道出什么事了吗?

32375 次浏览

Stealing @martin-baulig's answer:

Try a full rebuild / clean. It's possible that the previous build has been abnormally aborted, leaving the TWRAppDelegate.o file corrupted or zero-size.

Since building a clean project may take way too long there is shorter way for those that have the access to the file that is corrupt in the cache:

  • Delete the file (Remove reference)
  • Build project
  • Reinsert file
  • Build project

Full version so you have no trouble finding the file:

  • Find the file in Xcode project navigator
  • Right click the file and press "show in finder" (opens a finder at the location where the file is)
  • Select the file in Xcode and press backspace then click "Remove reference"
  • Build project (it will fail but wait for it to finish)
  • Reinsert file by dragging it from the finder into the same location you just deleted it
  • Build project (should work now)

Step 1. Go to: Project > Build Settings > Search Paths

Step 2. Set "Always Search User Paths" to Yes

Step 3. Build the project (You'll get a warning but the project will build.)

Step 4. Set "Always Search User Paths" back to No and build again to eliminate the warning

rm -rf /Users/hostname/Library/Developer/Xcode/DerivedData

A clean rebuild didn´t in my case so i explain how i solved the problem:
- Removed reference to the file (don´t delete the file)
- Add the file to the project again and run

I usually add a space (could be any character for that matter) to the file in question, remove it and then save. Easier and quicker than a clean build.

You can just delete the TWRAppDelegate.o file and continue your build. Copy the full path mentioned in the error message and paste it behind an 'rm' command in your terminal. There's no need to clean/rebuild, delete derived data, add/remove the file from the project, etc.

just remove this file by run cmd in your terminal app:

rm /Users/theodore/Library/Developer/Xcode/DerivedData/Tower-bkpdifuqssebjdgurzmtirbxejnn/Build/Intermediates/Tower.build/Debug/Tower.build/Objects-normal/x86_64/TWRAppDelegate.o

To automatically fix this issue Build Script Phase can be added. Goto Xcode -> Your Project -> Your Target -> Build Phases -> + -> New Run Script Phase

Rename it to Xcode Link Fix and move it above Compile Sources phase. Paste this into script body:

# Legacy build system
legacy_dir=`dirname "${LD_DEPENDENCY_INFO_FILE}"`
if [ -d "${legacy_dir}" ]; then
find "${legacy_dir}" -size 0 | while read -d $'\n' file; do
rm "$file"
done
fi


# New build system
if [ -d "${OBJECT_FILE_DIR_normal}" ]; then
find "${OBJECT_FILE_DIR_normal}" -size 0 | while read -d $'\n' file; do
rm "$file"
done
fi

This script checks for object files with zero size and removes them so when compilation is done in next step it success.

You need to add this script for every app target if you have many.

This script takes ~0.1 second to run and saves you from full project rebuild.

Quick way to fix error without complete cache clean:

  1. Open file described in error (in case of this question TWRAppDelegate)
  2. cmd + A
  3. cmd + X
  4. Rebuild - fail
  5. cmd + V
  6. Rebuild - succeed