102

I'm developing an application in Xcode.

When I try to build, this error comes up:

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)

Does anyone know what's wrong?

tbodt
  • 16,609
  • 6
  • 58
  • 83
  • 66
    Try a full rebuild / clean. It's possible that the a previous build has been abnormally aborted, leaving the `TWRAppDelegate.o` file corrupted or zero-size. – Martin Baulig Dec 25 '12 at 21:27
  • 5
    A little precisation: you do that with cmd+shift+k, if that doesn't work, go into the derived data folder and delete the folder named as your project. – Ramy Al Zuhouri Dec 25 '12 at 21:34
  • 3
    @RamyAlZuhouri: There isn't any need to go Trashing build folders yourself anymore. Hold down Option and the Clean command (which is in the Product menu) changes to “Clean Build Folder…”, which does that for you. – Peter Hosey Dec 26 '12 at 04:38
  • Martin Baulig's suggestion solved my problem. Thanks! – tbodt Dec 31 '12 at 00:45
  • @MartinBaulig If you post your suggestion as an answer, I'll upvote it and mark it as the accepted answer. – tbodt Jul 18 '13 at 19:12
  • @MartinBaulig CAN YOU HEAR ME? – tbodt Jul 25 '13 at 10:33
  • @MartinBaulig : Thanks for letting me steal your comment. My cut-and-paste of it just got to 40 upvotes, and gave me the Guru silver badge. Go figure. :-) – Peter K. Jan 28 '16 at 16:28
  • 2
    @PeterK. It's earning me the Nice Question silver badge :-) – tbodt Jan 28 '16 at 17:49
  • I started to often get this error for a third-party library written in Swift (PromiseKit). It used to work OK when I used the old version that was written in Objective-C. Maybe it's a bug in the Swift compiler? – iosdude Jun 29 '16 at 09:54
  • @tbodt Woot! 100 upvotes!! – Peter K. Nov 12 '21 at 02:20

9 Answers9

212

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.

Peter K.
  • 8,028
  • 4
  • 48
  • 73
  • 3
    My project takes a while to build. You can get away with deleting just the 1-4 object files that get corrupted if you stop the build abnormally rather than everything. – Fernando Mazzon Nov 14 '16 at 12:23
  • Better use @grassyburrito's answer (just add a space or something to the corrupted file); cleaning the build folder is redundant and could waste a good deal of time – nrx Jun 30 '21 at 08:38
  • @nrx I tend to agree, but I've had it where several files were corrupted... and I didn't know until after several attempts at compiling. Depending on your project size, Martin's approach might be faster. And this answer was given two years before grassyburrito's :-) – Peter K. Jun 30 '21 at 19:24
27

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.

grassyburrito
  • 1,213
  • 22
  • 32
12

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.

Anton Plebanovich
  • 1,296
  • 17
  • 17
9

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

yeyimilk
  • 614
  • 2
  • 8
  • 18
6

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
HaiN
  • 917
  • 11
  • 31
  • 1
    I solved it the same way, much faster than a full clean/build on my big project. Next time I will give @Anton Plebanovich 's solution a try, it could be an excellent way to automate this fix. – Romano Jan 27 '20 at 08:26
3

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
Vadim Ahmerov
  • 708
  • 9
  • 12
1

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)
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
1

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.

-1

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

tland
  • 1
  • 2
  • Can just add this for others who find this answer. "Always Search User Path" has been deprecated as of Xcode 8.3 and from the documenation it also says "Disabling it is strongly recommended." (I.e. setting it to NO) – Groot Jul 03 '20 at 12:05